1
0
Fork 0

Add scripts for database migration

This commit is contained in:
Cadence Ember 2023-09-29 17:41:11 +13:00
parent ae3a86bff2
commit c9bc6ac629
4 changed files with 40 additions and 2 deletions

View file

@ -1,4 +1,3 @@
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "sim" ( CREATE TABLE IF NOT EXISTS "sim" (
"discord_id" TEXT NOT NULL, "discord_id" TEXT NOT NULL,
"sim_name" TEXT NOT NULL UNIQUE, "sim_name" TEXT NOT NULL UNIQUE,
@ -6,18 +5,21 @@ CREATE TABLE IF NOT EXISTS "sim" (
"mxid" TEXT NOT NULL, "mxid" TEXT NOT NULL,
PRIMARY KEY("discord_id") PRIMARY KEY("discord_id")
); );
CREATE TABLE IF NOT EXISTS "webhook" ( CREATE TABLE IF NOT EXISTS "webhook" (
"channel_id" TEXT NOT NULL, "channel_id" TEXT NOT NULL,
"webhook_id" TEXT NOT NULL, "webhook_id" TEXT NOT NULL,
"webhook_token" TEXT NOT NULL, "webhook_token" TEXT NOT NULL,
PRIMARY KEY("channel_id") PRIMARY KEY("channel_id")
); );
CREATE TABLE IF NOT EXISTS "sim_member" ( CREATE TABLE IF NOT EXISTS "sim_member" (
"mxid" TEXT NOT NULL, "mxid" TEXT NOT NULL,
"room_id" TEXT NOT NULL, "room_id" TEXT NOT NULL,
"profile_event_content_hash" BLOB, "profile_event_content_hash" BLOB,
PRIMARY KEY("room_id","mxid") PRIMARY KEY("room_id","mxid")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "member_cache" ( CREATE TABLE IF NOT EXISTS "member_cache" (
"room_id" TEXT NOT NULL, "room_id" TEXT NOT NULL,
"mxid" TEXT NOT NULL, "mxid" TEXT NOT NULL,
@ -25,16 +27,19 @@ CREATE TABLE IF NOT EXISTS "member_cache" (
"avatar_url" TEXT, "avatar_url" TEXT,
PRIMARY KEY("room_id","mxid") PRIMARY KEY("room_id","mxid")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "file" ( CREATE TABLE IF NOT EXISTS "file" (
"discord_url" TEXT NOT NULL, "discord_url" TEXT NOT NULL,
"mxc_url" TEXT NOT NULL, "mxc_url" TEXT NOT NULL,
PRIMARY KEY("discord_url") PRIMARY KEY("discord_url")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "guild_space" ( CREATE TABLE IF NOT EXISTS "guild_space" (
"guild_id" TEXT NOT NULL, "guild_id" TEXT NOT NULL,
"space_id" TEXT NOT NULL, "space_id" TEXT NOT NULL,
PRIMARY KEY("guild_id") PRIMARY KEY("guild_id")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "channel_room" ( CREATE TABLE IF NOT EXISTS "channel_room" (
"channel_id" TEXT NOT NULL, "channel_id" TEXT NOT NULL,
"room_id" TEXT NOT NULL, "room_id" TEXT NOT NULL,
@ -44,11 +49,13 @@ CREATE TABLE IF NOT EXISTS "channel_room" (
"custom_avatar" TEXT, "custom_avatar" TEXT,
PRIMARY KEY("channel_id","room_id") PRIMARY KEY("channel_id","room_id")
); );
CREATE TABLE IF NOT EXISTS "message_channel" ( CREATE TABLE IF NOT EXISTS "message_channel" (
"message_id" TEXT NOT NULL, "message_id" TEXT NOT NULL,
"channel_id" TEXT NOT NULL, "channel_id" TEXT NOT NULL,
PRIMARY KEY("message_id") PRIMARY KEY("message_id")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "event_message" ( CREATE TABLE IF NOT EXISTS "event_message" (
"event_id" TEXT NOT NULL, "event_id" TEXT NOT NULL,
"message_id" TEXT NOT NULL, "message_id" TEXT NOT NULL,
@ -58,11 +65,13 @@ CREATE TABLE IF NOT EXISTS "event_message" (
"source" INTEGER NOT NULL, "source" INTEGER NOT NULL,
PRIMARY KEY("message_id","event_id") PRIMARY KEY("message_id","event_id")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "lottie" ( CREATE TABLE IF NOT EXISTS "lottie" (
"id" TEXT NOT NULL, "id" TEXT NOT NULL,
"mxc" TEXT NOT NULL, "mxc" TEXT NOT NULL,
PRIMARY KEY("id") PRIMARY KEY("id")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "emoji" ( CREATE TABLE IF NOT EXISTS "emoji" (
"id" TEXT NOT NULL, "id" TEXT NOT NULL,
"name" TEXT NOT NULL, "name" TEXT NOT NULL,
@ -70,10 +79,10 @@ CREATE TABLE IF NOT EXISTS "emoji" (
"mxc_url" TEXT NOT NULL, "mxc_url" TEXT NOT NULL,
PRIMARY KEY("id") PRIMARY KEY("id")
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "reaction" ( CREATE TABLE IF NOT EXISTS "reaction" (
"hashed_event_id" INTEGER NOT NULL, "hashed_event_id" INTEGER NOT NULL,
"message_id" TEXT NOT NULL, "message_id" TEXT NOT NULL,
"encoded_emoji" TEXT NOT NULL, "encoded_emoji" TEXT NOT NULL,
PRIMARY KEY ("hashed_event_id") PRIMARY KEY ("hashed_event_id")
) WITHOUT ROWID; ) WITHOUT ROWID;
COMMIT;

View file

@ -0,0 +1,13 @@
module.exports = async function(db) {
const hasher = await require("xxhash-wasm")()
const contents = db.prepare("SELECT distinct hashed_profile_content FROM sim_member").pluck().all()
const stmt = db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE hashed_profile_content = ?")
db.transaction(() => {
for (const s of contents) {
if (!Buffer.isBuffer(s)) s = Buffer.from(s)
const unsignedHash = hasher.h64(eventID)
const signedHash = unsignedHash - 0x8000000000000000n // shifting down to signed 64-bit range
stmt.run(s, signedHash)
}
})()
}

View file

@ -0,0 +1,16 @@
-- Change hashed_profile_content column affinity to INTEGER
CREATE TABLE "new_sim_member" (
"mxid" TEXT NOT NULL,
"room_id" TEXT NOT NULL,
"hashed_profile_content" INTEGER,
PRIMARY KEY("room_id","mxid")
) WITHOUT ROWID;
INSERT INTO new_sim_member SELECT * FROM sim_member;
DROP TABLE sim_member;
ALTER TABLE new_sim_member RENAME TO sim_member;
VACUUM;