summaryrefslogtreecommitdiff
path: root/migrations
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-09-27 20:18:50 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-09-28 01:00:09 -0400
commit1458ff7be5d883444943090cb636e9343487d03e (patch)
treeadb410ce9245dc2a1562fc370f2398cab35e7507 /migrations
parenteff129bc1f29bcb1b2b9d10c6b49ab886edc83d6 (diff)
Send created events when channels are added.
Diffstat (limited to 'migrations')
-rw-r--r--migrations/20240928002608_channel_lifecycle.sql57
1 files changed, 57 insertions, 0 deletions
diff --git a/migrations/20240928002608_channel_lifecycle.sql b/migrations/20240928002608_channel_lifecycle.sql
new file mode 100644
index 0000000..bc690d7
--- /dev/null
+++ b/migrations/20240928002608_channel_lifecycle.sql
@@ -0,0 +1,57 @@
+alter table channel
+rename to old_channel;
+
+-- Add new columns
+create table channel (
+ id text
+ not null
+ primary key,
+ name text
+ not null
+ unique,
+ created_at text
+ not null
+);
+
+-- Transfer data from original table
+insert into channel
+select
+ channel.id,
+ channel.name,
+ coalesce(
+ min(message.sent_at),
+ strftime('%FT%R:%f+00:00', 'now', 'utc')
+ ) as created_at
+from old_channel as channel
+ left join message on channel.id = message.channel
+group by channel.id, channel.name;
+
+-- Fix up `message` foreign keys
+alter table message
+rename to old_message;
+
+create table message (
+ id text
+ not null
+ primary key,
+ sequence bigint
+ not null,
+ channel text
+ not null
+ references channel (id),
+ sender text
+ not null
+ references login (id),
+ body text
+ not null,
+ sent_at text
+ not null,
+ unique (channel, sequence)
+);
+
+insert into message
+select * from old_message;
+
+-- Bury the bodies respectfully
+drop table old_message;
+drop table old_channel;