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

Add DB schema check and instructions for updating the schema version #219

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rygine
Copy link
Collaborator

@rygine rygine commented Mar 20, 2024

Summary

When the local DB schema is updated as part of a code change in the React SDK, client apps must upgrade their schema version to include the changes. To help with this, instructions and a DB schema check have been added. If the schema check detects change, it will throw an error that informs app developers to upgrade the schema version.

@rygine rygine requested review from fabriguespe and a team as code owners March 20, 2024 20:47
Copy link

changeset-bot bot commented Mar 20, 2024

⚠️ No Changeset found

Latest commit: d2f53fa

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

.sort((a, b) => (a.name < b.name ? 1 : -1)),
);

const hasSchemaChanged = async (db: Dexie) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to have some tests for this. Feels like it could be a finicky process.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do! that's an easy add, it's just a string comparison.

await db.open();
if (await hasSchemaChanged(db)) {
throw new Error(
`The local DB schema has changed and must be upgraded.
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we only changing the schema on major version's? Seems like that is a requirement, given it requires an explicit user migration to update their schema version

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that's a good question and something i've thought about a lot. sometimes adding a new feature requires a schema change, but adding to the schema doesn't usually create an issue. it's only when updating/removing an existing schema that requires a new version.

that being said, having to add/update a component prop for an SDK update could be considered a breaking change in that it does require a code change. however, it's not an involved refactor, usually just a single character change. is that a breaking change? i could be convinced that it is... or isn't.

ideally, this is something that we would manage internally so that developers don't have to think about it. how we're currently managing the schema for content type caching makes this more difficult since we're not storing all versions of the schema over time.

i'm definitely open to going down this route. it was something i originally considered but went with a simpler solution at the time.

import App from "./App.tsx";

export const MyApp: React.FC = () => (
<XMTPProvider dbVersion={2}>
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens when I increment this version? Is it going to wipe out my entire local DB and force re-create it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just informs Dexie about the new schema so it understands how to work with the data. the current DB and its data should remain intact.

Copy link
Contributor

Choose a reason for hiding this comment

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

@rygine heading over here from 215 to further discuss the issue - i've now set the dbVersion to 1 (also tried 2) but on a browser where i did not manually delete the dexie from localStorage i'm getting the following error now:

image

any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

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

different error when version is set to 2:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

when you set the dbVersion to 2, did you have another tab open with the app? i haven't encountered that error yet, but this issue may provide some insight.

Copy link
Contributor

Choose a reason for hiding this comment

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

no other tab was open in firefox, but the same app dev instance was open in chrome - do you think this messed up the firefox dexie database?
this is the actual error in the console:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that's strange. chrome shouldn't affect firefox and vice versa.

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.

Bug: DexieError after upgrade to latest xmtp sdks
3 participants