diff --git a/migrations/20240308005811_users_invitations.down.sql b/migrations/20240308005811_users_invitations.down.sql new file mode 100644 index 0000000..ec254e3 --- /dev/null +++ b/migrations/20240308005811_users_invitations.down.sql @@ -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; + diff --git a/migrations/20240308005811_users_invitations.up.sql b/migrations/20240308005811_users_invitations.up.sql new file mode 100644 index 0000000..ef4c5ed --- /dev/null +++ b/migrations/20240308005811_users_invitations.up.sql @@ -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; + diff --git a/src/main.rs b/src/main.rs index db7f4dd..eeff7f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,8 @@ async fn main() { let pool = db().await; + sqlx::migrate!().run(&pool).await.unwrap(); + // the core application, defining the routes and handlers let app = Router::new() .nest_service("/assets", assets_svc)