add migrations

This commit is contained in:
Joe Ardent 2024-03-09 17:23:52 -08:00
parent f0d7219045
commit 5aea86d8af
3 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,10 @@
drop table if exists customers;
drop index if exists customers_username_dex;
drop index if exists customers_email_dex;
drop index if exists customers_invitation_dex;
drop trigger if exists update_last_updated_customers;
drop table if exists invitations;
drop index if exists invitations_owner_dex;
drop trigger if exists update_updated_at_invitations;

View File

@ -0,0 +1,40 @@
create table if not exists customers (
id integer primary key,
username text not null unique,
receipt text not null unique,
billing_email text,
invitation id,
created_at int not null default (unixepoch()),
updated_at int not null default (unixepoch()),
foreign key (invitation) references invitations (id)
);
create index if not exists customers_username_dex on customers (lower(username));
create index if not exists customers_email_dex on customers (lower(billing_email));
create index if not exists customers_invitation_dex on customers (invitation); -- does this need to be created? it's already a foreign key
create trigger if not exists update_last_updated_customers
after update on customers
when OLD.updated_at = NEW.updated_at or OLD.updated_at is null
BEGIN
update customers set updated_at = (select unixepoch()) where id=NEW.id;
END;
create table if not exists invitations (
id integer primary key,
owner integer not null,
remaining integer not null default 1,
expires_at integer,
created_at integer not null default (unixepoch()),
updated_at integer not null default (unixepoch()),
foreign key (owner) references customers (id)
);
create index if not exists invitations_owner_dex on invitations (owner);
create trigger if not exists update_updated_at_invitations
after update on invitations
when OLD.updated_at = NEW.updated_at or OLD.updated_at is null
BEGIN
update invitations set updated_at = (select unixepoch()) where id=NEW.id;
END;

View File

@ -34,6 +34,8 @@ async fn main() {
let pool = db().await; let pool = db().await;
sqlx::migrate!().run(&pool).await.unwrap();
// the core application, defining the routes and handlers // the core application, defining the routes and handlers
let app = Router::new() let app = Router::new()
.nest_service("/assets", assets_svc) .nest_service("/assets", assets_svc)