Files
d8d-admin-mobile-starter-pu…/server/migrations.ts

50 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
/**
* 异步加载所有迁移文件
* @returns Promise<MigrationLiveDefinition[]> 返回加载并排序后的迁移数组
*/
export async function loadMigrations(): Promise<MigrationLiveDefinition[]> {
const migrations: MigrationLiveDefinition[] = [];
try {
// 读取并加载所有迁移文件
const migrationsDir = import.meta.dirname + '/migrations';
for await (const entry of Deno.readDir(migrationsDir)) {
if (!entry.isFile || !entry.name.endsWith('.ts')) continue;
// 匹配文件名格式数字前缀_描述.ts
const match = entry.name.match(/^(\d+)_(.+)\.ts$/);
if (!match) continue;
const [_, prefix, name] = match;
try {
const migration = await import(`${migrationsDir}/${entry.name}`);
// 确保导出的迁移对象有效
if (migration?.default?.name && migration?.default?.up && migration?.default?.down) {
migrations.push(migration.default);
console.log(`✅ Loaded migration: ${entry.name}`);
} else {
console.warn(`⚠️ Invalid migration format in ${entry.name}`);
}
} catch (err) {
console.error(`❌ Failed to load migration ${entry.name}:`, err);
}
}
// 按数字前缀排序
migrations.sort((a, b) => {
const aNum = parseInt(a.name.match(/^(\d+)_/)?.[1] || '0');
const bNum = parseInt(b.name.match(/^(\d+)_/)?.[1] || '0');
return aNum - bNum;
});
console.log(`🎉 Successfully loaded ${migrations.length} migrations`);
return migrations;
} catch (err) {
console.error('❌ Failed to load migrations:', err);
throw err;
}
}