Fix migrations

This commit is contained in:
Till Faelligen 2022-02-16 18:03:48 +01:00
parent 849fc301e6
commit e28c6f1f61
4 changed files with 31 additions and 23 deletions

View file

@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS account_accounts (
-- If the account is currently active -- If the account is currently active
is_deactivated BOOLEAN DEFAULT FALSE, is_deactivated BOOLEAN DEFAULT FALSE,
-- The account_type (user = 1, guest = 2, admin = 3, appservice = 4) -- The account_type (user = 1, guest = 2, admin = 3, appservice = 4)
account_type SMALLINT account_type SMALLINT NOT NULL
-- TODO: -- TODO:
-- upgraded_ts, devices, any email reset stuff? -- upgraded_ts, devices, any email reset stuff?
); );

View file

@ -12,18 +12,15 @@ func LoadAddAccountType(m *sqlutil.Migrations) {
} }
func UpAddAccountType(tx *sql.Tx) error { func UpAddAccountType(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE account_accounts ADD COLUMN IF NOT EXISTS account_type SMALLINT;") // initially set every account to useraccount, change appservice and guest accounts afterwards
// (user = 1, guest = 2, admin = 3, appservice = 4)
_, err := tx.Exec(`ALTER TABLE account_accounts ADD COLUMN IF NOT EXISTS account_type SMALLINT NOT NULL DEFAULT 1;
UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> '';
UPDATE account_accounts SET account_type = 2 WHERE localpart ~ '^[0-9]+$';
ALTER TABLE account_accounts ALTER COLUMN account_type DROP DEFAULT;`,
)
if err != nil { if err != nil {
return fmt.Errorf("failed to add column: %w", err) return fmt.Errorf("failed to execute upgrade: %w", err)
}
_, err = tx.Exec("UPDATE account_accounts SET account_type = 1 WHERE appservice_id = '' OR appservice_id IS NULL;")
if err != nil {
return fmt.Errorf("failed to update user accounts: %w", err)
}
_, err = tx.Exec("UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> '';")
if err != nil {
return fmt.Errorf("failed to update appservice accounts upgrade: %w", err)
} }
return nil return nil
} }

View file

@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS account_accounts (
-- If the account is currently active -- If the account is currently active
is_deactivated BOOLEAN DEFAULT 0, is_deactivated BOOLEAN DEFAULT 0,
-- The account_type (user = 1, guest = 2, admin = 3, appservice = 4) -- The account_type (user = 1, guest = 2, admin = 3, appservice = 4)
account_type INTEGER account_type INTEGER NOT NULL
-- TODO: -- TODO:
-- upgraded_ts, devices, any email reset stuff? -- upgraded_ts, devices, any email reset stuff?
); );

View file

@ -18,19 +18,30 @@ func LoadAddAccountType(m *sqlutil.Migrations) {
} }
func UpAddAccountType(tx *sql.Tx) error { func UpAddAccountType(tx *sql.Tx) error {
// initially set every account to useracount, change appserver accounts afterwards // initially set every account to useraccount, change appservice and guest accounts afterwards
_, err := tx.Exec(`ALTER TABLE account_accounts ADD COLUMN account_type INTEGER;`) // (user = 1, guest = 2, admin = 3, appservice = 4)
_, err := tx.Exec(`ALTER TABLE account_accounts RENAME TO account_accounts_tmp;
CREATE TABLE account_accounts (
localpart TEXT NOT NULL PRIMARY KEY,
created_ts BIGINT NOT NULL,
password_hash TEXT,
appservice_id TEXT,
is_deactivated BOOLEAN DEFAULT 0,
account_type INTEGER NOT NULL
);
INSERT
INTO account_accounts (
localpart, created_ts, password_hash, appservice_id, account_type
) SELECT
localpart, created_ts, password_hash, appservice_id, 1
FROM account_accounts_tmp
;
UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> '';
UPDATE account_accounts SET account_type = 2 WHERE localpart GLOB '[0-9]*';
DROP TABLE account_accounts_tmp;`)
if err != nil { if err != nil {
return fmt.Errorf("failed to add column: %w", err) return fmt.Errorf("failed to add column: %w", err)
} }
_, err = tx.Exec(`UPDATE account_accounts SET account_type = 1 WHERE appservice_id = '' OR appservice_id IS NULL`)
if err != nil {
return fmt.Errorf("failed to update user accounts: %w", err)
}
_, err = tx.Exec(`UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> ''`)
if err != nil {
return fmt.Errorf("failed to update appservice accounts upgrade: %w", err)
}
return nil return nil
} }