Drop primary keys in favour of new indices
This commit is contained in:
parent
99ed784360
commit
f980ff267d
|
@ -36,10 +36,10 @@ CREATE TABLE IF NOT EXISTS userapi_account_datas (
|
|||
-- The account data type
|
||||
type TEXT NOT NULL,
|
||||
-- The account data content
|
||||
content TEXT NOT NULL,
|
||||
|
||||
PRIMARY KEY(localpart, server_name, room_id, type)
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS userapi_account_datas_idx ON userapi_account_datas(localpart, server_name, room_id, type);
|
||||
`
|
||||
|
||||
const insertAccountDataSQL = `
|
||||
|
|
|
@ -46,11 +46,12 @@ CREATE TABLE IF NOT EXISTS userapi_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 NOT NULL,
|
||||
account_type SMALLINT NOT NULL
|
||||
-- TODO:
|
||||
-- upgraded_ts, devices, any email reset stuff?
|
||||
PRIMARY KEY (localpart, server_name)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS userapi_accounts_idx ON userapi_accounts(localpart, server_name);
|
||||
`
|
||||
|
||||
const insertAccountSQL = "" +
|
||||
|
|
|
@ -20,6 +20,14 @@ var serverNamesTables = []string{
|
|||
"userapi_threepids",
|
||||
}
|
||||
|
||||
// These tables have a PRIMARY KEY constraint which we need to drop so
|
||||
// that we can recreate a new unique index that contains the server name.
|
||||
var serverNamesDropPK = []string{
|
||||
"userapi_accounts",
|
||||
"userapi_account_datas",
|
||||
"userapi_profiles",
|
||||
}
|
||||
|
||||
// I know what you're thinking: you're wondering "why doesn't this use $1
|
||||
// and pass variadic parameters to ExecContext?" — the answer is because
|
||||
// PostgreSQL doesn't expect the table name to be specified as a substituted
|
||||
|
@ -35,5 +43,14 @@ func UpServerNames(ctx context.Context, tx *sql.Tx, serverName gomatrixserverlib
|
|||
return fmt.Errorf("add server name to %q error: %w", table, err)
|
||||
}
|
||||
}
|
||||
for _, table := range serverNamesDropPK {
|
||||
q := fmt.Sprintf(
|
||||
"ALTER TABLE IF EXISTS %s DROP CONSTRAINT %s;",
|
||||
pq.QuoteIdentifier(table), pq.QuoteIdentifier(table+"_pkey"),
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, q); err != nil {
|
||||
return fmt.Errorf("drop PK from %q error: %w", table, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -35,9 +35,10 @@ CREATE TABLE IF NOT EXISTS userapi_profiles (
|
|||
-- The display name for this account
|
||||
display_name TEXT,
|
||||
-- The URL of the avatar for this account
|
||||
avatar_url TEXT,
|
||||
PRIMARY KEY (localpart, server_name)
|
||||
avatar_url TEXT
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS userapi_profiles_idx ON userapi_profiles(localpart, server_name);
|
||||
`
|
||||
|
||||
const insertProfileSQL = "" +
|
||||
|
|
|
@ -35,10 +35,10 @@ CREATE TABLE IF NOT EXISTS userapi_account_datas (
|
|||
-- The account data type
|
||||
type TEXT NOT NULL,
|
||||
-- The account data content
|
||||
content TEXT NOT NULL,
|
||||
|
||||
PRIMARY KEY(localpart, server_name, room_id, type)
|
||||
content TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS userapi_account_datas_idx ON userapi_account_datas(localpart, server_name, room_id, type);
|
||||
`
|
||||
|
||||
const insertAccountDataSQL = `
|
||||
|
|
|
@ -45,11 +45,12 @@ CREATE TABLE IF NOT EXISTS userapi_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 NOT NULL,
|
||||
account_type INTEGER NOT NULL
|
||||
-- TODO:
|
||||
-- upgraded_ts, devices, any email reset stuff?
|
||||
PRIMARY KEY (localpart, server_name)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS userapi_accounts_idx ON userapi_accounts(localpart, server_name);
|
||||
`
|
||||
|
||||
const insertAccountSQL = "" +
|
||||
|
|
|
@ -20,6 +20,14 @@ var serverNamesTables = []string{
|
|||
"userapi_threepids",
|
||||
}
|
||||
|
||||
// These tables have a PRIMARY KEY constraint which we need to drop so
|
||||
// that we can recreate a new unique index that contains the server name.
|
||||
var serverNamesDropPK = []string{
|
||||
"userapi_accounts",
|
||||
"userapi_account_datas",
|
||||
"userapi_profiles",
|
||||
}
|
||||
|
||||
// I know what you're thinking: you're wondering "why doesn't this use $1
|
||||
// and pass variadic parameters to ExecContext?" — the answer is because
|
||||
// PostgreSQL doesn't expect the table name to be specified as a substituted
|
||||
|
@ -43,5 +51,22 @@ func UpServerNames(ctx context.Context, tx *sql.Tx, serverName gomatrixserverlib
|
|||
return fmt.Errorf("add server name to %q error: %w", table, err)
|
||||
}
|
||||
}
|
||||
for _, table := range serverNamesDropPK {
|
||||
q := fmt.Sprintf(
|
||||
"SELECT COUNT(name) FROM sqlite_schema WHERE type='table' AND name=%s;",
|
||||
pq.QuoteIdentifier(table),
|
||||
)
|
||||
var c int
|
||||
if err := tx.QueryRowContext(ctx, q).Scan(&c); err != nil || c == 0 {
|
||||
continue
|
||||
}
|
||||
q = fmt.Sprintf(
|
||||
"ALTER TABLE %s DROP PRIMARY KEY;",
|
||||
pq.QuoteIdentifier(table),
|
||||
)
|
||||
if _, err := tx.ExecContext(ctx, q); err != nil {
|
||||
return fmt.Errorf("drop PK from %q error: %w", table, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -35,9 +35,10 @@ CREATE TABLE IF NOT EXISTS userapi_profiles (
|
|||
-- The display name for this account
|
||||
display_name TEXT,
|
||||
-- The URL of the avatar for this account
|
||||
avatar_url TEXT,
|
||||
PRIMARY KEY (localpart, server_name)
|
||||
avatar_url TEXT
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS userapi_profiles_idx ON userapi_profiles(localpart, server_name);
|
||||
`
|
||||
|
||||
const insertProfileSQL = "" +
|
||||
|
|
Loading…
Reference in a new issue