migrations.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import {
  3. EnableStatus, DeleteStatus,
  4. AuditStatus, ThemeMode, FontSize, CompactMode,
  5. SystemSettingKey,
  6. SystemSettingGroup,
  7. ALLOWED_FILE_TYPES,
  8. } from '../client/share/types.ts';
  9. // 动态加载迁移文件
  10. const migrations: MigrationLiveDefinition[] = [];
  11. try {
  12. // 读取并加载所有迁移文件
  13. const migrationsDir = './server/migrations';
  14. for await (const entry of Deno.readDir(migrationsDir)) {
  15. if (!entry.isFile || !entry.name.endsWith('.ts')) continue;
  16. // 匹配文件名格式:数字前缀_描述.ts
  17. const match = entry.name.match(/^(\d+)_(.+)\.ts$/);
  18. if (!match) continue;
  19. const [_, prefix, name] = match;
  20. try {
  21. const migration = await import(`${migrationsDir}/${entry.name}`);
  22. // 确保导出的迁移对象有效
  23. if (migration?.default?.name && migration?.default?.up && migration?.default?.down) {
  24. migrations.push(migration.default);
  25. console.log(`✅ Loaded migration: ${entry.name}`);
  26. } else {
  27. console.warn(`⚠️ Invalid migration format in ${entry.name}`);
  28. }
  29. } catch (err) {
  30. console.error(`❌ Failed to load migration ${entry.name}:`, err);
  31. }
  32. }
  33. // 按数字前缀排序
  34. migrations.sort((a, b) => {
  35. const aNum = parseInt(a.name.match(/^(\d+)_/)?.[1] || '0');
  36. const bNum = parseInt(b.name.match(/^(\d+)_/)?.[1] || '0');
  37. return aNum - bNum;
  38. });
  39. console.log(`🎉 Successfully loaded ${migrations.length} migrations`);
  40. } catch (err) {
  41. console.error('❌ Failed to load migrations:', err);
  42. }
  43. // 导出所有迁移
  44. export { migrations };