Add table migrations

This commit is contained in:
Till Faelligen 2022-02-14 14:52:16 +01:00
parent 3c5c3ea7fb
commit a505471c90
8 changed files with 64 additions and 4 deletions

View file

@ -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

View file

@ -10,6 +10,7 @@ import (
func LoadFromGoose() {
goose.AddMigration(UpIsActive, DownIsActive)
goose.AddMigration(UpAddPolicyVersion, DownAddPolicyVersion)
}
func LoadIsActive(m *sqlutil.Migrations) {

View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -10,6 +10,7 @@ import (
func LoadFromGoose() {
goose.AddMigration(UpIsActive, DownIsActive)
goose.AddMigration(UpAddPolicyVersion, DownAddPolicyVersion)
}
func LoadIsActive(m *sqlutil.Migrations) {

View file

@ -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
}

View file

@ -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
}