summaryrefslogtreecommitdiff
path: root/migrations/20240928012031_channel_stored_sequence.sql
diff options
context:
space:
mode:
authorKit La Touche <kit@transneptune.net>2024-09-28 21:55:50 -0400
committerKit La Touche <kit@transneptune.net>2024-09-28 21:55:50 -0400
commit897eef0306917baf3662e691b29f182d35805296 (patch)
tree024e2a3fa13ac96e0b4339a6d62ae533efe7db07 /migrations/20240928012031_channel_stored_sequence.sql
parentc524b333befc8cc97aa49f73b3ed28bc3b82420c (diff)
parent4d0bb0709b168a24ab6a8dbc86da45d7503596ee (diff)
Merge branch 'main' into feature-frontend
Diffstat (limited to 'migrations/20240928012031_channel_stored_sequence.sql')
-rw-r--r--migrations/20240928012031_channel_stored_sequence.sql60
1 files changed, 60 insertions, 0 deletions
diff --git a/migrations/20240928012031_channel_stored_sequence.sql b/migrations/20240928012031_channel_stored_sequence.sql
new file mode 100644
index 0000000..badd88d
--- /dev/null
+++ b/migrations/20240928012031_channel_stored_sequence.sql
@@ -0,0 +1,60 @@
+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,
+ last_sequence bigint
+ not null
+);
+
+-- Transfer data from original table
+insert into channel
+select
+ channel.id,
+ channel.name,
+ channel.created_at,
+ coalesce(
+ max(message.sequence),
+ 0
+ ) as last_sequence
+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;