sebastian-heinz / Arrowgene.DragonsDogmaOnline

Server for Dragons Dogma Online
GNU Affero General Public License v3.0
161 stars 52 forks source link

Database migrator proposal #369

Closed alborrajo closed 1 month ago

alborrajo commented 1 month ago

Addresses #93 and #102

Implement a simple database migrator CLI command that will read the current database version and figure out which strategies to run to upgrade to the target database version.

The migrator will have a list of IMigrationStrategy objects, each with a From and To properties and a Migrate method. The migrator will figure out the shortest path to upgrade from one version to another.

For example, if the database is in version 1 and the target version is 4, it'll try running the least number of migrations to reach version 4: If there's strategies to migrate from version 1 to 2, 2 to 3, 3 to 4, but also, there's a strategy to migrate from 1 to 4 directly, it'll favour the later.

These strategies are regular C# code, meaning you can call SQL but also perform complex logic if needed in C#.

Ironically this PR requires the following SQL script to be run

CREATE TABLE IF NOT EXISTS meta
(
    "db_version" INTEGER NOT NULL
);
INSERT INTO meta (db_version) VALUES (1);

And then, if using sqlite, rename the sqlite file from db.v1.sqlite to dB.sqlite


To migrate a database, run the following command: dotnet run dbmigration This will tell the server to read the current database and upgrade it to whatever database version the server requires (In this case, version 2). If the DB is already in the target version, no actions will be done, if it's not, it'll run the strategies needed to upgrade in order.

It has been tested with SQLite, althought it should work fine with MySQL or Postgres i haven't tested it.

Checklist: