fix(server): add the missing numbered migration for the booked-night reseat step (#242)

reseatBookedNights (legacy step 242) was never invoked from any shipped
MikroORM migration -- only from the retired db/migrations.ts array --
so an install upgrading through the numbered chain never got the
one-time reorder. Adds Migration20200101040300, which calls the frozen
function through the native better-sqlite3 handle so it runs inside
the migration's own transaction.

orm-driver.ts's BoundSqliteConnection now also captures that handle
onto the field SqliteConnection.getNativeClient() reads, which its
createKyselyDialect() override previously left unset -- needed for
this migration (and any future one) to reach it in production, not
just under the bare test driver.

RESEAT-009 moves off the legacy createTables+runMigrations builder
onto the real Migrator via tests/helpers/migration-step.ts, plus a
new fresh-install guard (RESEAT-011).
This commit is contained in:
jubnl
2026-09-24 07:45:29 +02:00
parent 90b1b1d4c7
commit c9c40b8a0b
3 changed files with 121 additions and 40 deletions
@@ -0,0 +1,40 @@
import { Migration } from '@mikro-orm/migrations';
import type { SqliteConnection } from '@mikro-orm/sqlite';
import { reseatBookedNights } from '../reseat-booked-nights';
/**
* Legacy migration step 242 (`db/migrations.ts`'s own last step).
*
* Seat every booked night where its check-in says, the way a night booked
* today is seated (`AccommodationsService.positionForCheckIn`) — see
* `reseat-booked-nights.ts`'s own header for the full rule. Every trip
* planned before that ordering change keeps the old, wrong order until
* somebody edits its check-in; this brings them forward the same way step
* 229/230 (`Migration20200101034900`) brought the day stop itself forward for
* older bookings.
*
* Plan 4 Task 5c's finding: unlike ruling 7's assumption, nothing in the
* numbered chain ever called `reseatBookedNights` — the two migrations near
* the end that touch booked nights both call `attachStayStopsToCheckInDay`
* instead (a different legacy step). This is the numbered migration that was
* missing; an install upgrading through the chain without it never got this
* one-time reorder.
*
* Frozen, called rather than reimplemented (ruling 7's spirit: don't rewrite
* a step onto the ORM's query builder, since that risks producing a
* different result than the one already shipped). `reseatBookedNights` wants
* the synchronous better-sqlite3 API directly, not `this.execute()`, so this
* reaches it through `getNativeClient()` — the same physical connection/
* transaction `this.addSql()` would use, so it still runs inside the
* migration's transaction exactly as the function's own docstring assumes.
*/
export class Migration20200101040300_a_booked_night_finally_took_the_seat_a_new extends Migration {
override name = 'Migration20200101040300_a_booked_night_finally_took_the_seat_a_new';
override async up(): Promise<void> {
const connection = this.driver.getConnection() as SqliteConnection;
const db = await connection.getNativeClient();
const seated = reseatBookedNights(db);
if (seated > 0) console.log(`[DB] Reseated ${seated} booked night(s) to their check-in day`);
}
}
+13 -1
View File
@@ -42,7 +42,19 @@ class NonClosingSqliteDriver extends KyselySqliteDriver {
export function createBoundSqliteDriver(getHandle: () => Database.Database): typeof SqliteDriver {
class BoundSqliteConnection extends SqliteConnection {
override createKyselyDialect(): Dialect {
const config: SqliteDialectConfig = { database: getHandle() };
const handle = getHandle();
const config: SqliteDialectConfig = { database: handle };
// `SqliteConnection.getNativeClient()` reads this private field, but only
// ever sets it from the base class's own `createKyselyDialect()` — which
// this override replaces entirely, so it never runs. Capturing it here
// too is what lets a migration reach this exact synchronous
// better-sqlite3 handle via `getNativeClient()` in production, not just
// under the unbound test driver (`createMigrationOrm()` in
// tests/helpers/migration-step.ts, whose stock SqliteConnection sets it
// unmodified). Needed by migrations that call a frozen, hand-written
// step directly instead of through `this.execute()` — e.g.
// Migration20200101040300, which calls reseat-booked-nights.ts.
(this as unknown as { database: Database.Database }).database = handle;
// Delegate the compiler/adapter/introspector to a throwaway stock dialect
// (stateless factories); only `createDriver()` needs the non-closing swap.
const stock = new SqliteDialect(config);
@@ -8,16 +8,20 @@
*
* The rules live in reseat-booked-nights.ts and are tested there directly on a fully
* migrated database: the columns the step reads (assignment_time, accommodation_id)
* and the vias table come from migrations, so seeding before runMigrations is not an
* option, and rewinding schema_version would tie this file to the tail of the
* append-only array.
* and the vias table come from migrations, so seeding before the schema is fully
* migrated is not an option.
*
* As the numbered migration (step 242, `Migration20200101040300`): ported off the
* legacy `createTables`+`runMigrations` builder onto the real `Migrator`, same shape
* as `tests/unit/db/trip-days-backfill-migration.test.ts` — migrate to the step
* immediately before it, seed rows with raw SQL, apply just that one migration, assert.
*/
import { describe, it, expect } from 'vitest';
import Database from 'better-sqlite3';
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import type { MikroORM } from '@mikro-orm/sqlite';
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { reseatBookedNights } from '../../../src/db/reseat-booked-nights';
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
function freshDb() {
const db = createSnapshotTestDb();
@@ -198,38 +202,63 @@ describe('booked night reseat', () => {
expect(order(db)).toEqual(once);
});
it('RESEAT-009: the migration runs the step on a database being upgraded', () => {
// Plan 4 Task 5c FINDING: unlike ruling 7's claim, `reseatBookedNights` is
// NOT invoked from inside any numbered MikroORM migration — only from the
// legacy `db/migrations.ts` array (its last step, #242). Grepped the whole
// `src/db/migrations/` tree for `reseat`/`reseatBookedNights` and every
// check-in/seat-ordering wording: no hit. The two numbered migrations that
// DO touch booked nights near the end of the chain
// (`Migration20200101034900_give_every_stay_booked_before_this_release`,
// `Migration20200101035000_the_same_sweep_once_more_for_the`) both call
// `attachStayStopsToCheckInDay` (creates the day stop), a DIFFERENT legacy
// step (#229/#230) from the reseat/reorder step (#242) this test exercises.
// So this one case is left on the legacy `createTables`+`runMigrations`
// builder deliberately, NOT swapped: there is no numbered-migration target
// to port it onto. Flagged for the controller/Task 6 — this is a real "no
// equivalent found" gap (ruling 10), not an oversight: an install upgrading
// through the numbered chain never gets this one-time reorder applied, and
// this file will still show up in Task 6's "zero test files import
// db/schema or db/migrations" precondition grep until that gap is resolved
// one way or another.
const db = new Database(':memory:');
db.exec('PRAGMA journal_mode = WAL');
db.exec('PRAGMA foreign_keys = ON');
createTables(db);
db.prepare("INSERT INTO users (id, username, email, password_hash, role) VALUES (1, 'u', 'u@test.local', 'x', 'user')").run();
db.prepare("INSERT INTO trips (id, user_id, title) VALUES (1, 1, 'T')").run();
db.prepare("INSERT INTO days (id, trip_id, day_number, date) VALUES (1, 1, 1, '2026-10-02')").run();
db.prepare("INSERT INTO places (id, trip_id, name, lat, lng) VALUES (901, 1, 'Aral', 50.1, 10), (902, 1, 'Rostock', 50.2, 10)").run();
db.prepare('INSERT INTO day_assignments (day_id, place_id, order_index) VALUES (1, 901, 0)').run();
db.prepare("INSERT INTO day_accommodations (trip_id, place_id, start_day_id, end_day_id, check_in) VALUES (1, 902, 1, 1, '10:00')").run();
runMigrations(db);
expect(order(db)).toEqual([902, 901]);
});
});
describe('booked night reseat — as the numbered migration (step 242)', () => {
// Plan 4 Task 6 item 1: ruling 7 assumed `reseatBookedNights` was already
// invoked from a shipped migration; Task 5c found it wasn't (only the
// legacy `db/migrations.ts` array called it, as its last step, #242). This
// is that missing numbered migration, appended at the end of the chain.
const MIGRATION = 'Migration20200101040300_a_booked_night_finally_took_the_seat_a_new';
async function ormBefore(): Promise<MikroORM> {
const orm = await createMigrationOrm();
const names = await pendingNames(orm);
const idx = names.indexOf(MIGRATION);
expect(idx).toBeGreaterThan(0);
await migrateTo(orm, names[idx - 1]);
return orm;
}
it('RESEAT-009: an install upgrading through the chain gets an out-of-order night reseated', async () => {
const orm = await ormBefore();
try {
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash, role) VALUES (1, 'u', 'u@test.local', 'x', 'user')");
await rawExec(orm, "INSERT INTO trips (id, user_id, title) VALUES (1, 1, 'T')");
await rawExec(orm, "INSERT INTO days (id, trip_id, day_number, date) VALUES (1, 1, 1, '2026-10-02')");
await rawExec(orm, "INSERT INTO places (id, trip_id, name, lat, lng) VALUES (901, 1, 'Aral', 50.1, 10), (902, 1, 'Rostock', 50.2, 10)");
await rawExec(orm, "INSERT INTO day_accommodations (trip_id, place_id, start_day_id, end_day_id, check_in) VALUES (1, 902, 1, 1, '10:00')");
const accRow = await rawQuery<{ id: number }>(orm, 'SELECT last_insert_rowid() as id');
const accId = accRow[0]!.id;
// The stop step 229/230 (Migration20200101034900/035000) puts on a
// booked night's check-in day already ran by the time `ormBefore()`
// returns, so it must be seeded here rather than left for that earlier
// migration to create — same convention `stop()`/`night()` above use.
await rawExec(orm, 'INSERT INTO day_assignments (day_id, place_id, order_index) VALUES (1, 901, 0)');
await rawExec(orm, 'INSERT INTO day_assignments (day_id, place_id, order_index, accommodation_id) VALUES (1, 902, 1, ?)', [accId]);
await migrateTo(orm, MIGRATION);
const rows = await rawQuery<{ place_id: number }>(orm, 'SELECT place_id FROM day_assignments WHERE day_id = 1 ORDER BY order_index');
expect(rows.map((r) => r.place_id)).toEqual([902, 901]);
} finally {
await orm.close(true);
}
}, 30000);
it('RESEAT-011: a fresh install with no booked nights out of order migrates through cleanly', async () => {
const orm = await ormBefore();
try {
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash, role) VALUES (1, 'u', 'u@test.local', 'x', 'user')");
await rawExec(orm, "INSERT INTO trips (id, user_id, title) VALUES (1, 1, 'T')");
await rawExec(orm, "INSERT INTO days (id, trip_id, day_number, date) VALUES (1, 1, 1, '2026-10-02')");
await expect(migrateTo(orm, MIGRATION)).resolves.toBeUndefined();
const rows = await rawQuery(orm, 'SELECT * FROM day_assignments WHERE day_id = 1');
expect(rows).toHaveLength(0);
} finally {
await orm.close(true);
}
}, 30000);
});