-
Notifications
You must be signed in to change notification settings - Fork 2
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
7bb06aa
commit 0415013
Showing
8 changed files
with
147 additions
and
7 deletions.
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
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,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<typeof AnySettingSchema>) { | ||
|
||
case '2.0': | ||
return migrate(v2.migrate(settings)) | ||
|
||
} | ||
``` | ||
|
||
5. Export types from the new version in `/schemas/settings/index.ts`. | ||
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. |
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,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') | ||
}) | ||
}) |
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,30 @@ | ||
import { z } from 'zod' | ||
import * as v1 from './v1' | ||
import * as v2 from './v2' | ||
import { exhaustive } from '../../utils/typescript' | ||
|
||
const AnySettingSchema = z.discriminatedUnion('version', [ | ||
v1.AppSettingsSchema, | ||
v2.AppSettingsSchema, | ||
]) | ||
|
||
export function migrate(settings: z.infer<typeof AnySettingSchema>) { | ||
switch (settings.version) { | ||
case '1.0': | ||
return migrate(v1.migrate(settings)) | ||
case '2.0': | ||
return settings | ||
default: | ||
return exhaustive(settings) | ||
} | ||
} | ||
|
||
export const AppSettingsSchema = AnySettingSchema.transform(migrate) | ||
|
||
export { | ||
AppearanceSchema, | ||
ProxySettingsSchema, | ||
RecorderSettingsSchema, | ||
UsageReportSettingsSchema, | ||
WindowStateSchema, | ||
} from './v2' |
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
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,32 @@ | ||
import { z } from 'zod' | ||
import { | ||
AppearanceSchema, | ||
ProxySettingsSchema, | ||
RecorderSettingsSchema, | ||
UsageReportSettingsSchema, | ||
WindowStateSchema, | ||
} from '../v1' | ||
|
||
export { | ||
AppearanceSchema, | ||
ProxySettingsSchema, | ||
RecorderSettingsSchema, | ||
UsageReportSettingsSchema, | ||
WindowStateSchema, | ||
} | ||
|
||
export const AppSettingsSchema = z.object({ | ||
version: z.literal('2.0'), | ||
proxy: ProxySettingsSchema, | ||
recorder: RecorderSettingsSchema, | ||
windowState: WindowStateSchema, | ||
usageReport: UsageReportSettingsSchema, | ||
appearance: AppearanceSchema, | ||
}) | ||
|
||
export type AppSettings = z.infer<typeof AppSettingsSchema> | ||
|
||
// TODO: Migrate settings to the next version | ||
export function migrate(settings: z.infer<typeof AppSettingsSchema>) { | ||
return { ...settings } | ||
} |
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
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