diff --git a/userapi/storage/accounts/postgres/accounts_table.go b/userapi/storage/accounts/postgres/accounts_table.go index cc60b57c0..9e3e456a7 100644 --- a/userapi/storage/accounts/postgres/accounts_table.go +++ b/userapi/storage/accounts/postgres/accounts_table.go @@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS account_accounts ( -- If the account is currently active is_deactivated BOOLEAN DEFAULT FALSE, -- The account_type (user = 1, guest = 2, admin = 3, appservice = 4) - account_type SMALLINT + account_type SMALLINT NOT NULL -- TODO: -- upgraded_ts, devices, any email reset stuff? ); diff --git a/userapi/storage/accounts/postgres/deltas/2022021013023800_add_account_type.go b/userapi/storage/accounts/postgres/deltas/2022021013023800_add_account_type.go index 5489a5a20..2fae00cb9 100644 --- a/userapi/storage/accounts/postgres/deltas/2022021013023800_add_account_type.go +++ b/userapi/storage/accounts/postgres/deltas/2022021013023800_add_account_type.go @@ -12,18 +12,15 @@ func LoadAddAccountType(m *sqlutil.Migrations) { } 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 { - 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 fmt.Errorf("failed to execute upgrade: %w", err) } return nil } diff --git a/userapi/storage/accounts/sqlite3/accounts_table.go b/userapi/storage/accounts/sqlite3/accounts_table.go index ec8cd13f5..5a918e034 100644 --- a/userapi/storage/accounts/sqlite3/accounts_table.go +++ b/userapi/storage/accounts/sqlite3/accounts_table.go @@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS account_accounts ( -- If the account is currently active is_deactivated BOOLEAN DEFAULT 0, -- The account_type (user = 1, guest = 2, admin = 3, appservice = 4) - account_type INTEGER + account_type INTEGER NOT NULL -- TODO: -- upgraded_ts, devices, any email reset stuff? ); diff --git a/userapi/storage/accounts/sqlite3/deltas/2022021012490600_add_account_type.go b/userapi/storage/accounts/sqlite3/deltas/2022021012490600_add_account_type.go index 620e6185c..9b058dedd 100644 --- a/userapi/storage/accounts/sqlite3/deltas/2022021012490600_add_account_type.go +++ b/userapi/storage/accounts/sqlite3/deltas/2022021012490600_add_account_type.go @@ -18,19 +18,30 @@ func LoadAddAccountType(m *sqlutil.Migrations) { } func UpAddAccountType(tx *sql.Tx) error { - // initially set every account to useracount, change appserver accounts afterwards - _, err := tx.Exec(`ALTER TABLE account_accounts ADD COLUMN account_type INTEGER;`) + // 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 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 { 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 }