Isolate legacy migration transactions
This commit is contained in:
@@ -336,6 +336,8 @@ Implemented foundation:
|
|||||||
- circuit-device-row transaction tests cover both successful commits and forced SQLite rollbacks
|
- circuit-device-row transaction tests cover both successful commits and forced SQLite rollbacks
|
||||||
- project-device row synchronization has real multi-row SQLite commit and rollback coverage
|
- project-device row synchronization has real multi-row SQLite commit and rollback coverage
|
||||||
- BMK swaps and section reorder have real SQLite commit and rollback coverage
|
- BMK swaps and section reorder have real SQLite commit and rollback coverage
|
||||||
|
- the legacy consumer migration receives its database dependency at the script entry point
|
||||||
|
- legacy circuit, row, mapping and report writes have real late-failure rollback coverage
|
||||||
- circuit and device-row persistence value mapping is separated from the general repositories
|
- circuit and device-row persistence value mapping is separated from the general repositories
|
||||||
|
|
||||||
## Phase 12: Project Revisions and Persistent Undo / Redo
|
## Phase 12: Project Revisions and Persistent Undo / Redo
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
|
import { db } from "../src/db/client.js";
|
||||||
import { CircuitListRepository } from "../src/db/repositories/circuit-list.repository.js";
|
import { CircuitListRepository } from "../src/db/repositories/circuit-list.repository.js";
|
||||||
|
import { LegacyConsumerMigrationRepository } from "../src/db/repositories/legacy-consumer-migration.repository.js";
|
||||||
import { ProjectRepository } from "../src/db/repositories/project.repository.js";
|
import { ProjectRepository } from "../src/db/repositories/project.repository.js";
|
||||||
import { LegacyConsumerMigrationService } from "../src/domain/services/legacy-consumer-migration.service.js";
|
import { LegacyConsumerMigrationService } from "../src/domain/services/legacy-consumer-migration.service.js";
|
||||||
|
|
||||||
const projectRepository = new ProjectRepository();
|
const projectRepository = new ProjectRepository();
|
||||||
const circuitListRepository = new CircuitListRepository();
|
const circuitListRepository = new CircuitListRepository();
|
||||||
const migrationService = new LegacyConsumerMigrationService();
|
const migrationService = new LegacyConsumerMigrationService(
|
||||||
|
new LegacyConsumerMigrationRepository(db)
|
||||||
|
);
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
const projects = await projectRepository.list();
|
const projects = await projectRepository.list();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import { eq, inArray } from "drizzle-orm";
|
import { eq, inArray } from "drizzle-orm";
|
||||||
import { db } from "../client.js";
|
import type { AppDatabase } from "../database-context.js";
|
||||||
import { circuitDeviceRows } from "../schema/circuit-device-rows.js";
|
import { circuitDeviceRows } from "../schema/circuit-device-rows.js";
|
||||||
import { circuits } from "../schema/circuits.js";
|
import { circuits } from "../schema/circuits.js";
|
||||||
import { legacyConsumerCircuitMigrations } from "../schema/legacy-consumer-circuit-migrations.js";
|
import { legacyConsumerCircuitMigrations } from "../schema/legacy-consumer-circuit-migrations.js";
|
||||||
@@ -8,11 +8,11 @@ import { legacyConsumerMigrationReports } from "../schema/legacy-consumer-migrat
|
|||||||
import {
|
import {
|
||||||
toCircuitDeviceRowCreateValues,
|
toCircuitDeviceRowCreateValues,
|
||||||
type CircuitDeviceRowCreateInput,
|
type CircuitDeviceRowCreateInput,
|
||||||
} from "./circuit-device-row.repository.js";
|
} from "./circuit-device-row.persistence.js";
|
||||||
import {
|
import {
|
||||||
toCircuitCreateValues,
|
toCircuitCreateValues,
|
||||||
type CircuitCreatePersistenceInput,
|
type CircuitCreatePersistenceInput,
|
||||||
} from "./circuit.repository.js";
|
} from "./circuit.persistence.js";
|
||||||
|
|
||||||
export interface LegacyMigrationCircuitPersistenceInput {
|
export interface LegacyMigrationCircuitPersistenceInput {
|
||||||
circuit: CircuitCreatePersistenceInput;
|
circuit: CircuitCreatePersistenceInput;
|
||||||
@@ -34,8 +34,10 @@ export interface LegacyMigrationReportPersistenceInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class LegacyConsumerMigrationRepository {
|
export class LegacyConsumerMigrationRepository {
|
||||||
|
constructor(private readonly database: AppDatabase) {}
|
||||||
|
|
||||||
async listMigratedConsumerIds(circuitListId: string) {
|
async listMigratedConsumerIds(circuitListId: string) {
|
||||||
const rows = await db
|
const rows = await this.database
|
||||||
.select({ consumerId: legacyConsumerCircuitMigrations.consumerId })
|
.select({ consumerId: legacyConsumerCircuitMigrations.consumerId })
|
||||||
.from(legacyConsumerCircuitMigrations)
|
.from(legacyConsumerCircuitMigrations)
|
||||||
.where(eq(legacyConsumerCircuitMigrations.circuitListId, circuitListId));
|
.where(eq(legacyConsumerCircuitMigrations.circuitListId, circuitListId));
|
||||||
@@ -78,7 +80,7 @@ export class LegacyConsumerMigrationRepository {
|
|||||||
});
|
});
|
||||||
const createdAtIso = new Date().toISOString();
|
const createdAtIso = new Date().toISOString();
|
||||||
|
|
||||||
db.transaction((tx) => {
|
this.database.transaction((tx) => {
|
||||||
if (consumerIds.length > 0) {
|
if (consumerIds.length > 0) {
|
||||||
const existingMappings = tx
|
const existingMappings = tx
|
||||||
.select({ consumerId: legacyConsumerCircuitMigrations.consumerId })
|
.select({ consumerId: legacyConsumerCircuitMigrations.consumerId })
|
||||||
|
|||||||
@@ -34,7 +34,10 @@ export class LegacyConsumerMigrationService {
|
|||||||
private readonly circuitRepository = new CircuitRepository();
|
private readonly circuitRepository = new CircuitRepository();
|
||||||
private readonly consumerRepository = new ConsumerRepository();
|
private readonly consumerRepository = new ConsumerRepository();
|
||||||
private readonly roomRepository = new RoomRepository();
|
private readonly roomRepository = new RoomRepository();
|
||||||
private readonly migrationRepository = new LegacyConsumerMigrationRepository();
|
|
||||||
|
constructor(
|
||||||
|
private readonly migrationRepository: LegacyConsumerMigrationRepository
|
||||||
|
) {}
|
||||||
|
|
||||||
async migrateCircuitList(projectId: string, circuitListId: string): Promise<LegacyMigrationReport> {
|
async migrateCircuitList(projectId: string, circuitListId: string): Promise<LegacyMigrationReport> {
|
||||||
// Migration is additive: legacy consumers are preserved, and circuit-first entities are created
|
// Migration is additive: legacy consumers are preserved, and circuit-first entities are created
|
||||||
|
|||||||
@@ -1,141 +1,183 @@
|
|||||||
|
import path from "node:path";
|
||||||
import assert from "node:assert/strict";
|
import assert from "node:assert/strict";
|
||||||
import { describe, it } from "node:test";
|
import { describe, it } from "node:test";
|
||||||
import { db } from "../src/db/client.js";
|
import { eq } from "drizzle-orm";
|
||||||
|
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||||
|
import {
|
||||||
|
createDatabaseContext,
|
||||||
|
type DatabaseContext,
|
||||||
|
} from "../src/db/database-context.js";
|
||||||
|
import { DistributionBoardRepository } from "../src/db/repositories/distribution-board.repository.js";
|
||||||
import { LegacyConsumerMigrationRepository } from "../src/db/repositories/legacy-consumer-migration.repository.js";
|
import { LegacyConsumerMigrationRepository } from "../src/db/repositories/legacy-consumer-migration.repository.js";
|
||||||
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
||||||
|
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||||
import { circuits } from "../src/db/schema/circuits.js";
|
import { circuits } from "../src/db/schema/circuits.js";
|
||||||
import { legacyConsumerCircuitMigrations } from "../src/db/schema/legacy-consumer-circuit-migrations.js";
|
import { legacyConsumerCircuitMigrations } from "../src/db/schema/legacy-consumer-circuit-migrations.js";
|
||||||
import { legacyConsumerMigrationReports } from "../src/db/schema/legacy-consumer-migration-report.js";
|
import { legacyConsumerMigrationReports } from "../src/db/schema/legacy-consumer-migration-report.js";
|
||||||
|
import { projects } from "../src/db/schema/projects.js";
|
||||||
|
|
||||||
describe("legacy consumer migration repository", () => {
|
function createTestDatabase() {
|
||||||
it("writes circuits, rows, mappings and report in one synchronous transaction", () => {
|
const context = createDatabaseContext(":memory:");
|
||||||
const originalTransaction = db.transaction;
|
migrate(context.db, {
|
||||||
const inserts: Array<{ table: unknown; values: unknown }> = [];
|
migrationsFolder: path.resolve("src", "db", "migrations"),
|
||||||
let transactionCalls = 0;
|
});
|
||||||
|
context.db.insert(projects).values({ id: "project-1", name: "Test project" }).run();
|
||||||
|
const board = new DistributionBoardRepository(
|
||||||
|
context.db
|
||||||
|
).createWithCircuitListAndDefaultSections("project-1", "UV-01");
|
||||||
|
const [section] = context.db
|
||||||
|
.select()
|
||||||
|
.from(circuitSections)
|
||||||
|
.where(eq(circuitSections.circuitListId, board.id))
|
||||||
|
.limit(1)
|
||||||
|
.all();
|
||||||
|
return { context, circuitListId: board.id, sectionId: section.id };
|
||||||
|
}
|
||||||
|
|
||||||
(db as unknown as { transaction: (callback: (tx: unknown) => unknown) => unknown }).transaction =
|
function migrationInput(circuitListId: string, sectionId: string) {
|
||||||
(callback) => {
|
return {
|
||||||
transactionCalls += 1;
|
circuitListId,
|
||||||
const emptySelectChain = {
|
circuits: [
|
||||||
from() {
|
{
|
||||||
return emptySelectChain;
|
circuit: {
|
||||||
},
|
circuitListId,
|
||||||
where() {
|
sectionId,
|
||||||
return emptySelectChain;
|
equipmentIdentifier: "-1F1",
|
||||||
},
|
displayName: "Legacy lighting",
|
||||||
limit() {
|
sortOrder: 10,
|
||||||
return emptySelectChain;
|
},
|
||||||
},
|
deviceRows: [
|
||||||
all() {
|
|
||||||
return [];
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return callback({
|
|
||||||
select() {
|
|
||||||
return emptySelectChain;
|
|
||||||
},
|
|
||||||
insert(table: unknown) {
|
|
||||||
return {
|
|
||||||
values(values: unknown) {
|
|
||||||
inserts.push({ table, values });
|
|
||||||
return {
|
|
||||||
run() {
|
|
||||||
return { changes: 1 };
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const repository = new LegacyConsumerMigrationRepository();
|
|
||||||
repository.persistCircuitListMigration({
|
|
||||||
circuitListId: "list-1",
|
|
||||||
circuits: [
|
|
||||||
{
|
{
|
||||||
circuit: {
|
legacyConsumerId: "consumer-1",
|
||||||
circuitListId: "list-1",
|
sortOrder: 10,
|
||||||
sectionId: "section-1",
|
name: "Light 1",
|
||||||
equipmentIdentifier: "-1F1",
|
displayName: "Light 1",
|
||||||
displayName: "Legacy lighting",
|
quantity: 1,
|
||||||
sortOrder: 10,
|
powerPerUnit: 0.1,
|
||||||
},
|
simultaneityFactor: 1,
|
||||||
deviceRows: [
|
},
|
||||||
{
|
{
|
||||||
legacyConsumerId: "consumer-1",
|
legacyConsumerId: "consumer-2",
|
||||||
sortOrder: 10,
|
sortOrder: 20,
|
||||||
name: "Light 1",
|
name: "Light 2",
|
||||||
displayName: "Light 1",
|
displayName: "Light 2",
|
||||||
quantity: 1,
|
quantity: 2,
|
||||||
powerPerUnit: 0.1,
|
powerPerUnit: 0.2,
|
||||||
simultaneityFactor: 1,
|
simultaneityFactor: 0.8,
|
||||||
},
|
|
||||||
{
|
|
||||||
legacyConsumerId: "consumer-2",
|
|
||||||
sortOrder: 20,
|
|
||||||
name: "Light 2",
|
|
||||||
displayName: "Light 2",
|
|
||||||
quantity: 2,
|
|
||||||
powerPerUnit: 0.2,
|
|
||||||
simultaneityFactor: 0.8,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
report: {
|
},
|
||||||
legacyConsumerCount: 2,
|
],
|
||||||
createdCircuitCount: 1,
|
report: {
|
||||||
createdDeviceRowCount: 2,
|
legacyConsumerCount: 2,
|
||||||
duplicateGroupedCount: 1,
|
createdCircuitCount: 1,
|
||||||
generatedIdentifierCount: 0,
|
createdDeviceRowCount: 2,
|
||||||
unassignedRowCount: 0,
|
duplicateGroupedCount: 1,
|
||||||
warningsJson: "[]",
|
generatedIdentifierCount: 0,
|
||||||
generatedIdentifiersJson: "[]",
|
unassignedRowCount: 0,
|
||||||
duplicateGroupsJson: "[{\"normalizedCircuitNumber\":\"-1F1\",\"count\":2}]",
|
warningsJson: "[]",
|
||||||
},
|
generatedIdentifiersJson: "[]",
|
||||||
});
|
duplicateGroupsJson:
|
||||||
|
"[{\"normalizedCircuitNumber\":\"-1F1\",\"count\":2}]",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
assert.equal(transactionCalls, 1);
|
function assertNoMigrationWrites(context: DatabaseContext) {
|
||||||
|
assert.equal(context.db.select().from(circuits).all().length, 0);
|
||||||
|
assert.equal(context.db.select().from(circuitDeviceRows).all().length, 0);
|
||||||
|
assert.equal(
|
||||||
|
context.db.select().from(legacyConsumerCircuitMigrations).all().length,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
context.db.select().from(legacyConsumerMigrationReports).all().length,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("legacy consumer migration repository", () => {
|
||||||
|
it("commits circuits, rows, mappings and report together", () => {
|
||||||
|
const { context, circuitListId, sectionId } = createTestDatabase();
|
||||||
|
try {
|
||||||
|
const repository = new LegacyConsumerMigrationRepository(context.db);
|
||||||
|
|
||||||
|
repository.persistCircuitListMigration(
|
||||||
|
migrationInput(circuitListId, sectionId)
|
||||||
|
);
|
||||||
|
|
||||||
|
const persistedCircuits = context.db.select().from(circuits).all();
|
||||||
|
const persistedRows = context.db
|
||||||
|
.select()
|
||||||
|
.from(circuitDeviceRows)
|
||||||
|
.all()
|
||||||
|
.sort((left, right) => left.sortOrder - right.sortOrder);
|
||||||
|
const persistedMappings = context.db
|
||||||
|
.select()
|
||||||
|
.from(legacyConsumerCircuitMigrations)
|
||||||
|
.all()
|
||||||
|
.sort((left, right) => left.consumerId.localeCompare(right.consumerId));
|
||||||
|
const persistedReports = context.db
|
||||||
|
.select()
|
||||||
|
.from(legacyConsumerMigrationReports)
|
||||||
|
.all();
|
||||||
|
|
||||||
|
assert.equal(persistedCircuits.length, 1);
|
||||||
|
assert.equal(persistedRows.length, 2);
|
||||||
|
assert.equal(persistedMappings.length, 2);
|
||||||
|
assert.equal(persistedReports.length, 1);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
inserts.map((entry) => entry.table),
|
persistedMappings.map((mapping) => [
|
||||||
|
mapping.consumerId,
|
||||||
|
mapping.circuitId,
|
||||||
|
mapping.circuitDeviceRowId,
|
||||||
|
mapping.circuitListId,
|
||||||
|
]),
|
||||||
[
|
[
|
||||||
circuits,
|
[
|
||||||
circuitDeviceRows,
|
"consumer-1",
|
||||||
legacyConsumerCircuitMigrations,
|
persistedCircuits[0].id,
|
||||||
circuitDeviceRows,
|
persistedRows[0].id,
|
||||||
legacyConsumerCircuitMigrations,
|
circuitListId,
|
||||||
legacyConsumerMigrationReports,
|
],
|
||||||
|
[
|
||||||
|
"consumer-2",
|
||||||
|
persistedCircuits[0].id,
|
||||||
|
persistedRows[1].id,
|
||||||
|
circuitListId,
|
||||||
|
],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
assert.equal(persistedReports[0].createdCircuitCount, 1);
|
||||||
const circuit = inserts[0].values as { id: string };
|
assert.equal(persistedReports[0].createdDeviceRowCount, 2);
|
||||||
const firstRow = inserts[1].values as { id: string; circuitId: string; legacyConsumerId: string };
|
|
||||||
const firstMapping = inserts[2].values as {
|
|
||||||
consumerId: string;
|
|
||||||
circuitId: string;
|
|
||||||
circuitDeviceRowId: string;
|
|
||||||
circuitListId: string;
|
|
||||||
};
|
|
||||||
assert.equal(firstRow.circuitId, circuit.id);
|
|
||||||
assert.equal(firstRow.legacyConsumerId, "consumer-1");
|
|
||||||
assert.deepEqual(
|
|
||||||
{
|
|
||||||
consumerId: firstMapping.consumerId,
|
|
||||||
circuitId: firstMapping.circuitId,
|
|
||||||
circuitDeviceRowId: firstMapping.circuitDeviceRowId,
|
|
||||||
circuitListId: firstMapping.circuitListId,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
consumerId: "consumer-1",
|
|
||||||
circuitId: circuit.id,
|
|
||||||
circuitDeviceRowId: firstRow.id,
|
|
||||||
circuitListId: "list-1",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} finally {
|
} finally {
|
||||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
context.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rolls back all migrated entities when the final report write fails", () => {
|
||||||
|
const { context, circuitListId, sectionId } = createTestDatabase();
|
||||||
|
try {
|
||||||
|
context.sqlite.exec(`
|
||||||
|
CREATE TRIGGER fail_migration_report
|
||||||
|
BEFORE INSERT ON legacy_consumer_migration_reports
|
||||||
|
BEGIN
|
||||||
|
SELECT RAISE(ABORT, 'forced migration report failure');
|
||||||
|
END;
|
||||||
|
`);
|
||||||
|
const repository = new LegacyConsumerMigrationRepository(context.db);
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() =>
|
||||||
|
repository.persistCircuitListMigration(
|
||||||
|
migrationInput(circuitListId, sectionId)
|
||||||
|
),
|
||||||
|
/forced migration report failure/
|
||||||
|
);
|
||||||
|
|
||||||
|
assertNoMigrationWrites(context);
|
||||||
|
} finally {
|
||||||
|
context.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user