Skip to content

Commit

Permalink
Use SQLite schema as foundation & transform incompatible syntax to av…
Browse files Browse the repository at this point in the history
…oid multiple schema files.
  • Loading branch information
Sehkah committed Sep 23, 2023
1 parent 7533f89 commit 1a1926d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
13 changes: 13 additions & 0 deletions Arrowgene.Ddon.Database/DdonDatabaseBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using Arrowgene.Ddon.Database.Model;
using Arrowgene.Ddon.Database.Sql;
using Arrowgene.Logging;
Expand Down Expand Up @@ -52,6 +53,13 @@ public static DdonPostgresDb BuildPostgres(string databaseFolder, string host, s
DdonPostgresDb db = new DdonPostgresDb(host, user, password, database, wipeOnStartup);
if (db.CreateDatabase())
{
string schemaFilePath = Path.Combine(databaseFolder, "Script/schema_sqlite.sql");
String schema = File.ReadAllText(schemaFilePath, Encoding.UTF8);
schema = schema.Replace(" DATETIME ", " TIMESTAMP WITH TIME ZONE ");
schema = schema.Replace(" INTEGER PRIMARY KEY AUTOINCREMENT ", " SERIAL PRIMARY KEY ");
schema = schema.Replace(" BLOB ", " BYTEA ");
File.WriteAllText(schemaFilePath, schema);

ScriptRunner scriptRunner = new ScriptRunner(db);
scriptRunner.Run(Path.Combine(databaseFolder, "Script/schema_postgres.sql"));
}
Expand All @@ -64,6 +72,11 @@ public static DdonMariaDb BuildMariaDB(string databaseFolder, string host, strin
DdonMariaDb db = new DdonMariaDb(host, user, password, database, wipeOnStartup);
if (db.CreateDatabase())
{
string schemaFilePath = Path.Combine(databaseFolder, "Script/schema_sqlite.sql");
String schema = File.ReadAllText(schemaFilePath, Encoding.UTF8);
schema = schema.Replace(" AUTOINCREMENT ", " AUTO_INCREMENT ");
File.WriteAllText(schemaFilePath, schema);

ScriptRunner scriptRunner = new ScriptRunner(db);
scriptRunner.Run(Path.Combine(databaseFolder, "Script/schema_mariadb.sql"));
}
Expand Down
2 changes: 1 addition & 1 deletion deploy/mariadb/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
restart: no
volumes:
- ddon-mariadb-volume:/var/lib/mysql
- $PWD/../../Arrowgene.Ddon.Database/Files/Database/Script/schema_mariadb.sql:/docker-entrypoint-initdb.d/schema_mariadb.sql:ro
# - $PWD/schema_mariadb.sql:/docker-entrypoint-initdb.d/schema_mariadb.sql:ro
- $PWD/mariadb.cnf:/etc/mysql/mariadb.conf.d/mariadb.cnf:ro
environment:
- MARIADB_USER=admin
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion deploy/postgresql/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
restart: no
volumes:
- ddon-psql-data:/var/lib/postgresql/data
- $PWD/../../Arrowgene.Ddon.Database/Files/Database/Script/schema_postgres.sql:/docker-entrypoint-initdb.d/schema_postgres.sql:ro
# - $PWD/schema_postgres.sql:/docker-entrypoint-initdb.d/schema_postgres.sql:ro
- $PWD/postgresql.conf:/etc/postgresql/postgresql.conf:ro
environment:
- POSTGRES_USER=root
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
CREATE DOMAIN DATETIME AS TIMESTAMP WITH TIME ZONE;
CREATE DOMAIN BLOB AS BYTEA;

CREATE TABLE IF NOT EXISTS setting
(
"key" VARCHAR(32) NOT NULL,
Expand All @@ -16,14 +13,14 @@ CREATE TABLE IF NOT EXISTS account
"hash" TEXT NOT NULL,
"mail" TEXT NOT NULL,
"mail_verified" BOOLEAN NOT NULL,
"mail_verified_at" DATETIME DEFAULT NULL,
"mail_verified_at" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
"mail_token" TEXT DEFAULT NULL,
"password_token" TEXT DEFAULT NULL,
"login_token" TEXT DEFAULT NULL,
"login_token_created" DATETIME DEFAULT NULL,
"login_token_created" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
"state" INTEGER NOT NULL,
"last_login" DATETIME DEFAULT NULL,
"created" DATETIME NOT NULL,
"last_login" TIMESTAMP WITH TIME ZONE DEFAULT NULL,
"created" TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT uq_account_name UNIQUE ("name"),
CONSTRAINT uq_account_normal_name UNIQUE ("normal_name"),
CONSTRAINT uq_account_login_token UNIQUE ("login_token"),
Expand All @@ -47,7 +44,7 @@ CREATE TABLE IF NOT EXISTS ddon_character
"version" INTEGER NOT NULL,
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"created" DATETIME NOT NULL,
"created" TIMESTAMP WITH TIME ZONE NOT NULL,
"my_pawn_slot_num" SMALLINT NOT NULL,
"rental_pawn_slot_num" SMALLINT NOT NULL,
"hide_equip_head_pawn" BOOLEAN NOT NULL,
Expand Down Expand Up @@ -246,7 +243,7 @@ CREATE TABLE IF NOT EXISTS ddon_storage
"character_id" INTEGER NOT NULL,
"storage_type" SMALLINT NOT NULL,
"slot_max" SMALLINT NOT NULL,
"item_sort" BLOB NOT NULL,
"item_sort" BYTEA NOT NULL,
CONSTRAINT pk_ddon_storage PRIMARY KEY (character_id, storage_type),
CONSTRAINT fk_storage_character_id FOREIGN KEY ("character_id") REFERENCES ddon_character ("character_id") ON DELETE CASCADE
);
Expand Down Expand Up @@ -406,7 +403,7 @@ CREATE TABLE IF NOT EXISTS ddon_game_token
"account_id" INTEGER PRIMARY KEY NOT NULL,
"character_id" INTEGER NOT NULL,
"token" TEXT NOT NULL,
"created" DATETIME NOT NULL,
"created" TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT uq_game_token_token UNIQUE ("token"),
CONSTRAINT fk_game_token_account_id FOREIGN KEY ("account_id") REFERENCES account ("id"),
CONSTRAINT fk_game_token_character_id FOREIGN KEY ("character_id") REFERENCES ddon_character ("character_id")
Expand All @@ -417,7 +414,7 @@ CREATE TABLE IF NOT EXISTS ddon_connection
"server_id" INTEGER NOT NULL,
"account_id" INTEGER NOT NULL,
"type" INTEGER NOT NULL,
"created" DATETIME NOT NULL,
"created" TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT uq_connection_server_id_account_id UNIQUE (server_id, account_id),
CONSTRAINT fk_connection_token_account_id FOREIGN KEY ("account_id") REFERENCES account ("id")
);

0 comments on commit 1a1926d

Please sign in to comment.