summaryrefslogtreecommitdiff
path: root/migrations/20241017005219_retain_deleted.sql
diff options
context:
space:
mode:
Diffstat (limited to 'migrations/20241017005219_retain_deleted.sql')
-rw-r--r--migrations/20241017005219_retain_deleted.sql92
1 files changed, 92 insertions, 0 deletions
diff --git a/migrations/20241017005219_retain_deleted.sql b/migrations/20241017005219_retain_deleted.sql
new file mode 100644
index 0000000..6205482
--- /dev/null
+++ b/migrations/20241017005219_retain_deleted.sql
@@ -0,0 +1,92 @@
+alter table channel
+rename to old_channel;
+
+alter table message
+rename to old_message;
+
+create table channel (
+ id text
+ not null
+ primary key,
+ name text
+ not null,
+ created_sequence bigint
+ unique
+ not null,
+ created_at text
+ not null,
+ unique (id, name)
+);
+
+insert into channel (id, name, created_sequence, created_at)
+select id, name, created_sequence, created_at from old_channel;
+
+create table channel_name_reservation (
+ id text
+ not null
+ unique,
+ name text
+ not null
+ unique,
+ primary key (id, name),
+ foreign key (id, name)
+ references channel (id, name)
+);
+
+insert into channel_name_reservation (id, name)
+select id, name from old_channel;
+
+create table channel_deleted (
+ id text
+ not null
+ primary key
+ references channel (id),
+ deleted_sequence bigint
+ unique
+ not null,
+ deleted_at text
+ not null
+);
+
+create table message (
+ id text
+ not null
+ primary key,
+ channel text
+ not null
+ references channel (id),
+ sender text
+ not null
+ references login (id),
+ sent_sequence bigint
+ unique
+ not null,
+ sent_at text
+ not null,
+ body text
+ not null
+);
+
+insert into message (id, channel, sender, sent_sequence, sent_at, body)
+select id, channel, sender, sent_sequence, sent_at, body from old_message;
+
+create table message_deleted (
+ id text
+ not null
+ primary key
+ references message (id),
+ deleted_sequence bigint
+ unique
+ not null,
+ deleted_at text
+ not null
+);
+
+drop table old_message;
+drop table old_channel;
+
+create index message_sent_at
+on message (sent_at);
+
+create index channel_created_at
+on channel (created_at);