Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: settings migration #371

Merged
merged 6 commits into from
Dec 4, 2024
Merged

feat: settings migration #371

merged 6 commits into from
Dec 4, 2024

Conversation

cristianoventura
Copy link
Collaborator

@cristianoventura cristianoventura commented Dec 2, 2024

Description

This PR adds migrations to the settings schema, ensuring that changes to its structure remain backward-compatible. This allows the settings to work properly even when the schema is updated.

The same pattern will be applied to the Generator schema on a separate PR.

How to Test

This PR contains one extra commit that is intended to be used for testing purposes and will be reverted before merging.

Before testing this branch, make sure your local settings file (k6-studio-dev.json) contains usageReport.enabled false and appearance.theme as light so you can validate the changes.

"version": "1.0",
...
"usageReport": { "enabled": false },
"appearance": { "theme": "light" }
  • Launch k6 studio from this branch
  • Check that the theme and usage report are still the same
  • Make changes to any other settings in the Settings dialog and check that the previous changes were migrated

Once the settings are saved, the file structure should look like this instead:

"version": "1.0",
...
"general": {
   "usageReport": { "enabled": false },
   "appearance": { "theme": "light" }
}

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (npm run lint) and all checks pass.
  • I have run tests locally (npm test) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Screenshots (if appropriate):

Related PR(s)/Issue(s)

Resolves #265

@cristianoventura cristianoventura self-assigned this Dec 2, 2024
src/main.ts Outdated
@@ -198,9 +198,9 @@ const createWindow = async () => {
app.whenReady().then(async () => {
await initSettings()
appSettings = await getSettings()
nativeTheme.themeSource = appSettings.appearance.theme
nativeTheme.themeSource = appSettings.general.appearance.theme
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in this file are intended for testing purposes and will be reverted before merging.

Comment on lines +13 to +16
case '1.0':
return migrate(v1.migrate(settings))
case '2.0':
return settings
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will run recursively until the latest version of the schema is reached.

Comment on lines +112 to +114
export function migrate(
settings: z.infer<typeof AppSettingsSchema>
): v2.AppSettings {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every schema should export a migrate function that takes the current version and returns the next.

Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Will have another look once test commit is removed

@@ -3,7 +3,7 @@ import {
ProxySettingsSchema,
RecorderSettingsSchema,
UsageReportSettingsSchema,
} from '@/schemas/appSettings'
} from '@/schemas/settings/v2'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid updating imports every time we introduce a new version we should add export to schemas/settings/index.ts

export * from './v2'

Copy link
Collaborator Author

@cristianoventura cristianoventura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@e-fisher The test commit has been reverted.

Comment on lines +18 to +19
export const AppSettingsSchema = z.object({
version: z.literal('2.0'),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as v1, but I left some breadcrumbs to help us with the first migration when the time comes so no functional change at this time.

@cristianoventura cristianoventura marked this pull request as ready for review December 3, 2024 16:08
@cristianoventura cristianoventura requested a review from a team as a code owner December 3, 2024 16:08
@cristianoventura cristianoventura requested review from going-confetti, Llandy3d, e-fisher and allansson and removed request for a team December 3, 2024 16:08
Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Copy link
Collaborator

@allansson allansson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

@cristianoventura cristianoventura merged commit 0415013 into main Dec 4, 2024
2 checks passed
@cristianoventura cristianoventura deleted the feat/settings-migration branch December 4, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Settings migration
3 participants