70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
|
import { BaseMigration } from './base-migration.ts';
|
||
|
import { MigrationV1ToV2 } from './migration-v1-to-v2.ts';
|
||
|
import { MigrationV3ToV4 } from './migration-v3-to-v4.ts';
|
||
|
import { logger } from '../logger.ts';
|
||
|
|
||
|
/**
|
||
|
* Migration runner
|
||
|
*
|
||
|
* Discovers all available migrations, sorts them by order,
|
||
|
* and runs applicable migrations in sequence.
|
||
|
*/
|
||
|
export class MigrationRunner {
|
||
|
private migrations: BaseMigration[];
|
||
|
|
||
|
constructor() {
|
||
|
// Register all migrations here
|
||
|
this.migrations = [
|
||
|
new MigrationV1ToV2(),
|
||
|
new MigrationV3ToV4(),
|
||
|
// Add future migrations here (v4→v5, v5→v6, etc.)
|
||
|
];
|
||
|
|
||
|
// Sort by order to ensure they run in sequence
|
||
|
this.migrations.sort((a, b) => a.order - b.order);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Run all applicable migrations on the config
|
||
|
*
|
||
|
* @param config - Raw configuration object to migrate
|
||
|
* @returns Migrated configuration and whether migrations ran
|
||
|
*/
|
||
|
async run(config: any): Promise<{ config: any; migrated: boolean }> {
|
||
|
let currentConfig = config;
|
||
|
let anyMigrationsRan = false;
|
||
|
|
||
|
logger.dim('Checking for required config migrations...');
|
||
|
|
||
|
for (const migration of this.migrations) {
|
||
|
const shouldRun = await migration.shouldRun(currentConfig);
|
||
|
|
||
|
if (shouldRun) {
|
||
|
logger.info(`Running ${migration.getName()}...`);
|
||
|
currentConfig = await migration.migrate(currentConfig);
|
||
|
anyMigrationsRan = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (anyMigrationsRan) {
|
||
|
logger.success('Configuration migrations complete');
|
||
|
} else {
|
||
|
logger.dim('No migrations needed');
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
config: currentConfig,
|
||
|
migrated: anyMigrationsRan,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get all registered migrations
|
||
|
*
|
||
|
* @returns Array of all migrations sorted by order
|
||
|
*/
|
||
|
getMigrations(): BaseMigration[] {
|
||
|
return [...this.migrations];
|
||
|
}
|
||
|
}
|