Room version 12 and room upgrades

This commit is contained in:
Cadence Ember
2026-01-07 02:43:20 +13:00
parent 092a4cf7b0
commit 55e0e5dfa1
27 changed files with 666 additions and 483 deletions

View File

@@ -0,0 +1,10 @@
BEGIN TRANSACTION;
CREATE TABLE room_upgrade_pending (
new_room_id TEXT NOT NULL,
old_room_id TEXT NOT NULL UNIQUE,
PRIMARY KEY (new_room_id),
FOREIGN KEY (old_room_id) REFERENCES channel_room (room_id) ON DELETE CASCADE
) WITHOUT ROWID;
COMMIT;

View File

@@ -0,0 +1,59 @@
/*
a. If the bridge bot sim already has the correct ID:
- No rows updated.
b. If the bridge bot sim has the wrong ID but there's no duplicate:
- One row updated.
c. If the bridge bot sim has the wrong ID and there's a duplicate:
- One row updated (replaces an existing row).
*/
const {discord} = require("../../passthrough")
const ones = "₀₁₂₃₄₅₆₇₈₉"
const tens = "0123456789"
module.exports = async function(db) {
/** @type {{name: string, channel_id: string, thread_parent: string | null}[]} */
const rows = db.prepare("SELECT name, channel_id, thread_parent FROM channel_room WHERE guild_id IS NULL").all()
/** @type {Map<string, string>} channel or thread ID -> guild ID */
const cache = new Map()
// Process channels
process.stdout.write(` loading metadata for ${rows.length} channels/threads... `)
for (let counter = 1; counter <= rows.length; counter++) {
process.stdout.write(String(counter).at(-1) === "0" ? tens[(counter/10)%10] : ones[counter%10])
const row = rows[counter-1]
const id = row.thread_parent || row.channel_id
if (cache.has(id)) continue
try {
var channel = await discord.snow.channel.getChannel(id)
} catch (e) {
continue
}
const guildID = channel.guild_id
const channels = await discord.snow.guild.getGuildChannels(guildID)
for (const channel of channels) {
cache.set(channel.id, guildID)
}
}
// Update channels and threads
process.stdout.write("\n")
db.transaction(() => {
// Fill in missing data
for (const row of rows) {
const guildID = cache.get(row.thread_parent) || cache.get(row.channel_id)
if (guildID) {
db.prepare("UPDATE channel_room SET guild_id = ? WHERE channel_id = ?").run(guildID, row.channel_id)
} else {
db.prepare("DELETE FROM webhook WHERE channel_id = ?").run(row.channel_id)
db.prepare("DELETE FROM channel_room WHERE channel_id = ?").run(row.channel_id)
}
}
})()
}

View File

@@ -0,0 +1,44 @@
-- https://sqlite.org/lang_altertable.html
-- 1
PRAGMA foreign_keys=OFF;
-- 2
BEGIN TRANSACTION;
-- 4
CREATE TABLE "new_channel_room" (
"channel_id" TEXT NOT NULL,
"room_id" TEXT NOT NULL UNIQUE,
"name" TEXT NOT NULL,
"nick" TEXT,
"thread_parent" TEXT,
"custom_avatar" TEXT,
"last_bridged_pin_timestamp" INTEGER,
"speedbump_id" TEXT,
"speedbump_checked" INTEGER,
"speedbump_webhook_id" TEXT,
"guild_id" TEXT NOT NULL,
"custom_topic" INTEGER DEFAULT 0,
PRIMARY KEY("channel_id"),
FOREIGN KEY("guild_id") REFERENCES "guild_active"("guild_id") ON DELETE CASCADE
) WITHOUT ROWID;
-- 5
INSERT INTO new_channel_room
(channel_id, room_id, name, nick, thread_parent, custom_avatar, last_bridged_pin_timestamp, speedbump_id, speedbump_checked, speedbump_webhook_id, guild_id, custom_topic)
SELECT channel_id, room_id, name, nick, thread_parent, custom_avatar, last_bridged_pin_timestamp, speedbump_id, speedbump_checked, speedbump_webhook_id, guild_id, custom_topic
FROM channel_room;
-- 6
DROP TABLE channel_room;
-- 7
ALTER TABLE new_channel_room RENAME TO channel_room;
-- 10
PRAGMA foreign_key_check;
-- 11
COMMIT;
-- 12
PRAGMA foreign_keys=ON;

View File

@@ -103,6 +103,11 @@ export type Models = {
historical_room_index: number
}
room_upgrade_pending: {
new_room_id: string
old_room_id: string
}
sim: {
user_id: string
username: string