213 lines
6.7 KiB
TypeScript
213 lines
6.7 KiB
TypeScript
import { createHash } from 'node:crypto';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { dirname, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import type { MySqlPool } from './mysql.js';
|
|
|
|
export type MigrationDirection = 'up' | 'verify' | 'down';
|
|
|
|
export interface MigrationPlan {
|
|
direction: MigrationDirection;
|
|
file: string;
|
|
checksum: string;
|
|
statements: readonly string[];
|
|
}
|
|
|
|
export interface MigrationExecutionResult extends MigrationPlan {
|
|
executed: boolean;
|
|
affectedRows: number;
|
|
}
|
|
|
|
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../../..');
|
|
const migrationFiles: Record<MigrationDirection, readonly string[]> = {
|
|
up: [
|
|
'database/migrations/2026061601_m01b_core_schema.up.sql',
|
|
'database/migrations/2026061802_m01c_async_tasks.up.sql',
|
|
'database/migrations/2026061803_m02a_tenant_apps.up.sql',
|
|
'database/migrations/2026061804_m02b_wechat_auth.up.sql',
|
|
'database/migrations/2026061805_m02c_rbac.up.sql',
|
|
'database/migrations/2026061806_m02d_user_management.up.sql',
|
|
'database/migrations/2026061807_m03a_store_room_domain.up.sql',
|
|
'database/migrations/2026061808_m03b_decoration_ads_media.up.sql',
|
|
'database/migrations/2026061809_m03c_store_discovery.up.sql',
|
|
'database/migrations/2026061810_m03d_scene_wifi_access.up.sql',
|
|
'database/migrations/2026061811_m04a_pricing_reservations.up.sql',
|
|
'database/migrations/2026062012_m04b_order_state_machine.up.sql',
|
|
'database/migrations/2026062013_m04c_order_adjustments.up.sql'
|
|
],
|
|
verify: [
|
|
'database/migrations/2026061601_m01b_core_schema.verify.sql',
|
|
'database/migrations/2026061802_m01c_async_tasks.verify.sql',
|
|
'database/migrations/2026061803_m02a_tenant_apps.verify.sql',
|
|
'database/migrations/2026061804_m02b_wechat_auth.verify.sql',
|
|
'database/migrations/2026061805_m02c_rbac.verify.sql',
|
|
'database/migrations/2026061806_m02d_user_management.verify.sql',
|
|
'database/migrations/2026061807_m03a_store_room_domain.verify.sql',
|
|
'database/migrations/2026061808_m03b_decoration_ads_media.verify.sql',
|
|
'database/migrations/2026061809_m03c_store_discovery.verify.sql',
|
|
'database/migrations/2026061810_m03d_scene_wifi_access.verify.sql',
|
|
'database/migrations/2026061811_m04a_pricing_reservations.verify.sql',
|
|
'database/migrations/2026062012_m04b_order_state_machine.verify.sql',
|
|
'database/migrations/2026062013_m04c_order_adjustments.verify.sql'
|
|
],
|
|
down: [
|
|
'database/migrations/2026062013_m04c_order_adjustments.down.sql',
|
|
'database/migrations/2026062012_m04b_order_state_machine.down.sql',
|
|
'database/migrations/2026061811_m04a_pricing_reservations.down.sql',
|
|
'database/migrations/2026061810_m03d_scene_wifi_access.down.sql',
|
|
'database/migrations/2026061809_m03c_store_discovery.down.sql',
|
|
'database/migrations/2026061808_m03b_decoration_ads_media.down.sql',
|
|
'database/migrations/2026061807_m03a_store_room_domain.down.sql',
|
|
'database/migrations/2026061806_m02d_user_management.down.sql',
|
|
'database/migrations/2026061805_m02c_rbac.down.sql',
|
|
'database/migrations/2026061804_m02b_wechat_auth.down.sql',
|
|
'database/migrations/2026061803_m02a_tenant_apps.down.sql',
|
|
'database/migrations/2026061802_m01c_async_tasks.down.sql',
|
|
'database/migrations/2026061601_m01b_core_schema.down.sql'
|
|
]
|
|
};
|
|
|
|
export function splitSqlStatements(sql: string): string[] {
|
|
const statements: string[] = [];
|
|
let current = '';
|
|
let quote: "'" | '"' | '`' | null = null;
|
|
let escaped = false;
|
|
let lineComment = false;
|
|
let blockComment = false;
|
|
|
|
for (let index = 0; index < sql.length; index += 1) {
|
|
const character = sql[index];
|
|
const next = sql[index + 1];
|
|
|
|
if (lineComment) {
|
|
if (character === '\n') {
|
|
lineComment = false;
|
|
current += character;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (blockComment) {
|
|
if (character === '*' && next === '/') {
|
|
blockComment = false;
|
|
index += 1;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!quote && character === '-' && next === '-' && (index === 0 || /\s/.test(sql[index - 1]))) {
|
|
lineComment = true;
|
|
index += 1;
|
|
continue;
|
|
}
|
|
|
|
if (!quote && character === '/' && next === '*') {
|
|
blockComment = true;
|
|
index += 1;
|
|
continue;
|
|
}
|
|
|
|
current += character;
|
|
|
|
if (quote) {
|
|
if (escaped) {
|
|
escaped = false;
|
|
} else if (character === '\\') {
|
|
escaped = true;
|
|
} else if (character === quote) {
|
|
if (next === quote) {
|
|
current += next;
|
|
index += 1;
|
|
} else {
|
|
quote = null;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (character === "'" || character === '"' || character === '`') {
|
|
quote = character;
|
|
continue;
|
|
}
|
|
|
|
if (character === ';') {
|
|
const statement = current.slice(0, -1).trim();
|
|
if (statement) {
|
|
statements.push(statement);
|
|
}
|
|
current = '';
|
|
}
|
|
}
|
|
|
|
const trailing = current.trim();
|
|
if (trailing) {
|
|
statements.push(trailing);
|
|
}
|
|
|
|
if (quote || blockComment) {
|
|
throw new Error('Migration SQL contains an unterminated quote or comment.');
|
|
}
|
|
|
|
return statements;
|
|
}
|
|
|
|
export async function loadMigrationPlan(direction: MigrationDirection): Promise<MigrationPlan> {
|
|
const relativeFiles = migrationFiles[direction];
|
|
const sqlParts = await Promise.all(
|
|
relativeFiles.map((relativeFile) => readFile(resolve(repoRoot, relativeFile), 'utf8'))
|
|
);
|
|
const sql = sqlParts.join('\n');
|
|
|
|
return {
|
|
direction,
|
|
file: relativeFiles.join(','),
|
|
checksum: createHash('sha256').update(sql).digest('hex'),
|
|
statements: splitSqlStatements(sql)
|
|
};
|
|
}
|
|
|
|
export async function executeMigrationPlan(
|
|
pool: Pick<MySqlPool, 'query'>,
|
|
plan: MigrationPlan,
|
|
dryRun = false
|
|
): Promise<MigrationExecutionResult> {
|
|
if (dryRun) {
|
|
return { ...plan, executed: false, affectedRows: 0 };
|
|
}
|
|
|
|
let affectedRows = 0;
|
|
for (const [index, statement] of plan.statements.entries()) {
|
|
const [result] = await pool.query(statement);
|
|
if (plan.direction === 'verify') {
|
|
const minimumRows = [
|
|
10, 26, 1,
|
|
2, 5, 1,
|
|
3, 5, 1,
|
|
3, 7, 1,
|
|
5, 3, 7, 1,
|
|
1, 3, 1,
|
|
3, 6, 13, 1,
|
|
3, 3, 1,
|
|
2, 2, 1,
|
|
3, 3, 1,
|
|
2, 1, 3, 3, 1,
|
|
2, 1, 3, 1,
|
|
2, 2, 1, 2, 1
|
|
][index] ?? 1;
|
|
if (!Array.isArray(result) || result.length < minimumRows) {
|
|
throw new Error(
|
|
`Migration verification statement ${index + 1} returned fewer than ${minimumRows} rows.`
|
|
);
|
|
}
|
|
}
|
|
if (result && typeof result === 'object' && 'affectedRows' in result) {
|
|
const value = Reflect.get(result, 'affectedRows');
|
|
if (typeof value === 'number') {
|
|
affectedRows += value;
|
|
}
|
|
}
|
|
}
|
|
|
|
return { ...plan, executed: true, affectedRows };
|
|
}
|