feat(M01-C): 建立MySQL异步任务基础
This commit is contained in:
@@ -19,10 +19,19 @@ export interface MigrationExecutionResult extends MigrationPlan {
|
||||
}
|
||||
|
||||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../../..');
|
||||
const migrationFiles: Record<MigrationDirection, string> = {
|
||||
up: 'database/migrations/2026061601_m01b_core_schema.up.sql',
|
||||
verify: 'database/migrations/2026061601_m01b_core_schema.verify.sql',
|
||||
down: 'database/migrations/2026061601_m01b_core_schema.down.sql'
|
||||
const migrationFiles: Record<MigrationDirection, readonly string[]> = {
|
||||
up: [
|
||||
'database/migrations/2026061601_m01b_core_schema.up.sql',
|
||||
'database/migrations/2026061802_m01c_async_tasks.up.sql'
|
||||
],
|
||||
verify: [
|
||||
'database/migrations/2026061601_m01b_core_schema.verify.sql',
|
||||
'database/migrations/2026061802_m01c_async_tasks.verify.sql'
|
||||
],
|
||||
down: [
|
||||
'database/migrations/2026061802_m01c_async_tasks.down.sql',
|
||||
'database/migrations/2026061601_m01b_core_schema.down.sql'
|
||||
]
|
||||
};
|
||||
|
||||
export function splitSqlStatements(sql: string): string[] {
|
||||
@@ -110,12 +119,15 @@ export function splitSqlStatements(sql: string): string[] {
|
||||
}
|
||||
|
||||
export async function loadMigrationPlan(direction: MigrationDirection): Promise<MigrationPlan> {
|
||||
const relativeFile = migrationFiles[direction];
|
||||
const sql = await readFile(resolve(repoRoot, relativeFile), 'utf8');
|
||||
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: relativeFile,
|
||||
file: relativeFiles.join(','),
|
||||
checksum: createHash('sha256').update(sql).digest('hex'),
|
||||
statements: splitSqlStatements(sql)
|
||||
};
|
||||
@@ -134,7 +146,7 @@ export async function executeMigrationPlan(
|
||||
for (const [index, statement] of plan.statements.entries()) {
|
||||
const [result] = await pool.query(statement);
|
||||
if (plan.direction === 'verify') {
|
||||
const minimumRows = [10, 26, 1][index] ?? 1;
|
||||
const minimumRows = [10, 26, 1, 2, 5, 1][index] ?? 1;
|
||||
if (!Array.isArray(result) || result.length < minimumRows) {
|
||||
throw new Error(
|
||||
`Migration verification statement ${index + 1} returned fewer than ${minimumRows} rows.`
|
||||
|
||||
Reference in New Issue
Block a user