From 30d42bd832e6837ef70d8a7f4c7a8409dff4aa67 Mon Sep 17 00:00:00 2001 From: Tau Date: Thu, 10 Oct 2019 19:59:13 -0400 Subject: [PATCH] Store array colums as CSV text This is in preparation for a transition to SQLite, which sadly does not support array-valued columns (or indeed have much of a type system in general). --- schema/init/idz.sql | 6 +++--- schema/init/meta.sql | 2 +- schema/migrate/M0006-flatten-arrays.sql | 19 +++++++++++++++++++ src/idz/db/car.ts | 4 ++-- src/idz/db/timeAttack.ts | 10 +++++----- 5 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 schema/migrate/M0006-flatten-arrays.sql diff --git a/schema/init/idz.sql b/schema/init/idz.sql index a3c6d24..101c23e 100644 --- a/schema/init/idz.sql +++ b/schema/init/idz.sql @@ -57,7 +57,7 @@ create table "idz_car" ( "selector" integer not null, "field_00" integer not null, "field_02" integer not null, - "field_04" integer[] not null, + "field_04" text not null, "field_46" integer not null, "field_48" integer not null, "field_4a" integer not null, @@ -147,7 +147,7 @@ create table "idz_ta_result" ( on delete cascade, "route_no" smallint not null, "total_time" float not null, - "section_times" float[] not null, + "section_times" text not null, "flags" smallint not null, "grade" smallint not null, "car_selector" integer not null, @@ -161,7 +161,7 @@ create table "idz_ta_best" ( on delete cascade, "route_no" smallint not null, "total_time" float not null, - "section_times" float[] not null, + "section_times" text not null, "flags" smallint not null, "grade" smallint not null, -- TODO enum "car_selector" integer not null, diff --git a/schema/init/meta.sql b/schema/init/meta.sql index b5e5e1b..baef11b 100644 --- a/schema/init/meta.sql +++ b/schema/init/meta.sql @@ -1,2 +1,2 @@ create table "meta" ("schemaver" integer not null); -insert into "meta" values (5); +insert into "meta" values (6); diff --git a/schema/migrate/M0006-flatten-arrays.sql b/schema/migrate/M0006-flatten-arrays.sql new file mode 100644 index 0000000..a4d3973 --- /dev/null +++ b/schema/migrate/M0006-flatten-arrays.sql @@ -0,0 +1,19 @@ +alter table "idz_car" rename column "field_04" to "tmp"; +alter table "idz_car" add column "field_04" text; +update "idz_car" set "field_04" = array_to_string("tmp", ','); +alter table "idz_car" alter column "field_04" set not null; +alter table "idz_car" drop column "tmp"; + +alter table "idz_ta_result" rename column "section_times" to "tmp"; +alter table "idz_ta_result" add column "section_times" text; +update "idz_ta_result" set "section_times" = array_to_string("tmp", ','); +alter table "idz_ta_result" alter column "section_times" set not null; +alter table "idz_ta_result" drop column "tmp"; + +alter table "idz_ta_best" rename column "section_times" to "tmp"; +alter table "idz_ta_best" add column "section_times" text; +update "idz_ta_best" set "section_times" = array_to_string("tmp", ','); +alter table "idz_ta_best" alter column "section_times" set not null; +alter table "idz_ta_best" drop column "tmp"; + +update "meta" set "schemaver" = 6; diff --git a/src/idz/db/car.ts b/src/idz/db/car.ts index 1f966c1..017ebdb 100644 --- a/src/idz/db/car.ts +++ b/src/idz/db/car.ts @@ -10,7 +10,7 @@ function _extractRow(row: any): Car { selector: row.selector, field_00: row.field_00, field_02: row.field_02, - field_04: row.field_04, + field_04: row.field_04.split(",").map((x: string) => parseInt(x, 10)), field_46: row.field_46, field_48: row.field_48, field_4a: row.field_4a, @@ -70,7 +70,7 @@ export class SqlCarRepository implements CarRepository { selector: car.selector, field_00: car.field_00, field_02: car.field_02, - field_04: car.field_04, + field_04: car.field_04.join(","), field_46: car.field_46, field_48: car.field_48, field_4a: car.field_4a, diff --git a/src/idz/db/timeAttack.ts b/src/idz/db/timeAttack.ts index 8b249c4..c88b1c9 100644 --- a/src/idz/db/timeAttack.ts +++ b/src/idz/db/timeAttack.ts @@ -31,7 +31,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { timestamp: new Date(row.timestamp), flags: row.flags, totalTime: row.total_time, - sectionTimes: row.section_times, + sectionTimes: row.section_times.split(","), grade: row.grade, carSelector: row.car_selector, }, @@ -51,7 +51,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { timestamp: new Date(row.timestamp), flags: row.flags, totalTime: row.total_time, - sectionTimes: row.section_times, + sectionTimes: row.section_times.split(",").map(parseFloat), grade: row.grade, carSelector: row.car_selector, })); @@ -63,7 +63,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { profile_id: profileId, route_no: score.routeNo, total_time: score.totalTime, - section_times: score.sectionTimes, + section_times: score.sectionTimes.join(","), flags: score.flags, grade: score.grade, car_selector: score.carSelector, @@ -86,7 +86,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { profile_id: profileId, route_no: score.routeNo, total_time: score.totalTime, - section_times: score.sectionTimes, + section_times: score.sectionTimes.join(","), flags: score.flags, grade: score.grade, car_selector: score.carSelector, @@ -98,7 +98,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { const updateSql = sql .update("idz_ta_best", { total_time: score.totalTime, - section_times: score.sectionTimes, + section_times: score.sectionTimes.join(","), flags: score.flags, grade: score.grade, car_selector: score.carSelector,