what2watch/migrations/20240116054222_users_and_invites.up.sql

42 lines
1.4 KiB
MySQL
Raw Permalink Normal View History

2024-01-17 04:21:38 +00:00
create table if not exists users (
id blob not null primary key default (julid_new()),
username text not null unique,
displayname text,
email text,
2024-04-10 00:44:37 +00:00
last_seen text,
2024-01-17 04:21:38 +00:00
pwhash blob not null,
invited_by blob not null,
is_active boolean not null default true,
2024-04-10 00:44:37 +00:00
last_updated text not null default CURRENT_TIMESTAMP,
2024-01-17 04:21:38 +00:00
foreign key (invited_by) references users (id)
);
create index if not exists users_username_dex on users (lower(username));
create index if not exists users_email_dex on users (lower(email));
create index if not exists users_invited_by_dex on users (invited_by);
create trigger if not exists update_last_updated_users
after update on users
when OLD.last_updated = NEW.last_updated or OLD.last_updated is null
BEGIN
2024-04-07 19:11:55 +00:00
update users set last_updated = CURRENT_TIMESTAMP where id=NEW.id;
2024-01-17 04:21:38 +00:00
END;
-- invitations
create table if not exists invites (
id blob not null primary key default (julid_new()),
owner blob not null,
2024-04-10 00:44:37 +00:00
expires_at text,
2024-01-17 04:21:38 +00:00
remaining int not null default 1,
2024-04-10 00:44:37 +00:00
last_updated text not null default CURRENT_TIMESTAMP,
2024-01-17 04:21:38 +00:00
foreign key (owner) references users (id) on delete cascade on update no action
);
create index if not exists invites_owner_dex on invites (owner);
create trigger if not exists update_last_updated_invites
after update on invites
when OLD.last_updated = NEW.last_updated or OLD.last_updated is null
BEGIN
2024-04-07 19:11:55 +00:00
update invites set last_updated = CURRENT_TIMESTAMP where id=NEW.id;
2024-01-17 04:21:38 +00:00
END;