feat(M01-B): 增加迁移执行器与旧库只读兼容层
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
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.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.');
|
||||
Reference in New Issue
Block a user