Remove project device mirror fields
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { describe, it } from "node:test";
|
||||
import Database from "better-sqlite3";
|
||||
|
||||
describe("project device canonical-field migration", () => {
|
||||
it("preserves canonical values while removing transitional mirror columns", () => {
|
||||
const sqlite = new Database(":memory:");
|
||||
try {
|
||||
sqlite.exec(`
|
||||
CREATE TABLE project_devices (
|
||||
id text PRIMARY KEY NOT NULL,
|
||||
power_per_unit real NOT NULL,
|
||||
simultaneity_factor real NOT NULL,
|
||||
phase_type text NOT NULL,
|
||||
cos_phi real,
|
||||
remark text,
|
||||
voltage_v real,
|
||||
installed_power_per_unit_kw real NOT NULL,
|
||||
demand_factor real NOT NULL,
|
||||
phase_count integer,
|
||||
power_factor real,
|
||||
note text
|
||||
);
|
||||
INSERT INTO project_devices VALUES (
|
||||
'device-1',
|
||||
0.25,
|
||||
0.8,
|
||||
'three_phase',
|
||||
0.95,
|
||||
'Canonical remark',
|
||||
400,
|
||||
0.25,
|
||||
0.8,
|
||||
3,
|
||||
0.95,
|
||||
'Canonical remark'
|
||||
);
|
||||
`);
|
||||
|
||||
const migrationSql = fs.readFileSync(
|
||||
path.resolve(
|
||||
"src",
|
||||
"db",
|
||||
"migrations",
|
||||
"0011_project_device_canonical_fields.sql"
|
||||
),
|
||||
"utf8"
|
||||
);
|
||||
for (const statement of migrationSql.split("--> statement-breakpoint")) {
|
||||
if (statement.trim()) {
|
||||
sqlite.exec(statement);
|
||||
}
|
||||
}
|
||||
|
||||
const columns = (
|
||||
sqlite.prepare("PRAGMA table_info(project_devices)").all() as Array<{
|
||||
name: string;
|
||||
}>
|
||||
).map((column) => column.name);
|
||||
assert.deepEqual(columns, [
|
||||
"id",
|
||||
"power_per_unit",
|
||||
"simultaneity_factor",
|
||||
"phase_type",
|
||||
"cos_phi",
|
||||
"remark",
|
||||
"voltage_v",
|
||||
]);
|
||||
assert.deepEqual(
|
||||
sqlite
|
||||
.prepare(
|
||||
`SELECT
|
||||
power_per_unit AS powerPerUnit,
|
||||
simultaneity_factor AS simultaneityFactor,
|
||||
phase_type AS phaseType,
|
||||
cos_phi AS cosPhi,
|
||||
remark,
|
||||
voltage_v AS voltageV
|
||||
FROM project_devices
|
||||
WHERE id = ?`
|
||||
)
|
||||
.get("device-1"),
|
||||
{
|
||||
powerPerUnit: 0.25,
|
||||
simultaneityFactor: 0.8,
|
||||
phaseType: "three_phase",
|
||||
cosPhi: 0.95,
|
||||
remark: "Canonical remark",
|
||||
voltageV: 400,
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
sqlite.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user