mirror of
https://github.com/element-hq/dendrite.git
synced 2025-09-14 13:22:26 +03:00
drop primary key constraint from userapi_devices.access_token
This commit is contained in:
parent
ff63e7fa98
commit
bf310d558f
5 changed files with 111 additions and 12 deletions
|
@ -0,0 +1,65 @@
|
|||
package deltas
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func UpDropPrimaryKeyConstraint(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
ALTER TABLE userapi_devices RENAME TO userapi_devices_tmp;
|
||||
CREATE TABLE userapi_devices (
|
||||
access_token TEXT,
|
||||
session_id INTEGER,
|
||||
device_id TEXT ,
|
||||
localpart TEXT ,
|
||||
server_name TEXT NOT NULL,
|
||||
created_ts BIGINT,
|
||||
display_name TEXT,
|
||||
last_seen_ts BIGINT,
|
||||
ip TEXT,
|
||||
user_agent TEXT,
|
||||
UNIQUE (localpart, device_id)
|
||||
);
|
||||
INSERT
|
||||
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 userapi_devices_tmp;
|
||||
DROP TABLE userapi_devices_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DownDropPrimaryKeyConstraint(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
ALTER TABLE userapi_devices RENAME TO userapi_devices_tmp;
|
||||
CREATE TABLE userapi_devices (
|
||||
access_token TEXT PRIMARY KEY,
|
||||
session_id INTEGER,
|
||||
device_id TEXT ,
|
||||
localpart TEXT ,
|
||||
server_name TEXT NOT NULL,
|
||||
created_ts BIGINT,
|
||||
display_name TEXT,
|
||||
last_seen_ts BIGINT,
|
||||
ip TEXT,
|
||||
user_agent TEXT,
|
||||
UNIQUE (localpart, device_id)
|
||||
);
|
||||
INSERT
|
||||
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 userapi_devices_tmp;
|
||||
DROP TABLE userapi_devices_tmp;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute downgrade: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -102,10 +102,16 @@ func NewSQLiteDevicesTable(db *sql.DB, serverName spec.ServerName) (tables.Devic
|
|||
return nil, err
|
||||
}
|
||||
m := sqlutil.NewMigrator(db)
|
||||
m.AddMigrations(sqlutil.Migration{
|
||||
Version: "userapi: add last_seen_ts",
|
||||
Up: deltas.UpLastSeenTSIP,
|
||||
})
|
||||
m.AddMigrations(
|
||||
sqlutil.Migration{
|
||||
Version: "userapi: add last_seen_ts",
|
||||
Up: deltas.UpLastSeenTSIP,
|
||||
},
|
||||
sqlutil.Migration{
|
||||
Version: "userapi: drop primary key constraint",
|
||||
Up: deltas.UpDropPrimaryKeyConstraint,
|
||||
},
|
||||
)
|
||||
if err = m.Up(context.Background()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue