46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
|
||
|
||
// 动态加载迁移文件
|
||
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`);
|
||
} catch (err) {
|
||
console.error('❌ Failed to load migrations:', err);
|
||
}
|
||
|
||
// 导出所有迁移
|
||
export { migrations }; |