From a505471c90895b4e340a0989fe5e4bbe1b42c101 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 14 Feb 2022 14:52:16 +0100 Subject: [PATCH] Add table migrations --- .../accounts/postgres/accounts_table.go | 4 +-- .../deltas/20200929203058_is_active.go | 1 + .../2022021414375800_add_policy_version.go | 28 +++++++++++++++++++ userapi/storage/accounts/postgres/storage.go | 1 + .../accounts/sqlite3/accounts_table.go | 4 +-- .../deltas/20200929203058_is_active.go | 1 + .../2022021414375800_add_policy_version.go | 28 +++++++++++++++++++ userapi/storage/accounts/sqlite3/storage.go | 1 + 8 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 userapi/storage/accounts/postgres/deltas/2022021414375800_add_policy_version.go create mode 100644 userapi/storage/accounts/sqlite3/deltas/2022021414375800_add_policy_version.go diff --git a/userapi/storage/accounts/postgres/accounts_table.go b/userapi/storage/accounts/postgres/accounts_table.go index 874b8abcf..db8cdecdf 100644 --- a/userapi/storage/accounts/postgres/accounts_table.go +++ b/userapi/storage/accounts/postgres/accounts_table.go @@ -68,10 +68,10 @@ const selectNewNumericLocalpartSQL = "" + "SELECT nextval('numeric_username_seq')" const selectPrivacyPolicySQL = "" + - "SELECT policy_version FROM accounts_accounts WHERE localpart = $1" + "SELECT policy_version FROM account_accounts WHERE localpart = $1" const batchSelectPrivacyPolicySQL = "" + - "SELECT localpart FROM accounts_accounts WHERE policy_version IS NULL or policy_version <> $1" + "SELECT localpart FROM account_accounts WHERE policy_version IS NULL or policy_version <> $1" type accountsStatements struct { insertAccountStmt *sql.Stmt diff --git a/userapi/storage/accounts/postgres/deltas/20200929203058_is_active.go b/userapi/storage/accounts/postgres/deltas/20200929203058_is_active.go index 9e14286e0..1d50d3d5a 100644 --- a/userapi/storage/accounts/postgres/deltas/20200929203058_is_active.go +++ b/userapi/storage/accounts/postgres/deltas/20200929203058_is_active.go @@ -10,6 +10,7 @@ import ( func LoadFromGoose() { goose.AddMigration(UpIsActive, DownIsActive) + goose.AddMigration(UpAddPolicyVersion, DownAddPolicyVersion) } func LoadIsActive(m *sqlutil.Migrations) { diff --git a/userapi/storage/accounts/postgres/deltas/2022021414375800_add_policy_version.go b/userapi/storage/accounts/postgres/deltas/2022021414375800_add_policy_version.go new file mode 100644 index 000000000..8cbbe6f33 --- /dev/null +++ b/userapi/storage/accounts/postgres/deltas/2022021414375800_add_policy_version.go @@ -0,0 +1,28 @@ +package deltas + +import ( + "database/sql" + "fmt" + + "github.com/matrix-org/dendrite/internal/sqlutil" +) + +func LoadAddPolicyVersion(m *sqlutil.Migrations) { + m.AddMigration(UpAddPolicyVersion, DownAddPolicyVersion) +} + +func UpAddPolicyVersion(tx *sql.Tx) error { + _, err := tx.Exec("ALTER TABLE account_accounts ADD COLUMN IF NOT EXISTS policy_version TEXT;") + if err != nil { + return fmt.Errorf("failed to execute upgrade: %w", err) + } + return nil +} + +func DownAddPolicyVersion(tx *sql.Tx) error { + _, err := tx.Exec("ALTER TABLE account_accounts DROP COLUMN policy_version;") + if err != nil { + return fmt.Errorf("failed to execute downgrade: %w", err) + } + return nil +} diff --git a/userapi/storage/accounts/postgres/storage.go b/userapi/storage/accounts/postgres/storage.go index b128f450c..ff91ae447 100644 --- a/userapi/storage/accounts/postgres/storage.go +++ b/userapi/storage/accounts/postgres/storage.go @@ -73,6 +73,7 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver } m := sqlutil.NewMigrations() deltas.LoadIsActive(m) + deltas.LoadAddPolicyVersion(m) if err = m.RunDeltas(db, dbProperties); err != nil { return nil, err } diff --git a/userapi/storage/accounts/sqlite3/accounts_table.go b/userapi/storage/accounts/sqlite3/accounts_table.go index eab6114f1..95b9eb7bf 100644 --- a/userapi/storage/accounts/sqlite3/accounts_table.go +++ b/userapi/storage/accounts/sqlite3/accounts_table.go @@ -66,10 +66,10 @@ const selectNewNumericLocalpartSQL = "" + "SELECT COUNT(localpart) FROM account_accounts" const selectPrivacyPolicySQL = "" + - "SELECT policy_version FROM accounts_accounts WHERE localpart = $1" + "SELECT policy_version FROM account_accounts WHERE localpart = $1" const batchSelectPrivacyPolicySQL = "" + - "SELECT localpart FROM accounts_accounts WHERE policy_version IS NULL or policy_version <> $1" + "SELECT localpart FROM account_accounts WHERE policy_version IS NULL or policy_version <> $1" type accountsStatements struct { db *sql.DB diff --git a/userapi/storage/accounts/sqlite3/deltas/20200929203058_is_active.go b/userapi/storage/accounts/sqlite3/deltas/20200929203058_is_active.go index 9fddb05a1..0b95b4996 100644 --- a/userapi/storage/accounts/sqlite3/deltas/20200929203058_is_active.go +++ b/userapi/storage/accounts/sqlite3/deltas/20200929203058_is_active.go @@ -10,6 +10,7 @@ import ( func LoadFromGoose() { goose.AddMigration(UpIsActive, DownIsActive) + goose.AddMigration(UpAddPolicyVersion, DownAddPolicyVersion) } func LoadIsActive(m *sqlutil.Migrations) { diff --git a/userapi/storage/accounts/sqlite3/deltas/2022021414375800_add_policy_version.go b/userapi/storage/accounts/sqlite3/deltas/2022021414375800_add_policy_version.go new file mode 100644 index 000000000..ae69cf0f0 --- /dev/null +++ b/userapi/storage/accounts/sqlite3/deltas/2022021414375800_add_policy_version.go @@ -0,0 +1,28 @@ +package deltas + +import ( + "database/sql" + "fmt" + + "github.com/matrix-org/dendrite/internal/sqlutil" +) + +func LoadAddPolicyVersion(m *sqlutil.Migrations) { + m.AddMigration(UpAddPolicyVersion, DownAddPolicyVersion) +} + +func UpAddPolicyVersion(tx *sql.Tx) error { + _, err := tx.Exec("ALTER TABLE account_accounts ADD COLUMN policy_version TEXT;") + if err != nil { + return fmt.Errorf("failed to execute upgrade: %w", err) + } + return nil +} + +func DownAddPolicyVersion(tx *sql.Tx) error { + _, err := tx.Exec("ALTER TABLE account_accounts DROP COLUMN policy_version;") + if err != nil { + return fmt.Errorf("failed to execute downgrade: %w", err) + } + return nil +} diff --git a/userapi/storage/accounts/sqlite3/storage.go b/userapi/storage/accounts/sqlite3/storage.go index 911492d53..c7551299b 100644 --- a/userapi/storage/accounts/sqlite3/storage.go +++ b/userapi/storage/accounts/sqlite3/storage.go @@ -77,6 +77,7 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver } m := sqlutil.NewMigrations() deltas.LoadIsActive(m) + deltas.LoadAddPolicyVersion(m) if err = m.RunDeltas(db, dbProperties); err != nil { return nil, err }