-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2f73f3
commit 101a1db
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/main/src/backend/configManager/configMigration/configMigrator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { z } from 'zod'; | ||
import { type Config } from '../../commonTypes'; | ||
import { isOriginalConfig } from './versions/original'; | ||
import { v1ConfigSchema } from './versions/v1'; | ||
|
||
const latestConfigSchema = v1ConfigSchema; | ||
|
||
// migrations[n] should be a function that converts version n to version n+1 | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const migrations: Record<number, (config: any) => any> = {}; | ||
|
||
export function migrateConfig(config: unknown): Config { | ||
let currentConfig = config; | ||
// original config does not have version key and must be handled separately | ||
if (isOriginalConfig(config)) { | ||
return config as Config; | ||
} | ||
let currentVersion = getConfigVersion(currentConfig); | ||
|
||
while (migrations[currentVersion]) { | ||
currentConfig = migrations[currentVersion](currentConfig); | ||
currentVersion = getConfigVersion(currentConfig); | ||
} | ||
|
||
return latestConfigSchema.parse(currentConfig) as Config; | ||
} | ||
|
||
function getConfigVersion(config: unknown): keyof typeof migrations { | ||
const versionSchema = z.object({ version: z.number().int().positive() }); | ||
return versionSchema.parse(config).version; | ||
} |