···
CREATE TABLE IF NOT EXISTS channel_mappings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slack_channel_id TEXT NOT NULL UNIQUE,
9
-
irc_channel TEXT NOT NULL,
9
+
irc_channel TEXT NOT NULL UNIQUE,
created_at INTEGER DEFAULT (strftime('%s', 'now'))
···
CREATE TABLE IF NOT EXISTS user_mappings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slack_user_id TEXT NOT NULL UNIQUE,
18
-
irc_nick TEXT NOT NULL,
18
+
irc_nick TEXT NOT NULL UNIQUE,
created_at INTEGER DEFAULT (strftime('%s', 'now'))
23
+
// Migration: Add unique constraints if they don't exist
24
+
// SQLite doesn't support ALTER TABLE to add constraints, so we need to recreate the table
25
+
function migrateSchema() {
26
+
// Check if irc_channel has unique constraint by examining table schema
27
+
const channelSchema = db
28
+
.query("SELECT sql FROM sqlite_master WHERE type='table' AND name='channel_mappings'")
29
+
.get() as { sql: string } | null;
31
+
const hasIrcChannelUnique = channelSchema?.sql?.includes("irc_channel TEXT NOT NULL UNIQUE") ?? false;
33
+
if (!hasIrcChannelUnique && channelSchema) {
34
+
// Check if table has any data with duplicate irc_channel values
35
+
const duplicates = db.query(
36
+
"SELECT irc_channel, COUNT(*) as count FROM channel_mappings GROUP BY irc_channel HAVING count > 1",
39
+
if (duplicates.length > 0) {
41
+
"Warning: Found duplicate IRC channel mappings. Keeping only the most recent mapping for each IRC channel.",
43
+
for (const dup of duplicates as { irc_channel: string }[]) {
44
+
// Delete all but the most recent mapping for this IRC channel
46
+
`DELETE FROM channel_mappings
47
+
WHERE irc_channel = ?
49
+
SELECT id FROM channel_mappings
50
+
WHERE irc_channel = ?
51
+
ORDER BY created_at DESC
54
+
[dup.irc_channel, dup.irc_channel],
59
+
// Recreate the table with unique constraint
61
+
CREATE TABLE channel_mappings_new (
62
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
63
+
slack_channel_id TEXT NOT NULL UNIQUE,
64
+
irc_channel TEXT NOT NULL UNIQUE,
65
+
created_at INTEGER DEFAULT (strftime('%s', 'now'))
70
+
"INSERT INTO channel_mappings_new SELECT * FROM channel_mappings",
72
+
db.run("DROP TABLE channel_mappings");
73
+
db.run("ALTER TABLE channel_mappings_new RENAME TO channel_mappings");
74
+
console.log("Migrated channel_mappings table to add unique constraint on irc_channel");
77
+
// Check if irc_nick has unique constraint by examining table schema
78
+
const userSchema = db
79
+
.query("SELECT sql FROM sqlite_master WHERE type='table' AND name='user_mappings'")
80
+
.get() as { sql: string } | null;
82
+
const hasIrcNickUnique = userSchema?.sql?.includes("irc_nick TEXT NOT NULL UNIQUE") ?? false;
84
+
if (!hasIrcNickUnique && userSchema) {
85
+
// Check if table has any data with duplicate irc_nick values
86
+
const duplicates = db.query(
87
+
"SELECT irc_nick, COUNT(*) as count FROM user_mappings GROUP BY irc_nick HAVING count > 1",
90
+
if (duplicates.length > 0) {
92
+
"Warning: Found duplicate IRC nick mappings. Keeping only the most recent mapping for each IRC nick.",
94
+
for (const dup of duplicates as { irc_nick: string }[]) {
95
+
// Delete all but the most recent mapping for this IRC nick
97
+
`DELETE FROM user_mappings
100
+
SELECT id FROM user_mappings
102
+
ORDER BY created_at DESC
105
+
[dup.irc_nick, dup.irc_nick],
110
+
// Recreate the table with unique constraint
112
+
CREATE TABLE user_mappings_new (
113
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
114
+
slack_user_id TEXT NOT NULL UNIQUE,
115
+
irc_nick TEXT NOT NULL UNIQUE,
116
+
created_at INTEGER DEFAULT (strftime('%s', 'now'))
120
+
db.run("INSERT INTO user_mappings_new SELECT * FROM user_mappings");
121
+
db.run("DROP TABLE user_mappings");
122
+
db.run("ALTER TABLE user_mappings_new RENAME TO user_mappings");
123
+
console.log("Migrated user_mappings table to add unique constraint on irc_nick");
CREATE TABLE IF NOT EXISTS thread_timestamps (