update db-move to support migration version 14

This commit is contained in:
finn 2022-04-25 17:21:52 -07:00
parent e50f392288
commit 8458f63316
2 changed files with 40 additions and 36 deletions

View file

@ -17,6 +17,8 @@ import (
"gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/common"
) )
const expectedMigrationVersion = "14"
var ( var (
sqlitePath string sqlitePath string
postgresURL string postgresURL string
@ -166,7 +168,7 @@ var (
func verifyMigration(source *sql.DB) error { func verifyMigration(source *sql.DB) error {
// Lower bound of the database state. // Lower bound of the database state.
rows, err := source.Query("SELECT * FROM flyway_schema_history WHERE version = 12") rows, err := source.Query("SELECT version FROM flyway_schema_history ORDER BY installed_rank DESC LIMIT 1")
if err != nil { if err != nil {
return err return err
} }
@ -176,16 +178,16 @@ func verifyMigration(source *sql.DB) error {
return errors.New("source database is not up to date! Please update signald and start it to move the sqlite database to an acceptable format") return errors.New("source database is not up to date! Please update signald and start it to move the sqlite database to an acceptable format")
} }
// Upper bound of the database state var version string
rows, err = source.Query("SELECT * FROM flyway_schema_history WHERE version = 13") err = rows.Scan(&version)
if err != nil { if err != nil {
return err return err
} }
defer rows.Close()
if rows.Next() { if version != expectedMigrationVersion {
return errors.New("source database is too new! Please update signaldctl to the latest version") return fmt.Errorf("source database must be on migration %s (found %s instead)", expectedMigrationVersion, version)
} }
return nil return nil
} }
@ -235,7 +237,7 @@ func moveAccounts(source *sql.DB, dest *sql.DB) error {
} }
func moveRecipients(source *sql.DB, dest *sql.DB) error { func moveRecipients(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT rowid, account_uuid, uuid, e164 FROM recipients") rows, err := source.Query("SELECT rowid, account_uuid, uuid, e164, registered FROM recipients")
if err != nil { if err != nil {
return err return err
} }
@ -247,8 +249,9 @@ func moveRecipients(source *sql.DB, dest *sql.DB) error {
accountUUID uuid.UUID accountUUID uuid.UUID
recipientUUID uuid.NullUUID recipientUUID uuid.NullUUID
e164 sql.NullString e164 sql.NullString
registered bool
) )
err = rows.Scan(&rowID, &accountUUID, &recipientUUID, &e164) err = rows.Scan(&rowID, &accountUUID, &recipientUUID, &e164, &registered)
if err != nil { if err != nil {
return err return err
} }
@ -259,7 +262,7 @@ func moveRecipients(source *sql.DB, dest *sql.DB) error {
e164.String = "" e164.String = ""
} }
_, err = dest.Exec("INSERT INTO signald_recipients (rowid, account_uuid, uuid, e164) VALUES ($1, $2, $3, $4)", rowID, accountUUID, recipientUUID, e164) _, err = dest.Exec("INSERT INTO signald_recipients (rowid, account_uuid, uuid, e164, registered) VALUES ($1, $2, $3, $4, $5)", rowID, accountUUID, recipientUUID, e164, registered)
if err != nil { if err != nil {
return err return err
} }

View file

@ -107,6 +107,7 @@ var (
account_uuid UUID NOT NULL REFERENCES signald_accounts(uuid) ON DELETE CASCADE, account_uuid UUID NOT NULL REFERENCES signald_accounts(uuid) ON DELETE CASCADE,
uuid UUID, uuid UUID,
e164 TEXT, e164 TEXT,
registered BOOLEAN DEFAULT true,
UNIQUE (account_uuid, e164, uuid) UNIQUE (account_uuid, e164, uuid)
); );