summaryrefslogtreecommitdiff
path: root/migrations/20240924232919_message_serial_numbers.sql
blob: a53e4a25499894f5294c3a9ec8dba37f0c661d25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
create table sequenced_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 sequenced_message
select
	id,
	rank() over (
		partition by channel
		order by sent_at
	) as sequence,
	channel,
	sender,
	body,
	sent_at
from message;

drop table message;

alter table sequenced_message
rename to message;

create index message_sent_at on message (channel, sent_at);