73 lines
2.5 KiB
JavaScript
73 lines
2.5 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.file, /2026062013_m04c_order_adjustments\.up\.sql/);
|
|
assert.match(plan.file, /2026062014_m04d_order_shares\.up\.sql/);
|
|
assert.match(plan.file, /2026062015_m05a_payment_domain\.up\.sql/);
|
|
assert.match(plan.file, /2026062216_m05b_wechat_refunds\.up\.sql/);
|
|
assert.match(plan.file, /2026062217_m05c_third_party\.up\.sql/);
|
|
assert.match(plan.file, /2026062218_m05d_profit_sharing\.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.');
|