|
@@ -1,46 +1,50 @@
|
|
|
import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
|
|
import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
|
|
|
|
|
|
|
|
-// 动态加载迁移文件
|
|
|
|
|
-const migrations: MigrationLiveDefinition[] = [];
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 异步加载所有迁移文件
|
|
|
|
|
+ * @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;
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 读取并加载所有迁移文件
|
|
|
|
|
+ const migrationsDir = import.meta.dirname + '/migrations';
|
|
|
|
|
|
|
|
- // 匹配文件名格式:数字前缀_描述.ts
|
|
|
|
|
- const match = entry.name.match(/^(\d+)_(.+)\.ts$/);
|
|
|
|
|
- if (!match) continue;
|
|
|
|
|
-
|
|
|
|
|
- const [_, prefix, name] = match;
|
|
|
|
|
- try {
|
|
|
|
|
- const migration = await import(`${migrationsDir}/${entry.name}`);
|
|
|
|
|
|
|
+ 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;
|
|
|
|
|
|
|
|
- // 确保导出的迁移对象有效
|
|
|
|
|
- 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}`);
|
|
|
|
|
|
|
+ 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);
|
|
|
}
|
|
}
|
|
|
- } 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;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ // 按数字前缀排序
|
|
|
|
|
+ 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 };
|
|
|
|
|
|
|
+ console.log(`🎉 Successfully loaded ${migrations.length} migrations`);
|
|
|
|
|
+ return migrations;
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ console.error('❌ Failed to load migrations:', err);
|
|
|
|
|
+ throw err;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|