mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-26 00:03:09 -06:00
Auto-upgrade device DB deltas
This commit is contained in:
parent
4b797f28be
commit
dd79f69974
|
|
@ -0,0 +1,34 @@
|
||||||
|
package deltas
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadLastSeenTSIP(m *sqlutil.Migrations) {
|
||||||
|
m.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpLastSeenTSIP(tx *sql.Tx) error {
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
ALTER TABLE device_devices ADD COLUMN IF NOT EXISTS last_seen_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)*1000;
|
||||||
|
ALTER TABLE device_devices ADD COLUMN IF NOT EXISTS ip TEXT;
|
||||||
|
ALTER TABLE device_devices ADD COLUMN IF NOT EXISTS user_agent TEXT;`)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DownLastSeenTSIP(tx *sql.Tx) error {
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
ALTER TABLE device_devices DROP COLUMN last_seen_ts;
|
||||||
|
ALTER TABLE device_devices DROP COLUMN ip;
|
||||||
|
ALTER TABLE device_devices DROP COLUMN user_agent;`)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute downgrade: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
-- +goose Up
|
|
||||||
-- +goose StatementBegin
|
|
||||||
ALTER TABLE device_devices ADD COLUMN IF NOT EXISTS last_seen_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)*1000;
|
|
||||||
ALTER TABLE device_devices ADD COLUMN IF NOT EXISTS ip TEXT;
|
|
||||||
ALTER TABLE device_devices ADD COLUMN IF NOT EXISTS user_agent TEXT;
|
|
||||||
-- +goose StatementEnd
|
|
||||||
|
|
||||||
-- +goose Down
|
|
||||||
-- +goose StatementBegin
|
|
||||||
ALTER TABLE device_devices DROP COLUMN last_seen_ts;
|
|
||||||
ALTER TABLE device_devices DROP COLUMN ip;
|
|
||||||
ALTER TABLE device_devices DROP COLUMN user_agent;
|
|
||||||
-- +goose StatementEnd
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/internal/config"
|
"github.com/matrix-org/dendrite/internal/config"
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
"github.com/matrix-org/dendrite/userapi/api"
|
"github.com/matrix-org/dendrite/userapi/api"
|
||||||
|
"github.com/matrix-org/dendrite/userapi/storage/devices/postgres/deltas"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -45,7 +46,10 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
|
||||||
if err = d.prepare(db, serverName); err != nil {
|
if err = d.prepare(db, serverName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Database{db, d}, nil
|
m := sqlutil.NewMigrations()
|
||||||
|
deltas.LoadLastSeenTSIP(m)
|
||||||
|
|
||||||
|
return &Database{db, d}, m.RunDeltas(db, dbProperties)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDeviceByAccessToken returns the device matching the given access token.
|
// GetDeviceByAccessToken returns the device matching the given access token.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package deltas
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadLastSeenTSIP(m *sqlutil.Migrations) {
|
||||||
|
m.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpLastSeenTSIP(tx *sql.Tx) error {
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
ALTER TABLE device_devices RENAME TO device_devices_tmp;
|
||||||
|
CREATE TABLE device_devices (
|
||||||
|
access_token TEXT PRIMARY KEY,
|
||||||
|
session_id INTEGER,
|
||||||
|
device_id TEXT ,
|
||||||
|
localpart TEXT ,
|
||||||
|
created_ts BIGINT,
|
||||||
|
display_name TEXT,
|
||||||
|
last_seen_ts BIGINT,
|
||||||
|
ip TEXT,
|
||||||
|
user_agent TEXT,
|
||||||
|
UNIQUE (localpart, device_id)
|
||||||
|
);
|
||||||
|
INSERT
|
||||||
|
INTO device_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;`)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute upgrade: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DownLastSeenTSIP(tx *sql.Tx) error {
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
ALTER TABLE device_devices RENAME TO device_devices_tmp;
|
||||||
|
CREATE TABLE IF NOT EXISTS device_devices (
|
||||||
|
access_token TEXT PRIMARY KEY,
|
||||||
|
session_id INTEGER,
|
||||||
|
device_id TEXT ,
|
||||||
|
localpart TEXT ,
|
||||||
|
created_ts BIGINT,
|
||||||
|
display_name TEXT,
|
||||||
|
UNIQUE (localpart, device_id)
|
||||||
|
);
|
||||||
|
INSERT
|
||||||
|
INTO device_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;`)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to execute downgrade: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
-- +goose Up
|
|
||||||
-- +goose StatementBegin
|
|
||||||
ALTER TABLE device_devices RENAME TO device_devices_tmp;
|
|
||||||
CREATE TABLE device_devices (
|
|
||||||
access_token TEXT PRIMARY KEY,
|
|
||||||
session_id INTEGER,
|
|
||||||
device_id TEXT ,
|
|
||||||
localpart TEXT ,
|
|
||||||
created_ts BIGINT,
|
|
||||||
display_name TEXT,
|
|
||||||
last_seen_ts BIGINT,
|
|
||||||
ip TEXT,
|
|
||||||
user_agent TEXT,
|
|
||||||
UNIQUE (localpart, device_id)
|
|
||||||
);
|
|
||||||
INSERT
|
|
||||||
INTO device_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;
|
|
||||||
-- +goose StatementEnd
|
|
||||||
|
|
||||||
-- +goose Down
|
|
||||||
-- +goose StatementBegin
|
|
||||||
ALTER TABLE device_devices RENAME TO device_devices_tmp;
|
|
||||||
CREATE TABLE IF NOT EXISTS device_devices (
|
|
||||||
access_token TEXT PRIMARY KEY,
|
|
||||||
session_id INTEGER,
|
|
||||||
device_id TEXT ,
|
|
||||||
localpart TEXT ,
|
|
||||||
created_ts BIGINT,
|
|
||||||
display_name TEXT,
|
|
||||||
UNIQUE (localpart, device_id)
|
|
||||||
);
|
|
||||||
INSERT
|
|
||||||
INTO device_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;
|
|
||||||
-- +goose StatementEnd
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/internal/config"
|
"github.com/matrix-org/dendrite/internal/config"
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
"github.com/matrix-org/dendrite/userapi/api"
|
"github.com/matrix-org/dendrite/userapi/api"
|
||||||
|
"github.com/matrix-org/dendrite/userapi/storage/devices/sqlite3/deltas"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
@ -49,7 +50,9 @@ func NewDatabase(dbProperties *config.DatabaseOptions, serverName gomatrixserver
|
||||||
if err = d.prepare(db, writer, serverName); err != nil {
|
if err = d.prepare(db, writer, serverName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Database{db, writer, d}, nil
|
m := sqlutil.NewMigrations()
|
||||||
|
deltas.LoadLastSeenTSIP(m)
|
||||||
|
return &Database{db, writer, d}, m.RunDeltas(db, dbProperties)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDeviceByAccessToken returns the device matching the given access token.
|
// GetDeviceByAccessToken returns the device matching the given access token.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue