Migrate to SQLite3

We'll keep the door open for side-by-side support of Postgres in
the background, but due to SQLite's type system quirks we cannot
use the same DDL for both databases, so we would have to maintain
two sets of DDL (schema init and schema migration scripts) at once.

Interested future contributors can shoulder this maintenance burden
if they so choose.
This commit is contained in:
Tau
2019-11-06 17:28:30 -05:00
parent f9970fa81c
commit 072d70b42a
15 changed files with 247 additions and 506 deletions
+7 -15
View File
@@ -1,22 +1,14 @@
create type "aime_region" as enum (
'JPN',
'HKG',
'SGP',
'KOR',
'USA'
);
create table "aime_shop" (
"id" bigint primary key not null,
"id" integer primary key not null,
"ext_id" integer not null,
"name" text not null,
"region" "aime_region" not null,
"region" text not null,
constraint "aime_shop_uq" unique ("ext_id")
);
create table "aime_machine" (
"id" bigint primary key not null,
"shop_id" bigint not null
"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,
@@ -25,15 +17,15 @@ create table "aime_machine" (
);
create table "aime_player" (
"id" bigint primary key not null,
"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" bigint primary key not null,
"player_id" bigint not null
"id" integer primary key not null,
"player_id" integer not null
references "aime_player"("id")
on delete cascade,
"nfc_id" text not null,
+57 -61
View File
@@ -2,18 +2,14 @@
-- A "course" contains an uphill and downhill "route"
-- (or inbound/outbound etc)
create schema "idz";
create type "idz_gender" as enum ('male', 'female');
create table "idz_profile" (
"id" bigint primary key not null,
"player_id" bigint not null
"id" integer primary key not null,
"player_id" integer not null
references "aime_player"("id")
on delete cascade,
-- TODO shop_id
"name" text not null,
"lv" smallint not null,
"lv" integer not null,
"exp" integer not null,
"fame" integer not null,
"dpoint" integer not null,
@@ -24,10 +20,10 @@ create table "idz_profile" (
);
create table "idz_chara" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"gender" "idz_gender" not null,
"gender" text not null,
"field_02" integer not null,
"field_04" integer not null,
"field_06" integer not null,
@@ -40,18 +36,18 @@ create table "idz_chara" (
);
create table "idz_course_plays" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"course_no" smallint not null,
"course_no" integer not null,
"count" integer not null,
constraint "idz_course_plays_uq" unique ("profile_id", "course_no")
);
create table "idz_car" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"selector" integer not null,
@@ -73,22 +69,22 @@ create table "idz_car" (
);
create table "idz_car_selection" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"car_id" bigint not null
"car_id" integer not null
references "idz_car"("id")
on delete cascade
);
create table "idz_solo_mission_state" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"grid_no" smallint not null,
"cell_no" smallint not null,
"value" smallint not null,
"grid_no" integer not null,
"cell_no" integer not null,
"value" integer not null,
constraint "idz_solo_mission_state_uq" unique (
"profile_id",
"grid_no",
@@ -97,32 +93,32 @@ create table "idz_solo_mission_state" (
);
create table "idz_settings" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"music" smallint not null,
"pack" bigint not null,
"paper_cup" smallint not null, -- Not a boolean, oddly enough
"music" integer not null,
"pack" integer not null,
"paper_cup" integer not null, -- Not a boolean, oddly enough
"gauges" integer not null
);
create table "idz_story_state" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"x" smallint not null,
"y" smallint not null
"x" integer not null,
"y" integer not null
);
create table "idz_story_cell_state" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"row_no" integer not null,
"col_no" integer not null,
"a" smallint not null,
"b" smallint not null,
"a" integer not null,
"b" integer not null,
constraint "idz_story_cell_state_uq" unique (
"profile_id",
"row_no",
@@ -131,7 +127,7 @@ create table "idz_story_cell_state" (
);
create table "idz_free_car" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"valid_from" timestamp not null
@@ -141,29 +137,29 @@ create table "idz_free_car" (
-- Times are stored as floating-point seconds
create table "idz_ta_result" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"route_no" smallint not null,
"route_no" integer not null,
"total_time" float not null,
"section_times" text not null,
"flags" smallint not null,
"grade" smallint not null,
"flags" integer not null,
"grade" integer not null,
"car_selector" integer not null,
"timestamp" timestamp not null
);
create table "idz_ta_best" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"route_no" smallint not null,
"route_no" integer not null,
"total_time" float not null,
"section_times" text not null,
"flags" smallint not null,
"grade" smallint not null, -- TODO enum
"flags" integer not null,
"grade" integer not null, -- TODO enum
"car_selector" integer not null,
"timestamp" timestamp not null,
constraint "idz_ta_best_uq" unique ("profile_id", "route_no")
@@ -172,11 +168,11 @@ create table "idz_ta_best" (
-- TODO ta_section_best
create table "idz_background_unlock" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"background_no" smallint not null,
"background_no" integer not null,
constraint "idz_background_unlock_uq" unique (
"profile_id",
"background_no"
@@ -184,48 +180,48 @@ create table "idz_background_unlock" (
);
create table "idz_title_unlock" (
"id" bigint primary key not null,
"profile_id" bigint not null
"id" integer primary key not null,
"profile_id" integer not null
references "idz_profile"("id")
on delete cascade,
"title_no" smallint not null,
"title_no" integer not null,
constraint "idz_title_unlock_uq" unique ("profile_id", "title_no")
);
create table "idz_unlocks" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"cup" smallint not null,
"cup" integer not null,
"gauges" integer not null,
"music" integer not null,
"last_mileage_reward" integer not null
);
create table "idz_team" (
"id" bigint primary key not null,
"id" integer primary key not null,
"ext_id" integer not null,
"name" text not null,
"name_bg" smallint not null,
"name_fx" smallint not null,
"name_bg" integer not null,
"name_fx" integer not null,
"register_time" timestamp not null,
constraint "idz_team_uq" unique ("ext_id")
);
create table "idz_team_auto" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_team"("id")
on delete cascade,
"serial_no" smallint not null,
"name_idx" smallint not null,
"serial_no" integer not null,
"name_idx" integer not null,
constraint "idz_team_auto_uq" unique ("serial_no", "name_idx")
);
create table "idz_team_member" (
"id" bigint primary key not null
"id" integer primary key not null
references "idz_profile"("id")
on delete cascade,
"team_id" bigint not null
"team_id" integer not null
references "idz_team"("id")
on delete cascade,
"join_time" timestamp not null,
@@ -233,10 +229,10 @@ create table "idz_team_member" (
);
create table "idz_team_reservation" (
"id" bigint primary key not null
"id" integer primary key not null
references "aime_player"("id")
on delete cascade,
"team_id" bigint not null
"team_id" integer not null
references "idz_team"("id")
on delete cascade,
"join_time" timestamp not null,
-81
View File
@@ -1,81 +0,0 @@
-- Helper functions which might be useful in one-off queries.
-- These are not utilised by any services; those have their own ID generators.
create or replace function idnum(
str text)
returns bigint as $$
declare
num bigint;
x integer;
begin
num := 0;
for i in 1..11 loop
x := ascii(substr(str, i, 1));
num := num << 6;
if x >= 65 and x <= 90 then
-- A to Z
num = num | (x - 65);
elsif x >= 97 and x <= 122 then
-- a to z
num = num | (x - 71);
elsif x >= 48 and x <= 58 then
-- 0 to 9
num = num | (x + 4);
elsif x = 45 then
-- Dash
num = num | 62;
elsif x = 95 then
-- Underscore
num = num | 63;
else
raise 'Bad input';
end if;
end loop;
return num;
end;
$$ language plpgsql;
create or replace function idstr(
num bigint)
returns text as $$
declare
a text;
pos integer;
str text;
begin
a := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
str := '';
for i in 1..11 loop
pos := (num & 63);
str := substr(a, pos + 1, 1) || str;
num := num >> 6;
end loop;
return str;
end;
$$ language plpgsql;
create or replace function newid()
returns bigint as $$
declare
bytes bytea;
result bigint;
begin
bytes := gen_random_bytes(8);
result := 0;
for i in 0..7 loop
result := (result << 8) | get_byte(bytes, i);
end loop;
-- Truncate high bit to ensure result is positive
-- This value is just the decimal representation of 0x7FFFFFFF`FFFFFFFF.
return result & 9223372036854775807;
end;
$$ language plpgsql;
@@ -1,3 +0,0 @@
alter table "idz"."profile" drop column "ext_id";
update "meta" set "schemaver" = 1;
-42
View File
@@ -1,42 +0,0 @@
create table "idz"."team" (
"id" bigint primary key not null,
"ext_id" integer not null,
"name" text not null,
"name_bg" smallint not null,
"name_fx" smallint not null,
"register_time" timestamp not null,
constraint "team_uq" unique ("ext_id")
);
create table "idz"."team_auto" (
"id" bigint primary key not null
references "idz"."team"("id")
on delete cascade,
"serial_no" smallint not null,
"name_idx" smallint not null,
constraint "team_auto_uq" unique ("serial_no", "name_idx")
);
create table "idz"."team_member" (
"id" bigint primary key not null
references "idz"."profile"("id")
on delete cascade,
"team_id" bigint not null
references "idz"."team"("id")
on delete cascade,
"join_time" timestamp not null,
"leader" boolean not null
);
create table "idz"."team_reservation" (
"id" bigint primary key not null
references "aime"."player"("id")
on delete cascade,
"team_id" bigint not null
references "idz"."team"("id")
on delete cascade,
"join_time" timestamp not null,
"leader" boolean not null
);
update "meta" set "schemaver" = 2;
@@ -1,21 +0,0 @@
-- Fun story: PostgreSQL (can't speak for other dbs) doesn't actually treat
-- its column and table names case-insensitively. It forces them to lower-
-- case instead. If you quote an upper-case field name then it will be upper-
-- case in the database and you will need to use quoted upper-case every time
-- you want to access it.
-- It is likely that people will find this behavior surprising, to say the
-- least. We'll change the column names so that they are always lower-case.
alter table "idz"."chara" rename column "field_0A" to "field_0a";
alter table "idz"."chara" rename column "field_0C" to "field_0c";
alter table "idz"."chara" rename column "field_0E" to "field_0e";
alter table "idz"."car" rename column "field_4A" to "field_4a";
alter table "idz"."car" rename column "field_4C" to "field_4c";
alter table "idz"."car" rename column "field_5A" to "field_5a";
alter table "idz"."car" rename column "field_5B" to "field_5b";
alter table "idz"."car" rename column "field_5C" to "field_5c";
alter table "idz"."car" rename column "field_5E" to "field_5e";
update "meta" set "schemaver" = 3;
-81
View File
@@ -1,81 +0,0 @@
alter type "aime"."region" set schema "public";
alter type "region" rename to "aime_region";
alter table "aime"."shop" set schema "public";
alter table "shop" rename to "aime_shop";
alter table "aime"."machine" set schema "public";
alter table "machine" rename to "aime_machine";
alter table "aime"."player" set schema "public";
alter table "player" rename to "aime_player";
alter table "aime"."card" set schema "public";
alter table "card" rename to "aime_card";
----
alter type "idz"."gender" set schema "public";
alter type "gender" rename to "idz_gender";
alter table "idz"."profile" set schema "public";
alter table "profile" rename to "idz_profile";
alter table "idz"."chara" set schema "public";
alter table "chara" rename to "idz_chara";
alter table "idz"."course_plays" set schema "public";
alter table "course_plays" rename to "idz_course_plays";
alter table "idz"."car" set schema "public";
alter table "car" rename to "idz_car";
alter table "idz"."car_selection" set schema "public";
alter table "car_selection" rename to "idz_car_selection";
alter table "idz"."solo_mission_state" set schema "public";
alter table "solo_mission_state" rename to "idz_solo_mission_state";
alter table "idz"."settings" set schema "public";
alter table "settings" rename to "idz_settings";
alter table "idz"."story_state" set schema "public";
alter table "story_state" rename to "idz_story_state";
alter table "idz"."story_cell_state" set schema "public";
alter table "story_cell_state" rename to "idz_story_cell_state";
alter table "idz"."free_car" set schema "public";
alter table "free_car" rename to "idz_free_car";
alter table "idz"."ta_result" set schema "public";
alter table "ta_result" rename to "idz_ta_result";
alter table "idz"."ta_best" set schema "public";
alter table "ta_best" rename to "idz_ta_best";
alter table "idz"."background_unlock" set schema "public";
alter table "background_unlock" rename to "idz_background_unlock";
alter table "idz"."title_unlock" set schema "public";
alter table "title_unlock" rename to "idz_title_unlock";
alter table "idz"."unlocks" set schema "public";
alter table "unlocks" rename to "idz_unlocks";
alter table "idz"."team" set schema "public";
alter table "team" rename to "idz_team";
alter table "idz"."team_auto" set schema "public";
alter table "team_auto" rename to "idz_team_auto";
alter table "idz"."team_member" set schema "public";
alter table "team_member" rename to "idz_team_member";
alter table "idz"."team_reservation" set schema "public";
alter table "team_reservation" rename to "idz_team_reservation";
drop schema "idz";
drop schema "aime";
update "meta" set "schemaver" = 5;
-19
View File
@@ -1,19 +0,0 @@
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;