mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
fix(db): baseline a database from the pre-ORM Tours branch correctly
That branch numbered its own steps after the upstream step it was cut from, so a database at schema_version 243 to 246 (or 216 to 218) with the Tours tables had four upstream migrations marked as run without ever running them. The baseline now cuts such a version back to the last upstream step, and the Tours migration adds the difficulty cap to a tours table an earlier branch stage created without it.
This commit is contained in:
@@ -128,6 +128,25 @@ async function legacyVersion(connection: Connection, migrator: Migrator): Promis
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* The pre-ORM Tours branch numbered its own steps after the upstream step it
|
||||
* was cut from: 216 to 218 on the older line, 243 to 246 on the later one.
|
||||
* Such a database's schema_version counts Tours steps, not the upstream steps
|
||||
* of the same numbers, so it is cut back to the last upstream step it really
|
||||
* has. The upstream steps then run, and the Tours migration adopts the tables
|
||||
* the branch already created.
|
||||
*/
|
||||
async function withoutToursSteps(connection: Connection, version: number): Promise<number> {
|
||||
const toursLine = version >= 216 && version <= 218 ? 215 : version >= 243 && version <= 246 ? 242 : null;
|
||||
if (toursLine === null) return version;
|
||||
const table: unknown[] = await connection.execute(
|
||||
`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'tour_types'`,
|
||||
);
|
||||
if (table.length === 0) return version;
|
||||
console.log(`[DB] schema_version ${version} was written by the pre-ORM Tours branch; baselining to upstream step ${toursLine}`);
|
||||
return toursLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* The migrations a positional-runner database already has — steps 1..N, never
|
||||
* the baseline (see the file header) — or `[]` for every database that is not
|
||||
@@ -138,8 +157,9 @@ export async function planLegacyBaseline(
|
||||
migrator: Migrator,
|
||||
readSource: ReadSource = readFromDisk,
|
||||
): Promise<string[]> {
|
||||
const version = await legacyVersion(connection, migrator);
|
||||
if (version === null) return [];
|
||||
const recorded = await legacyVersion(connection, migrator);
|
||||
if (recorded === null) return [];
|
||||
const version = await withoutToursSteps(connection, recorded);
|
||||
|
||||
const map = buildLegacyStepMap(await migrator.getPending(), readSource);
|
||||
if (version > map.finalStep) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
import { addColumnIfMissing, tableExists } from '../migration-utils';
|
||||
|
||||
/**
|
||||
* Tours: a facet on `places` for hikes and other single-day routes.
|
||||
@@ -18,7 +19,19 @@ import { Migration } from '@mikro-orm/migrations';
|
||||
export class Migration20200101042000_tours extends Migration {
|
||||
override name = 'Migration20200101042000_tours';
|
||||
|
||||
override up(): void {
|
||||
override async up(): Promise<void> {
|
||||
// The branch's earlier stages created `tours` without the difficulty cap.
|
||||
// `CREATE TABLE IF NOT EXISTS` below leaves such a table as it is, so the
|
||||
// column is added first, while the statements below are still queued.
|
||||
if (await tableExists(this, 'tours')) {
|
||||
await addColumnIfMissing(
|
||||
this,
|
||||
'tours',
|
||||
'max_hiking_difficulty',
|
||||
'max_hiking_difficulty INTEGER NOT NULL DEFAULT 2 CHECK(max_hiking_difficulty BETWEEN 1 AND 6)',
|
||||
);
|
||||
}
|
||||
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS tour_types (
|
||||
key TEXT PRIMARY KEY,
|
||||
|
||||
Reference in New Issue
Block a user