From 5aa7a64354694283e3b3b5c5d3bc0c4e8dff94d9 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 15 Jul 2023 12:29:07 -0700 Subject: [PATCH] add triggers for follows table, lowercase username index --- migrations/20230426221940_init.up.sql | 7 +++---- migrations/20230427212229_update_triggers.up.sql | 12 ++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/migrations/20230426221940_init.up.sql b/migrations/20230426221940_init.up.sql index d8ecb6a..47f390e 100644 --- a/migrations/20230426221940_init.up.sql +++ b/migrations/20230426221940_init.up.sql @@ -41,8 +41,7 @@ create table if not exists watch_quests ( foreign key (watch) references watches (id) on delete cascade on update no action ); --- friend lists; this should really be a graph db, maybe the whole thing should be --- TODO: look into replacing sqlite with https://www.cozodb.org/ +-- friend lists; created by trigger at the same time as the user, so no created_at needed create table if not exists follows ( user blob not null primary key, follows blob, -- possibly empty friends list in some app-specific format @@ -51,18 +50,18 @@ create table if not exists follows ( ); create table if not exists watch_notes ( - id blob not null primary key, user blob not null, watch blob not null, note blob, public boolean not null, + created_at int not null default (unixepoch()), last_updated int not null default (unixepoch()), foreign key (user) references users (id) on delete cascade on update no action, foreign key (watch) references watches (id) on delete cascade on update no action ); -- indices, not needed for follows -create index if not exists user_username_dex on users (username); +create index if not exists user_username_dex on users (lower(username)); create index if not exists user_email_dex on users (lower(email)); create index if not exists watch_title_dex on watches (lower(title)); create index if not exists watch_added_by_dex on watches (added_by); diff --git a/migrations/20230427212229_update_triggers.up.sql b/migrations/20230427212229_update_triggers.up.sql index 011d8bf..40b37f7 100644 --- a/migrations/20230427212229_update_triggers.up.sql +++ b/migrations/20230427212229_update_triggers.up.sql @@ -5,6 +5,18 @@ BEGIN update users set last_updated = (select unixepoch()) where id=NEW.id; END; +create trigger if not exists insert_user_follows + after insert on users +BEGIN + insert into follows (user) values (NEW.id); +END; + +create trigger if not exists delete_user_follows + after delete on users +BEGIN + delete from follows where user = OLD.id; +END; + create trigger if not exists update_last_updated_watches after update on watches when OLD.last_updated = NEW.last_updated or OLD.last_updated is null