diff --git a/schema/init/aime.sql b/schema/init/aime.sql index 342185f..65c2d5e 100644 --- a/schema/init/aime.sql +++ b/schema/init/aime.sql @@ -1,35 +1,9 @@ -create table "aime_shop" ( - "id" integer primary key not null, - "ext_id" integer not null, - "name" text not null, - "region" text not null, - constraint "aime_shop_uq" unique ("ext_id") -); - -create table "aime_machine" ( - "id" integer primary key not null, - "shop_id" integer not null - references "aime_shop"("id"), - "pcb_id" text not null, - "keychip_id" text not null, - constraint "aime_machine_pcb_id_uq" unique ("pcb_id"), - constraint "aime_machine_keychip_id_uq" unique ("keychip_id") -); - create table "aime_player" ( "id" integer primary key not null, "ext_id" integer not null, - "register_time" timestamp not null, - constraint "aime_player_uq" unique ("ext_id") -); - -create table "aime_card" ( - "id" integer primary key not null, - "player_id" integer not null - references "aime_player"("id") - on delete cascade, - "nfc_id" text not null, + "luid" text not null, "register_time" timestamp not null, "access_time" timestamp not null, - constraint "aime_card_uq" unique ("nfc_id") + constraint "aime_player_ext_id_uq" unique ("ext_id"), + constraint "aime_player_luid_uq" unique ("luid") ); diff --git a/schema/migrate/M0007-aime-simplify-player.sql b/schema/migrate/M0007-aime-simplify-player.sql new file mode 100644 index 0000000..cd927f8 --- /dev/null +++ b/schema/migrate/M0007-aime-simplify-player.sql @@ -0,0 +1,27 @@ +-- Can't add NOT NULL or UNIQUE constraints as we'd like to. Fortunately we +-- haven't released anything yet so there aren't really any DBs with live +-- data out there in the wild. + +-- (Can't drop and recreate the table either btw because "aime_player" is +-- transitively referenced by _literally everything_). + +alter table "aime_player" add column "luid" text; +alter table "aime_player" add column "access_time" text; + +update "aime_player" as "p" +set "luid" = ( + select "c"."nfc_id" + from "aime_card" as "c" + where "c"."player_id" = "p"."id" + ), + "access_time" = ( + select "c"."access_time" + from "aime_card" as "c" + where "c"."player_id" = "p"."id" + ); + +drop table "aime_card"; + +-- These were never used. Re-create them if they become necessary. +drop table "aime_machine"; +drop table "aime_shop"; diff --git a/src/aimedb/sql.ts b/src/aimedb/sql.ts index c4b54a4..3ae5429 100644 --- a/src/aimedb/sql.ts +++ b/src/aimedb/sql.ts @@ -9,10 +9,9 @@ class CardRepositoryImpl implements CardRepository { async lookup(luid: string, now: Date): Promise { const fetchSql = sql - .select("c.id", "p.ext_id") - .from("aime_card c") - .join("aime_player p", { "c.player_id": "p.id" }) - .where("c.nfc_id", luid); + .select("p.id", "p.ext_id") + .from("aime_player p") + .where("p.luid", luid); const row = await this._txn.fetchRow(fetchSql); @@ -24,7 +23,7 @@ class CardRepositoryImpl implements CardRepository { const extId = row.ext_id; const touchSql = sql - .update("aime_card") + .update("aime_player") .set({ access_time: now }) .where("id", id); @@ -34,27 +33,18 @@ class CardRepositoryImpl implements CardRepository { } async register(luid: string, now: Date): Promise { - const playerId = this._txn.generateId(); - const cardId = this._txn.generateId(); + const id = this._txn.generateId(); const aimeId = generateExtId() as AimeId; - const playerSql = sql.insert("aime_player", { - id: playerId, + const insertSql = sql.insert("aime_player", { + id: id, ext_id: aimeId, - register_time: now, - }); - - await this._txn.modify(playerSql); - - const cardSql = sql.insert("aime_card", { - id: cardId, - player_id: playerId, - nfc_id: luid, + luid: luid, register_time: now, access_time: now, }); - await this._txn.modify(cardSql); + await this._txn.modify(insertSql); return aimeId; }