diff --git a/src/schemas/settings/README.md b/src/schemas/settings/README.md new file mode 100644 index 00000000..dcd0af1b --- /dev/null +++ b/src/schemas/settings/README.md @@ -0,0 +1,27 @@ +# Settings migration + +Migrations are needed when changing the structure of existing settings file such as renaming or deleting keys. + +If you're simply adding new options to the settings file, a migration is not needed. In this case, extend the latest schema available. + +## Creating a new migration + +For when a new migration is needed: + +1. Create a directory for the new version (e.g. `/v2`). +2. Declare the new schema with appropriate changes. +3. In `/v2/index.ts`, implement a `migrate` function that takes a `v2` schema and returns a `v3` schema. +4. Update `/schemas/settings/index.ts` to use the new version: + +```ts +function migrate(settings: z.infer) { + +case '2.0': + return migrate(v2.migrate(settings)) + +} +``` + +5. Update `src/types/settings.ts` to use types from the new version. +6. Update the default settings in `src/settings.ts` according to the new schema. +7. Make changes to the remaining implementation to use the new schema. diff --git a/src/schemas/settings/index.test.ts b/src/schemas/settings/index.test.ts new file mode 100644 index 00000000..8bb9fe00 --- /dev/null +++ b/src/schemas/settings/index.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest' +import { migrate } from '.' +import * as v1 from './v1' + +describe('Settings migration', () => { + it('should migrate from v1 to latest', () => { + const v1Settings: v1.AppSettings = { + version: '1.0', + proxy: { + mode: 'regular', + port: 6000, + automaticallyFindPort: true, + }, + recorder: { + detectBrowserPath: true, + }, + windowState: { + width: 1200, + height: 800, + x: 0, + y: 0, + isMaximized: true, + }, + usageReport: { + enabled: true, + }, + appearance: { + theme: 'system', + }, + } + + expect(migrate(v1Settings).version).toBe('2.0') + }) +}) diff --git a/src/schemas/settings/index.ts b/src/schemas/settings/index.ts index b9ebbe85..4ac76f2f 100644 --- a/src/schemas/settings/index.ts +++ b/src/schemas/settings/index.ts @@ -8,7 +8,7 @@ const AnySettingSchema = z.discriminatedUnion('version', [ v2.AppSettingsSchema, ]) -function migrate(settings: z.infer) { +export function migrate(settings: z.infer) { switch (settings.version) { case '1.0': return migrate(v1.migrate(settings)) diff --git a/src/schemas/settings/v1/index.ts b/src/schemas/settings/v1/index.ts index de2d3180..6d04b894 100644 --- a/src/schemas/settings/v1/index.ts +++ b/src/schemas/settings/v1/index.ts @@ -121,3 +121,5 @@ export function migrate( appearance: settings.appearance, } } + +export type AppSettings = z.infer