Room version 12 and room upgrades
This commit is contained in:
10
src/db/migrations/0028-add-room-upgrade.sql
Normal file
10
src/db/migrations/0028-add-room-upgrade.sql
Normal 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;
|
||||
59
src/db/migrations/0029-force-guild-ids.js
Normal file
59
src/db/migrations/0029-force-guild-ids.js
Normal 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)
|
||||
}
|
||||
}
|
||||
})()
|
||||
}
|
||||
44
src/db/migrations/0030-require-guild-id.sql
Normal file
44
src/db/migrations/0030-require-guild-id.sql
Normal 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;
|
||||
5
src/db/orm-defs.d.ts
vendored
5
src/db/orm-defs.d.ts
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user