From 3aa83dcbc30c3d902008ae42e9903714469cc4f3 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 27 Apr 2023 15:22:49 -0700 Subject: [PATCH] add update triggers and default values for dates --- migrations/20230426221940_init.up.sql | 20 +++++------ .../20230427212229_update_triggers.down.sql | 6 ++++ .../20230427212229_update_triggers.up.sql | 34 +++++++++++++++++++ 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 migrations/20230427212229_update_triggers.down.sql create mode 100644 migrations/20230427212229_update_triggers.up.sql diff --git a/migrations/20230426221940_init.up.sql b/migrations/20230426221940_init.up.sql index 7a9c10a..acc16ec 100644 --- a/migrations/20230426221940_init.up.sql +++ b/migrations/20230426221940_init.up.sql @@ -10,8 +10,8 @@ create table if not exists witches ( name text, email text, secret blob not null, -- encrypted password? need to figure auth out - created_at int, - last_updated int + created_at int not null default (unixepoch()), + last_updated int not null default (unixepoch()) ); -- table of things to watch @@ -22,8 +22,8 @@ create table if not exists watches ( imdb text, -- possible url for imdb or other metadata-esque site to show the user runtime int, release_date int, - created_at int, - last_updated int + created_at int not null default (unixepoch()), + last_updated int not null default (unixepoch()) ); -- table of what people want to watch @@ -37,8 +37,8 @@ create table if not exists witch_watch ( watched boolean not null, when_added int, when_watched int, - created_at int, - last_updated int, + created_at int not null default (unixepoch()), + last_updated int not null default (unixepoch()), foreign key (witch) references witches (id) on delete cascade on update no action, foreign key (watch) references watches (id) on delete cascade on update no action ); @@ -48,8 +48,8 @@ create table if not exists witch_watch ( create table if not exists covens ( witch blob not null primary key, coven blob, -- possibly empty friends list in some app-specific format - created_at int, - last_updated int, + created_at int not null default (unixepoch()), + last_updated int not null default (unixepoch()), foreign key (witch) references witches (id) on delete cascade on update no action ); @@ -59,8 +59,8 @@ create table if not exists watch_notes ( watch blob not null, note blob, public boolean not null, - created_at int, - last_updated int, + created_at int not null default (unixepoch()), + last_updated int not null default (unixepoch()), foreign key (witch) references witches (id) on delete cascade on update no action, foreign key (watch) references watches (id) on delete cascade on update no action ); diff --git a/migrations/20230427212229_update_triggers.down.sql b/migrations/20230427212229_update_triggers.down.sql new file mode 100644 index 0000000..0bc8bd2 --- /dev/null +++ b/migrations/20230427212229_update_triggers.down.sql @@ -0,0 +1,6 @@ +-- Add down migration script here +drop trigger if exists update_last_updated_witches; +drop trigger if exists update_last_updated_watches; +drop trigger if exists update_last_updated_witch_watch; +drop trigger if exists update_last_updated_covens; +drop trigger if exists update_last_updated_watch_notes; diff --git a/migrations/20230427212229_update_triggers.up.sql b/migrations/20230427212229_update_triggers.up.sql new file mode 100644 index 0000000..447f9c2 --- /dev/null +++ b/migrations/20230427212229_update_triggers.up.sql @@ -0,0 +1,34 @@ +create trigger if not exists update_last_updated_witches + after update on witches + when OLD.last_updated = NEW.last_updated or OLD.last_updated is null +BEGIN + update witches set last_updated = (select unixepoch()) where id=NEW.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 +BEGIN + update watches set last_updated = (select unixepoch()) where id=NEW.id; +END; + +create trigger if not exists update_last_updated_witch_watch + after update on witch_watch + when OLD.last_updated = NEW.last_updated or OLD.last_updated is null +BEGIN + update witch_watch set last_updated = (select unixepoch()) where id=NEW.id; +END; + +create trigger if not exists update_last_updated_covens + after update on covens + when OLD.last_updated = NEW.last_updated or OLD.last_updated is null +BEGIN + update covens set last_updated = (select unixepoch()) where id=NEW.id; +END; + +create trigger if not exists update_last_updated_watch_notes + after update on watch_notes + when OLD.last_updated = NEW.last_updated or OLD.last_updated is null +BEGIN + update watch_notes set last_updated = (select unixepoch()) where id=NEW.id; +END;