295 lines
11 KiB
TypeScript
295 lines
11 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',
|
|
'database/migrations/2026062014_m04d_order_shares.up.sql',
|
|
'database/migrations/2026062015_m05a_payment_domain.up.sql',
|
|
'database/migrations/2026062216_m05b_wechat_refunds.up.sql',
|
|
'database/migrations/2026062217_m05c_third_party.up.sql',
|
|
'database/migrations/2026062218_m05d_profit_sharing.up.sql',
|
|
'database/migrations/2026062219_m06b_device_topology.up.sql',
|
|
'database/migrations/2026062220_m06c_iot_messages.up.sql',
|
|
'database/migrations/2026062421_m07a_wallet_ledger.up.sql',
|
|
'database/migrations/2026062422_m07b_recharge_plans.up.sql',
|
|
'database/migrations/2026062423_m07c_benefits.up.sql',
|
|
'database/migrations/2026062524_m08a_recharge_wechat.up.sql',
|
|
'database/migrations/2026062525_m08b_cleaner_tasks.up.sql',
|
|
'database/migrations/2026062626_m08b_cleaning_settlements.up.sql',
|
|
'database/migrations/2026062627_m08b_cleaning_collaboration.up.sql',
|
|
'database/migrations/2026062728_m08b_cleaning_payouts.up.sql',
|
|
'database/migrations/2026062729_m08b_cleaning_transfer_state.up.sql',
|
|
'database/migrations/2026081001_m08c_staff_management_access.up.sql',
|
|
'database/migrations/2026081002_m08d_content_asset_scope.up.sql',
|
|
'database/migrations/2026081003_m08d_franchise_leads.up.sql',
|
|
'database/migrations/2026081004_m08d_admin_password_auth.up.sql',
|
|
'database/migrations/2026081005_m09b_cleaning_rules.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',
|
|
'database/migrations/2026062014_m04d_order_shares.verify.sql',
|
|
'database/migrations/2026062015_m05a_payment_domain.verify.sql',
|
|
'database/migrations/2026062216_m05b_wechat_refunds.verify.sql',
|
|
'database/migrations/2026062217_m05c_third_party.verify.sql',
|
|
'database/migrations/2026062218_m05d_profit_sharing.verify.sql',
|
|
'database/migrations/2026062219_m06b_device_topology.verify.sql',
|
|
'database/migrations/2026062220_m06c_iot_messages.verify.sql',
|
|
'database/migrations/2026062421_m07a_wallet_ledger.verify.sql',
|
|
'database/migrations/2026062422_m07b_recharge_plans.verify.sql',
|
|
'database/migrations/2026062423_m07c_benefits.verify.sql',
|
|
'database/migrations/2026062524_m08a_recharge_wechat.verify.sql',
|
|
'database/migrations/2026062525_m08b_cleaner_tasks.verify.sql',
|
|
'database/migrations/2026062626_m08b_cleaning_settlements.verify.sql',
|
|
'database/migrations/2026062627_m08b_cleaning_collaboration.verify.sql',
|
|
'database/migrations/2026062728_m08b_cleaning_payouts.verify.sql',
|
|
'database/migrations/2026062729_m08b_cleaning_transfer_state.verify.sql',
|
|
'database/migrations/2026081001_m08c_staff_management_access.verify.sql',
|
|
'database/migrations/2026081002_m08d_content_asset_scope.verify.sql',
|
|
'database/migrations/2026081003_m08d_franchise_leads.verify.sql',
|
|
'database/migrations/2026081004_m08d_admin_password_auth.verify.sql',
|
|
'database/migrations/2026081005_m09b_cleaning_rules.verify.sql'
|
|
],
|
|
down: [
|
|
'database/migrations/2026081005_m09b_cleaning_rules.down.sql',
|
|
'database/migrations/2026081004_m08d_admin_password_auth.down.sql',
|
|
'database/migrations/2026081003_m08d_franchise_leads.down.sql',
|
|
'database/migrations/2026081002_m08d_content_asset_scope.down.sql',
|
|
'database/migrations/2026081001_m08c_staff_management_access.down.sql',
|
|
'database/migrations/2026062729_m08b_cleaning_transfer_state.down.sql',
|
|
'database/migrations/2026062728_m08b_cleaning_payouts.down.sql',
|
|
'database/migrations/2026062627_m08b_cleaning_collaboration.down.sql',
|
|
'database/migrations/2026062626_m08b_cleaning_settlements.down.sql',
|
|
'database/migrations/2026062525_m08b_cleaner_tasks.down.sql',
|
|
'database/migrations/2026062524_m08a_recharge_wechat.down.sql',
|
|
'database/migrations/2026062423_m07c_benefits.down.sql',
|
|
'database/migrations/2026062422_m07b_recharge_plans.down.sql',
|
|
'database/migrations/2026062421_m07a_wallet_ledger.down.sql',
|
|
'database/migrations/2026062220_m06c_iot_messages.down.sql',
|
|
'database/migrations/2026062219_m06b_device_topology.down.sql',
|
|
'database/migrations/2026062218_m05d_profit_sharing.down.sql',
|
|
'database/migrations/2026062217_m05c_third_party.down.sql',
|
|
'database/migrations/2026062216_m05b_wechat_refunds.down.sql',
|
|
'database/migrations/2026062015_m05a_payment_domain.down.sql',
|
|
'database/migrations/2026062014_m04d_order_shares.down.sql',
|
|
'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,
|
|
1, 8, 3, 1,
|
|
5, 8, 4, 1,
|
|
1, 5, 3, 1,
|
|
5, 6, 1,
|
|
3, 7, 5, 1,
|
|
5, 8, 5, 2, 1,
|
|
3, 3, 9, 1,
|
|
1, 1, 1, 4, 7, 1,
|
|
1, 1, 7, 7, 1,
|
|
5, 10, 7, 3, 1,
|
|
5, 2,
|
|
2, 8, 4, 3, 1,
|
|
2, 9, 5, 2, 1,
|
|
1, 7, 5, 1,
|
|
4, 1, 1, 1,
|
|
2, 1, 1,
|
|
1, 1, 1,
|
|
1, 2, 3, 1,
|
|
1, 2, 3, 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 };
|
|
}
|