adjust migration versioning
This commit is contained in:
parent
e385159f29
commit
60ed168053
6 changed files with 56 additions and 51 deletions
|
@ -1,40 +1,50 @@
|
|||
import { migrate as migrate0_1_0 } from "./migrations/0-1-0";
|
||||
import { migrate as migrate001 } from "./migrations/001";
|
||||
|
||||
export async function migrate(): Promise<void> {
|
||||
async function migrate(): Promise<void> {
|
||||
if (!game.user.isGM) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentVersion: string = game.settings.get("ds4", "systemMigrationVersion");
|
||||
const targetVersion = game.system.data.version;
|
||||
const oldMigrationVersion: number = game.settings.get("ds4", "systemMigrationVersion");
|
||||
|
||||
if (isFirstWorldStart(currentVersion)) {
|
||||
game.settings.set("ds4", "systemMigrationVersion", targetVersion);
|
||||
const targetMigrationVersion = migrations.length;
|
||||
|
||||
if (isFirstWorldStart(oldMigrationVersion)) {
|
||||
game.settings.set("ds4", "systemMigrationVersion", targetMigrationVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNewerVersion(targetVersion, currentVersion)) {
|
||||
return migrateFromTo(oldMigrationVersion, targetMigrationVersion);
|
||||
}
|
||||
|
||||
async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion: number): Promise<void> {
|
||||
if (!game.user.isGM) {
|
||||
return;
|
||||
}
|
||||
|
||||
const migrationsToExecute = migrations.slice(oldMigrationVersion, targetMigrationVersion);
|
||||
|
||||
if (migrationsToExecute.length > 0) {
|
||||
ui.notifications.info(
|
||||
game.i18n.format("DS4.InfoSystemUpdateStart", {
|
||||
currentVersion: currentVersion,
|
||||
targetVersion: targetVersion,
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
|
||||
const migrationFunctionsToExecute = Object.entries(migrations)
|
||||
.filter(([version]) => !isNewerVersion(currentVersion, version)) // currentVersion <= version of migration
|
||||
.filter(([version]) => isNewerVersion(targetVersion, version)); // targetVersion > version of migration
|
||||
|
||||
for (const migration of migrationFunctionsToExecute) {
|
||||
for (const [i, migration] of migrationsToExecute.entries()) {
|
||||
const currentMigrationVersion = oldMigrationVersion + i + 1;
|
||||
console.log("executing migration script ", currentMigrationVersion);
|
||||
try {
|
||||
await migration[1]();
|
||||
await migration();
|
||||
game.settings.set("ds4", "systemMigrationVersion", currentMigrationVersion);
|
||||
} catch (err) {
|
||||
ui.notifications.error(
|
||||
game.i18n.format("DS4.ErrorDuringMigration", {
|
||||
currentVersion: currentVersion,
|
||||
targetVersion: targetVersion,
|
||||
migrationVersion: migration[0],
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
migrationVersion: currentMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
|
@ -46,32 +56,26 @@ export async function migrate(): Promise<void> {
|
|||
|
||||
ui.notifications.info(
|
||||
game.i18n.format("DS4.InfoSystemUpdateCompleted", {
|
||||
currentVersion: currentVersion,
|
||||
targetVersion: targetVersion,
|
||||
currentVersion: oldMigrationVersion,
|
||||
targetVersion: targetMigrationVersion,
|
||||
}),
|
||||
{ permanent: true },
|
||||
);
|
||||
game.settings.set("ds4", "systemMigrationVersion", targetVersion);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The migrations to (potentially) execute when a world is loaded. The key
|
||||
* specifies the version number _from_ which is being migrated to the next
|
||||
* version.
|
||||
*
|
||||
* @example
|
||||
* Given the migration version numbers "0.0.1", "0.0.2", "0.1.2", "0.4.9", and
|
||||
* "0.5.0" an update from version "0.0.2" to "0.5.0" would execute the
|
||||
* migrations with version "0.0.2", "0.1.2", and "0.4.9". In particular,
|
||||
* migrations of a version lower than the current version or higher or equal to
|
||||
* the target version are _not_ executed while all others are.
|
||||
*/
|
||||
const migrations: Record<string, () => Promise<void>> = {
|
||||
"0.1.0": migrate0_1_0,
|
||||
};
|
||||
|
||||
function isFirstWorldStart(version: string): boolean {
|
||||
return version === "";
|
||||
function getTargetMigrationVersion(): number {
|
||||
return migrations.length;
|
||||
}
|
||||
|
||||
const migrations: Array<() => Promise<void>> = [migrate001];
|
||||
|
||||
function isFirstWorldStart(migrationVersion: number): boolean {
|
||||
return migrationVersion < 0;
|
||||
}
|
||||
|
||||
export const migration = {
|
||||
migrate: migrate,
|
||||
migrateFromTo: migrateFromTo,
|
||||
getTargetMigrationVersion: getTargetMigrationVersion,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue