| 1 | -- News for a user: somebody did something to one of their posts. |
| 2 | create table notifications ( |
| 3 | id bigint generated always as identity primary key, |
| 4 | user_id bigint not null references users (id), |
| 5 | kind text not null, |
| 6 | actor_id bigint not null references users (id), |
| 7 | post_id bigint not null references posts (id), |
| 8 | created_at timestamptz not null default now(), |
| 9 | constraint notifications_once unique (post_id, actor_id, kind) |
| 10 | ); |
| 11 | |
| 12 | create index notifications_user_id_id_idx on notifications (user_id, id desc); |
| 13 | |
| 14 | -- Every notification up to this id has been seen by its user. |
| 15 | alter table users add column seen_notification bigint not null default 0; |