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).
This commit is contained in:
Tau 2019-10-10 19:59:13 -04:00
parent 3e6b4f4798
commit 30d42bd832
5 changed files with 30 additions and 11 deletions

View File

@ -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,

View File

@ -1,2 +1,2 @@
create table "meta" ("schemaver" integer not null);
insert into "meta" values (5);
insert into "meta" values (6);

View File

@ -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;

View File

@ -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,

View File

@ -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,