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
},
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {
source, err := sql.Open("sqlite3", sqlitePath)
if err != nil {
return err
@ -93,83 +93,33 @@ var (
}
log.Println("created schema")
if err := moveAccounts(source, dest); err != nil {
log.Println("error moving accounts table")
return err
migrate := func(fn func(*sql.DB, *sql.DB) error, targetName string) {
if err = fn(source, dest); err != nil {
log.Println("error moving", targetName)
panic(err)
}
log.Println("moved accounts table")
log.Println("moved", targetName)
}
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 {
log.Println("error moving recipients table")
return err
}
log.Println("moved recipients table")
if err := movePrekeys(source, dest); err != nil {
log.Println("error moving prekeys table")
return err
}
log.Println("moved prekeys table")
if err := moveSessions(source, dest); err != nil {
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")
migrate(moveAccounts, "accounts table")
migrate(moveRecipients, "recipients table")
migrate(movePrekeys, "prekeys table")
migrate(moveSessions, "sessions table")
migrate(moveSignedPrekeys, "signed prekeys table")
migrate(moveIdentityKeys, "identity keys table")
migrate(moveAccountData, "account data")
migrate(movePendingAccountData, "pending account data table")
migrate(moveSenderKeys, "sender keys table")
migrate(moveSenderKeyShared, "sender key shared table")
migrate(moveGroups, "groups table")
migrate(moveGroupCredentials, "group credentials table")
migrate(moveContacts, "contacts table")
if err := moveProfileKeys(source, dest); err != nil {
log.Println("error moving profile keys table")