feat(migrations): add migration system for v3→v4 config format
- Create abstract BaseMigration class with order, shouldRun(), migrate() - Add MigrationRunner to execute migrations in order - Add Migration v1→v2 (snmp → upsDevices) - Add Migration v3→v4 (upsList → upsDevices) - Update INupstConfig with version field - Update loadConfig() to run migrations automatically - Update saveConfig() to ensure version field and remove legacy fields - Update Docker test scripts to use real UPS data from .nogit/env.json - Remove colors.bright (not available in @std/fmt/colors) Config migrations allow users to jump versions (e.g., v1→v4) with all intermediate migrations running automatically. Version field tracks config format for future migrations.
This commit is contained in:
45
ts/migrations/migration-v3-to-v4.ts
Normal file
45
ts/migrations/migration-v3-to-v4.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { BaseMigration } from './base-migration.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
|
||||
/**
|
||||
* Migration from v3 (upsList) to v4 (upsDevices)
|
||||
*
|
||||
* Detects v3 format:
|
||||
* {
|
||||
* upsList: [ ... ],
|
||||
* groups: [ ... ],
|
||||
* checkInterval: 30000
|
||||
* }
|
||||
*
|
||||
* Converts to:
|
||||
* {
|
||||
* version: "4.0",
|
||||
* upsDevices: [ ... ], // renamed from upsList
|
||||
* groups: [ ... ],
|
||||
* checkInterval: 30000
|
||||
* }
|
||||
*/
|
||||
export class MigrationV3ToV4 extends BaseMigration {
|
||||
readonly order = 4;
|
||||
readonly fromVersion = '3.x';
|
||||
readonly toVersion = '4.0';
|
||||
|
||||
async shouldRun(config: any): Promise<boolean> {
|
||||
// V3 format has upsList instead of upsDevices
|
||||
return !!config.upsList && !config.upsDevices;
|
||||
}
|
||||
|
||||
async migrate(config: any): Promise<any> {
|
||||
logger.info(`${this.getName()}: Renaming upsList to upsDevices...`);
|
||||
|
||||
const migrated = {
|
||||
version: this.toVersion,
|
||||
upsDevices: config.upsList, // Rename upsList → upsDevices
|
||||
groups: config.groups || [],
|
||||
checkInterval: config.checkInterval || 30000,
|
||||
};
|
||||
|
||||
logger.success(`${this.getName()}: Migration complete`);
|
||||
return migrated;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user