Refactor migration functions & messages

Ensure that success & error migration logs use the same description text
for the migration step that they refer to.
This commit is contained in:
Andrew Ferrazzutti 2022-08-11 10:42:43 -04:00 committed by Finn
parent dd208b6e82
commit 67a7576e24

View file

@ -60,7 +60,7 @@ var (
} }
return nil return nil
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) (err error) {
source, err := sql.Open("sqlite3", sqlitePath) source, err := sql.Open("sqlite3", sqlitePath)
if err != nil { if err != nil {
return err return err
@ -93,83 +93,33 @@ var (
} }
log.Println("created schema") log.Println("created schema")
if err := moveAccounts(source, dest); err != nil { migrate := func(fn func(*sql.DB, *sql.DB) error, targetName string) {
log.Println("error moving accounts table") if err = fn(source, dest); err != nil {
return err log.Println("error moving", targetName)
panic(err)
}
log.Println("moved", targetName)
} }
log.Println("moved accounts table") defer func() {
if r := recover(); r != nil && r != err {
// If r is something other than the error returned via the named return, re-panic it
panic(r)
}
}()
if err := moveRecipients(source, dest); err != nil { migrate(moveAccounts, "accounts table")
log.Println("error moving recipients table") migrate(moveRecipients, "recipients table")
return err migrate(movePrekeys, "prekeys table")
} migrate(moveSessions, "sessions table")
log.Println("moved recipients table") migrate(moveSignedPrekeys, "signed prekeys table")
migrate(moveIdentityKeys, "identity keys table")
if err := movePrekeys(source, dest); err != nil { migrate(moveAccountData, "account data")
log.Println("error moving prekeys table") migrate(movePendingAccountData, "pending account data table")
return err migrate(moveSenderKeys, "sender keys table")
} migrate(moveSenderKeyShared, "sender key shared table")
log.Println("moved prekeys table") migrate(moveGroups, "groups table")
migrate(moveGroupCredentials, "group credentials table")
if err := moveSessions(source, dest); err != nil { migrate(moveContacts, "contacts table")
log.Println("error moving sessions table")
return err
}
log.Println("moved sessions table")
if err := moveSignedPrekeys(source, dest); err != nil {
log.Println("error moving signed prekeys table")
return err
}
log.Println("moved signed prekeys table")
if err := moveIdentityKeys(source, dest); err != nil {
log.Println("error moving identity keys table")
return err
}
log.Println("moved identity keys table")
if err := moveAccountData(source, dest); err != nil {
log.Println("error moving account data")
return err
}
log.Println("moved account data table")
if err := movePendingAccountData(source, dest); err != nil {
log.Println("error moving pending account data tabe")
return err
}
log.Println("moved pending account data table")
if err := moveSenderKeys(source, dest); err != nil {
log.Println("error moving sender keys table")
return err
}
log.Println("moved sender keys table")
if err := moveSenderKeyShared(source, dest); err != nil {
log.Println("error moving sender key shared table")
return err
}
log.Println("moved sender key shared table")
if err := moveGroups(source, dest); err != nil {
log.Println("error moving groups table")
return err
}
log.Println("moved groups table")
if err := moveGroupCredentials(source, dest); err != nil {
log.Println("error moving group credentials table")
return err
}
log.Println("moved group credentials table")
if err := moveContacts(source, dest); err != nil {
log.Println("error moving group credentials table")
return err
}
log.Println("moved contacts table")
if err := moveProfileKeys(source, dest); err != nil { if err := moveProfileKeys(source, dest); err != nil {
log.Println("error moving profile keys table") log.Println("error moving profile keys table")