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:
2025-10-19 20:41:09 +00:00
parent 49f7a7da8b
commit 016681b77b
9 changed files with 310 additions and 64 deletions

View 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;
}
}