mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 06:12:45 +00:00
Namespace user API tables (#2806)
This migrates all the various user API tables, indices and sequences to be `userapi_`-namespaced, rather than the mess they are all now.
This commit is contained in:
parent
9c189b1b80
commit
3aa92efaa3
34 changed files with 441 additions and 194 deletions
|
@ -8,8 +8,8 @@ import (
|
|||
|
||||
func UpIsActive(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
ALTER TABLE account_accounts RENAME TO account_accounts_tmp;
|
||||
CREATE TABLE account_accounts (
|
||||
ALTER TABLE userapi_accounts RENAME TO userapi_accounts_tmp;
|
||||
CREATE TABLE userapi_accounts (
|
||||
localpart TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL,
|
||||
password_hash TEXT,
|
||||
|
@ -17,13 +17,13 @@ CREATE TABLE account_accounts (
|
|||
is_deactivated BOOLEAN DEFAULT 0
|
||||
);
|
||||
INSERT
|
||||
INTO account_accounts (
|
||||
INTO userapi_accounts (
|
||||
localpart, created_ts, password_hash, appservice_id
|
||||
) SELECT
|
||||
localpart, created_ts, password_hash, appservice_id
|
||||
FROM account_accounts_tmp
|
||||
FROM userapi_accounts_tmp
|
||||
;
|
||||
DROP TABLE account_accounts_tmp;`)
|
||||
DROP TABLE userapi_accounts_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||
}
|
||||
|
@ -32,21 +32,21 @@ DROP TABLE account_accounts_tmp;`)
|
|||
|
||||
func DownIsActive(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
ALTER TABLE account_accounts RENAME TO account_accounts_tmp;
|
||||
CREATE TABLE account_accounts (
|
||||
ALTER TABLE userapi_accounts RENAME TO userapi_accounts_tmp;
|
||||
CREATE TABLE userapi_accounts (
|
||||
localpart TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL,
|
||||
password_hash TEXT,
|
||||
appservice_id TEXT
|
||||
);
|
||||
INSERT
|
||||
INTO account_accounts (
|
||||
INTO userapi_accounts (
|
||||
localpart, created_ts, password_hash, appservice_id
|
||||
) SELECT
|
||||
localpart, created_ts, password_hash, appservice_id
|
||||
FROM account_accounts_tmp
|
||||
FROM userapi_accounts_tmp
|
||||
;
|
||||
DROP TABLE account_accounts_tmp;`)
|
||||
DROP TABLE userapi_accounts_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute downgrade: %w", err)
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@ import (
|
|||
|
||||
func UpLastSeenTSIP(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
ALTER TABLE device_devices RENAME TO device_devices_tmp;
|
||||
CREATE TABLE device_devices (
|
||||
ALTER TABLE userapi_devices RENAME TO userapi_devices_tmp;
|
||||
CREATE TABLE userapi_devices (
|
||||
access_token TEXT PRIMARY KEY,
|
||||
session_id INTEGER,
|
||||
device_id TEXT ,
|
||||
|
@ -22,12 +22,12 @@ func UpLastSeenTSIP(ctx context.Context, tx *sql.Tx) error {
|
|||
UNIQUE (localpart, device_id)
|
||||
);
|
||||
INSERT
|
||||
INTO device_devices (
|
||||
INTO userapi_devices (
|
||||
access_token, session_id, device_id, localpart, created_ts, display_name, last_seen_ts, ip, user_agent
|
||||
) SELECT
|
||||
access_token, session_id, device_id, localpart, created_ts, display_name, created_ts, '', ''
|
||||
FROM device_devices_tmp;
|
||||
DROP TABLE device_devices_tmp;`)
|
||||
FROM userapi_devices_tmp;
|
||||
DROP TABLE userapi_devices_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||
}
|
||||
|
@ -36,8 +36,8 @@ func UpLastSeenTSIP(ctx context.Context, tx *sql.Tx) error {
|
|||
|
||||
func DownLastSeenTSIP(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
ALTER TABLE device_devices RENAME TO device_devices_tmp;
|
||||
CREATE TABLE IF NOT EXISTS device_devices (
|
||||
ALTER TABLE userapi_devices RENAME TO userapi_devices_tmp;
|
||||
CREATE TABLE IF NOT EXISTS userapi_devices (
|
||||
access_token TEXT PRIMARY KEY,
|
||||
session_id INTEGER,
|
||||
device_id TEXT ,
|
||||
|
@ -47,12 +47,12 @@ CREATE TABLE IF NOT EXISTS device_devices (
|
|||
UNIQUE (localpart, device_id)
|
||||
);
|
||||
INSERT
|
||||
INTO device_devices (
|
||||
INTO userapi_devices (
|
||||
access_token, session_id, device_id, localpart, created_ts, display_name
|
||||
) SELECT
|
||||
access_token, session_id, device_id, localpart, created_ts, display_name
|
||||
FROM device_devices_tmp;
|
||||
DROP TABLE device_devices_tmp;`)
|
||||
FROM userapi_devices_tmp;
|
||||
DROP TABLE userapi_devices_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute downgrade: %w", err)
|
||||
}
|
||||
|
|
|
@ -9,8 +9,8 @@ import (
|
|||
func UpAddAccountType(ctx context.Context, tx *sql.Tx) error {
|
||||
// initially set every account to useraccount, change appservice and guest accounts afterwards
|
||||
// (user = 1, guest = 2, admin = 3, appservice = 4)
|
||||
_, err := tx.ExecContext(ctx, `ALTER TABLE account_accounts RENAME TO account_accounts_tmp;
|
||||
CREATE TABLE account_accounts (
|
||||
_, err := tx.ExecContext(ctx, `ALTER TABLE userapi_accounts RENAME TO userapi_accounts_tmp;
|
||||
CREATE TABLE userapi_accounts (
|
||||
localpart TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL,
|
||||
password_hash TEXT,
|
||||
|
@ -19,15 +19,15 @@ CREATE TABLE account_accounts (
|
|||
account_type INTEGER NOT NULL
|
||||
);
|
||||
INSERT
|
||||
INTO account_accounts (
|
||||
INTO userapi_accounts (
|
||||
localpart, created_ts, password_hash, appservice_id, account_type
|
||||
) SELECT
|
||||
localpart, created_ts, password_hash, appservice_id, 1
|
||||
FROM account_accounts_tmp
|
||||
FROM userapi_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;`)
|
||||
UPDATE userapi_accounts SET account_type = 4 WHERE appservice_id <> '';
|
||||
UPDATE userapi_accounts SET account_type = 2 WHERE localpart GLOB '[0-9]*';
|
||||
DROP TABLE userapi_accounts_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to add column: %w", err)
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ DROP TABLE account_accounts_tmp;`)
|
|||
}
|
||||
|
||||
func DownAddAccountType(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `ALTER TABLE account_accounts DROP COLUMN account_type;`)
|
||||
_, err := tx.ExecContext(ctx, `ALTER TABLE userapi_accounts DROP COLUMN account_type;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute downgrade: %w", err)
|
||||
}
|
||||
|
|
109
userapi/storage/sqlite3/deltas/2022101711000000_rename_tables.go
Normal file
109
userapi/storage/sqlite3/deltas/2022101711000000_rename_tables.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package deltas
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var renameTableMappings = map[string]string{
|
||||
"account_accounts": "userapi_accounts",
|
||||
"account_data": "userapi_account_datas",
|
||||
"device_devices": "userapi_devices",
|
||||
"account_e2e_room_keys": "userapi_key_backups",
|
||||
"account_e2e_room_keys_versions": "userapi_key_backup_versions",
|
||||
"login_tokens": "userapi_login_tokens",
|
||||
"open_id_tokens": "userapi_openid_tokens",
|
||||
"account_profiles": "userapi_profiles",
|
||||
"account_threepid": "userapi_threepids",
|
||||
}
|
||||
|
||||
var renameIndicesMappings = map[string]string{
|
||||
"device_localpart_id_idx": "userapi_device_localpart_id_idx",
|
||||
"e2e_room_keys_idx": "userapi_key_backups_idx",
|
||||
"e2e_room_keys_versions_idx": "userapi_key_backups_versions_idx",
|
||||
"account_e2e_room_keys_versions_idx": "userapi_key_backup_versions_idx",
|
||||
"login_tokens_expiration_idx": "userapi_login_tokens_expiration_idx",
|
||||
"account_threepid_localpart": "userapi_threepid_idx",
|
||||
}
|
||||
|
||||
func UpRenameTables(ctx context.Context, tx *sql.Tx) error {
|
||||
for old, new := range renameTableMappings {
|
||||
// SQLite has no "IF EXISTS" so check if the table exists.
|
||||
var name string
|
||||
if err := tx.QueryRowContext(
|
||||
ctx, "SELECT name FROM sqlite_schema WHERE type = 'table' AND name = $1;", old,
|
||||
).Scan(&name); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
q := fmt.Sprintf(
|
||||
"ALTER TABLE %s RENAME TO %s;", old, new,
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, q); err != nil {
|
||||
return fmt.Errorf("rename table %q to %q error: %w", old, new, err)
|
||||
}
|
||||
}
|
||||
for old, new := range renameIndicesMappings {
|
||||
var query string
|
||||
if err := tx.QueryRowContext(
|
||||
ctx, "SELECT sql FROM sqlite_schema WHERE type = 'index' AND name = $1;", old,
|
||||
).Scan(&query); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
query = strings.Replace(query, old, new, 1)
|
||||
if _, err := tx.ExecContext(ctx, fmt.Sprintf("DROP INDEX %s;", old)); err != nil {
|
||||
return fmt.Errorf("drop index %q to %q error: %w", old, new, err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, query); err != nil {
|
||||
return fmt.Errorf("recreate index %q to %q error: %w", old, new, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DownRenameTables(ctx context.Context, tx *sql.Tx) error {
|
||||
for old, new := range renameTableMappings {
|
||||
// SQLite has no "IF EXISTS" so check if the table exists.
|
||||
var name string
|
||||
if err := tx.QueryRowContext(
|
||||
ctx, "SELECT name FROM sqlite_schema WHERE type = 'table' AND name = $1;", new,
|
||||
).Scan(&name); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
q := fmt.Sprintf(
|
||||
"ALTER TABLE %s RENAME TO %s;", new, old,
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, q); err != nil {
|
||||
return fmt.Errorf("rename table %q to %q error: %w", new, old, err)
|
||||
}
|
||||
}
|
||||
for old, new := range renameIndicesMappings {
|
||||
var query string
|
||||
if err := tx.QueryRowContext(
|
||||
ctx, "SELECT sql FROM sqlite_schema WHERE type = 'index' AND name = $1;", new,
|
||||
).Scan(&query); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
query = strings.Replace(query, new, old, 1)
|
||||
if _, err := tx.ExecContext(ctx, fmt.Sprintf("DROP INDEX %s;", new)); err != nil {
|
||||
return fmt.Errorf("drop index %q to %q error: %w", new, old, err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, query); err != nil {
|
||||
return fmt.Errorf("recreate index %q to %q error: %w", new, old, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue