PostgreSQL sequences and indexes

This commit is contained in:
Neil Alexander 2022-10-17 13:21:30 +01:00
parent 4c52681a49
commit 1f077a0257
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
6 changed files with 44 additions and 12 deletions

View file

@ -18,11 +18,33 @@ var renameTableMappings = map[string]string{
"account_threepid": "userapi_threepids", "account_threepid": "userapi_threepids",
} }
var renameSequenceMappings = map[string]string{} var renameSequenceMappings = map[string]string{
"device_session_id_seq": "userapi_device_session_id_seq",
"account_e2e_room_keys_versions_seq": "userapi_key_backup_versions_seq",
}
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 { func UpRenameTables(ctx context.Context, tx *sql.Tx) error {
for old, new := range renameTableMappings { for old, new := range renameTableMappings {
if _, err := tx.ExecContext(ctx, "ALTER TABLE $1 RENAME TO $2;", old, new); err != nil { if _, err := tx.ExecContext(ctx, "ALTER TABLE IF EXISTS $1 RENAME TO $2;", old, new); err != nil {
return fmt.Errorf("rename %q to %q error: %w", old, new, err)
}
}
for old, new := range renameSequenceMappings {
if _, err := tx.ExecContext(ctx, "ALTER SEQUENCE IF EXISTS $1 RENAME TO $2;", old, new); err != nil {
return fmt.Errorf("rename %q to %q error: %w", old, new, err)
}
}
for old, new := range renameIndicesMappings {
if _, err := tx.ExecContext(ctx, "ALTER INDEX IF EXISTS $1 RENAME TO $2;", old, new); err != nil {
return fmt.Errorf("rename %q to %q error: %w", old, new, err) return fmt.Errorf("rename %q to %q error: %w", old, new, err)
} }
} }
@ -31,7 +53,17 @@ func UpRenameTables(ctx context.Context, tx *sql.Tx) error {
func DownRenameTables(ctx context.Context, tx *sql.Tx) error { func DownRenameTables(ctx context.Context, tx *sql.Tx) error {
for old, new := range renameTableMappings { for old, new := range renameTableMappings {
if _, err := tx.ExecContext(ctx, "ALTER TABLE $1 RENAME TO $2;", new, old); err != nil { if _, err := tx.ExecContext(ctx, "ALTER TABLE IF EXISTS $1 RENAME TO $2;", new, old); err != nil {
return fmt.Errorf("rename %q to %q error: %w", new, old, err)
}
}
for old, new := range renameSequenceMappings {
if _, err := tx.ExecContext(ctx, "ALTER SEQUENCE IF EXISTS $1 RENAME TO $2;", new, old); err != nil {
return fmt.Errorf("rename %q to %q error: %w", new, old, err)
}
}
for old, new := range renameIndicesMappings {
if _, err := tx.ExecContext(ctx, "ALTER INDEX IF EXISTS $1 RENAME TO $2;", new, old); err != nil {
return fmt.Errorf("rename %q to %q error: %w", new, old, err) return fmt.Errorf("rename %q to %q error: %w", new, old, err)
} }
} }

View file

@ -31,7 +31,7 @@ import (
const devicesSchema = ` const devicesSchema = `
-- This sequence is used for automatic allocation of session_id. -- This sequence is used for automatic allocation of session_id.
CREATE SEQUENCE IF NOT EXISTS device_session_id_seq START 1; CREATE SEQUENCE IF NOT EXISTS userapi_device_session_id_seq START 1;
-- Stores data about devices. -- Stores data about devices.
CREATE TABLE IF NOT EXISTS userapi_devices ( CREATE TABLE IF NOT EXISTS userapi_devices (
@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS userapi_devices (
-- This can be used as a secure substitution of the access token in situations -- This can be used as a secure substitution of the access token in situations
-- where data is associated with access tokens (e.g. transaction storage), -- where data is associated with access tokens (e.g. transaction storage),
-- so we don't have to store users' access tokens everywhere. -- so we don't have to store users' access tokens everywhere.
session_id BIGINT NOT NULL DEFAULT nextval('device_session_id_seq'), session_id BIGINT NOT NULL DEFAULT nextval('userapi_device_session_id_seq'),
-- The device identifier. This only needs to uniquely identify a device for a given user, not globally. -- The device identifier. This only needs to uniquely identify a device for a given user, not globally.
-- access_tokens will be clobbered based on the device ID for a user. -- access_tokens will be clobbered based on the device ID for a user.
device_id TEXT NOT NULL, device_id TEXT NOT NULL,
@ -65,7 +65,7 @@ CREATE TABLE IF NOT EXISTS userapi_devices (
); );
-- Device IDs must be unique for a given user. -- Device IDs must be unique for a given user.
CREATE UNIQUE INDEX IF NOT EXISTS device_localpart_id_idx ON userapi_devices(localpart, device_id); CREATE UNIQUE INDEX IF NOT EXISTS userapi_device_localpart_id_idx ON userapi_devices(localpart, device_id);
` `
const insertDeviceSQL = "" + const insertDeviceSQL = "" +

View file

@ -37,8 +37,8 @@ CREATE TABLE IF NOT EXISTS userapi_key_backups (
is_verified BOOLEAN NOT NULL, is_verified BOOLEAN NOT NULL,
session_data TEXT NOT NULL session_data TEXT NOT NULL
); );
CREATE UNIQUE INDEX IF NOT EXISTS e2e_room_keys_idx ON userapi_key_backups(user_id, room_id, session_id, version); CREATE UNIQUE INDEX IF NOT EXISTS userapi_key_backups_idx ON userapi_key_backups(user_id, room_id, session_id, version);
CREATE INDEX IF NOT EXISTS e2e_room_keys_versions_idx ON userapi_key_backups(user_id, version); CREATE INDEX IF NOT EXISTS userapi_key_backups_versions_idx ON userapi_key_backups(user_id, version);
` `
const insertBackupKeySQL = "" + const insertBackupKeySQL = "" +

View file

@ -26,14 +26,14 @@ import (
) )
const keyBackupVersionTableSchema = ` const keyBackupVersionTableSchema = `
CREATE SEQUENCE IF NOT EXISTS account_e2e_room_keys_versions_seq; CREATE SEQUENCE IF NOT EXISTS userapi_key_backup_versions_seq;
-- the metadata for each generation of encrypted e2e session backups -- the metadata for each generation of encrypted e2e session backups
CREATE TABLE IF NOT EXISTS userapi_key_backup_versions ( CREATE TABLE IF NOT EXISTS userapi_key_backup_versions (
user_id TEXT NOT NULL, user_id TEXT NOT NULL,
-- this means no 2 users will ever have the same version of e2e session backups which strictly -- this means no 2 users will ever have the same version of e2e session backups which strictly
-- isn't necessary, but this is easy to do rather than SELECT MAX(version)+1. -- isn't necessary, but this is easy to do rather than SELECT MAX(version)+1.
version BIGINT DEFAULT nextval('account_e2e_room_keys_versions_seq'), version BIGINT DEFAULT nextval('userapi_key_backup_versions_seq'),
algorithm TEXT NOT NULL, algorithm TEXT NOT NULL,
auth_data TEXT NOT NULL, auth_data TEXT NOT NULL,
etag TEXT NOT NULL, etag TEXT NOT NULL,

View file

@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS userapi_login_tokens (
); );
-- This index allows efficient garbage collection of expired tokens. -- This index allows efficient garbage collection of expired tokens.
CREATE INDEX IF NOT EXISTS login_tokens_expiration_idx ON userapi_login_tokens(token_expires_at); CREATE INDEX IF NOT EXISTS userapi_login_tokens_expiration_idx ON userapi_login_tokens(token_expires_at);
` `
const insertLoginTokenSQL = "" + const insertLoginTokenSQL = "" +

View file

@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS userapi_threepids (
PRIMARY KEY(threepid, medium) PRIMARY KEY(threepid, medium)
); );
CREATE INDEX IF NOT EXISTS account_threepid_localpart ON userapi_threepids(localpart); CREATE INDEX IF NOT EXISTS userapi_threepid_idx ON userapi_threepids(localpart);
` `
const selectLocalpartForThreePIDSQL = "" + const selectLocalpartForThreePIDSQL = "" +