Make legacy migration atomic
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { db } from "../src/db/client.js";
|
||||
import { LegacyConsumerMigrationRepository } from "../src/db/repositories/legacy-consumer-migration.repository.js";
|
||||
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
||||
import { circuits } from "../src/db/schema/circuits.js";
|
||||
import { legacyConsumerCircuitMigrations } from "../src/db/schema/legacy-consumer-circuit-migrations.js";
|
||||
import { legacyConsumerMigrationReports } from "../src/db/schema/legacy-consumer-migration-report.js";
|
||||
|
||||
describe("legacy consumer migration repository", () => {
|
||||
it("writes circuits, rows, mappings and report in one synchronous transaction", () => {
|
||||
const originalTransaction = db.transaction;
|
||||
const inserts: Array<{ table: unknown; values: unknown }> = [];
|
||||
let transactionCalls = 0;
|
||||
|
||||
(db as unknown as { transaction: (callback: (tx: unknown) => unknown) => unknown }).transaction =
|
||||
(callback) => {
|
||||
transactionCalls += 1;
|
||||
const emptySelectChain = {
|
||||
from() {
|
||||
return emptySelectChain;
|
||||
},
|
||||
where() {
|
||||
return emptySelectChain;
|
||||
},
|
||||
limit() {
|
||||
return emptySelectChain;
|
||||
},
|
||||
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: {
|
||||
circuitListId: "list-1",
|
||||
sectionId: "section-1",
|
||||
equipmentIdentifier: "-1F1",
|
||||
displayName: "Legacy lighting",
|
||||
sortOrder: 10,
|
||||
},
|
||||
deviceRows: [
|
||||
{
|
||||
legacyConsumerId: "consumer-1",
|
||||
sortOrder: 10,
|
||||
name: "Light 1",
|
||||
displayName: "Light 1",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0.1,
|
||||
simultaneityFactor: 1,
|
||||
},
|
||||
{
|
||||
legacyConsumerId: "consumer-2",
|
||||
sortOrder: 20,
|
||||
name: "Light 2",
|
||||
displayName: "Light 2",
|
||||
quantity: 2,
|
||||
powerPerUnit: 0.2,
|
||||
simultaneityFactor: 0.8,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
report: {
|
||||
legacyConsumerCount: 2,
|
||||
createdCircuitCount: 1,
|
||||
createdDeviceRowCount: 2,
|
||||
duplicateGroupedCount: 1,
|
||||
generatedIdentifierCount: 0,
|
||||
unassignedRowCount: 0,
|
||||
warningsJson: "[]",
|
||||
generatedIdentifiersJson: "[]",
|
||||
duplicateGroupsJson: "[{\"normalizedCircuitNumber\":\"-1F1\",\"count\":2}]",
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(transactionCalls, 1);
|
||||
assert.deepEqual(
|
||||
inserts.map((entry) => entry.table),
|
||||
[
|
||||
circuits,
|
||||
circuitDeviceRows,
|
||||
legacyConsumerCircuitMigrations,
|
||||
circuitDeviceRows,
|
||||
legacyConsumerCircuitMigrations,
|
||||
legacyConsumerMigrationReports,
|
||||
]
|
||||
);
|
||||
|
||||
const circuit = inserts[0].values as { id: string };
|
||||
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 {
|
||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user