Files
qipai/backend/tests/migration-runner.test.mjs
T

67 lines
2.1 KiB
JavaScript

import assert from 'node:assert/strict';
import {
executeMigrationPlan,
loadMigrationPlan,
splitSqlStatements
} from '../dist/db/migration-runner.js';
assert.deepEqual(
splitSqlStatements("SELECT 'a;b'; -- comment\nSELECT `semi;colon`;"),
["SELECT 'a;b'", 'SELECT `semi;colon`']
);
const plan = await loadMigrationPlan('up');
assert.equal(plan.direction, 'up');
assert.match(plan.file, /2026061601_m01b_core_schema\.up\.sql/);
assert.match(plan.file, /2026061802_m01c_async_tasks\.up\.sql/);
assert.match(plan.file, /2026061803_m02a_tenant_apps\.up\.sql/);
assert.match(plan.file, /2026061804_m02b_wechat_auth\.up\.sql/);
assert.match(plan.file, /2026061805_m02c_rbac\.up\.sql/);
assert.match(plan.file, /2026061806_m02d_user_management\.up\.sql/);
assert.match(plan.file, /2026061807_m03a_store_room_domain\.up\.sql/);
assert.match(plan.file, /2026061808_m03b_decoration_ads_media\.up\.sql/);
assert.match(plan.file, /2026061809_m03c_store_discovery\.up\.sql/);
assert.match(plan.file, /2026061810_m03d_scene_wifi_access\.up\.sql/);
assert.match(plan.file, /2026061811_m04a_pricing_reservations\.up\.sql/);
assert.match(plan.file, /2026062012_m04b_order_state_machine\.up\.sql$/);
assert.match(plan.checksum, /^[a-f0-9]{64}$/);
assert.ok(plan.statements.length >= 11);
const calls = [];
const fakePool = {
async query(sql) {
calls.push(sql);
return [{ affectedRows: 1 }, []];
}
};
const dryRun = await executeMigrationPlan(fakePool, plan, true);
assert.equal(dryRun.executed, false);
assert.equal(calls.length, 0);
const liveResult = await executeMigrationPlan(fakePool, {
direction: 'up',
file: 'test.sql',
checksum: 'test',
statements: ['SELECT 1', 'SELECT 2']
});
assert.equal(liveResult.executed, true);
assert.equal(liveResult.affectedRows, 2);
assert.deepEqual(calls, ['SELECT 1', 'SELECT 2']);
await assert.rejects(
() => executeMigrationPlan({
async query() {
return [[], []];
}
}, {
direction: 'verify',
file: 'verify.sql',
checksum: 'test',
statements: ['SELECT table_name']
}),
/returned fewer than 10 rows/
);
console.log('PASS: migration runner plan, parser and execution contracts are present.');