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

Should you clear storage when changing your schema? #30

Closed
lon9man opened this issue May 20, 2021 · 2 comments
Closed

Should you clear storage when changing your schema? #30

lon9man opened this issue May 20, 2021 · 2 comments
Labels
kind: question This is a usage or similar question

Comments

@lon9man
Copy link

lon9man commented May 20, 2021

Main Question
what do you think about clear storage, when its structure was upgraded (extended, updated, ...)?

Example

  1. development iteration 1: create basic model
const User = types
.model('User', {
    id: types.number,
    name: types.string,
  })
  
const UserStore = User.create({
  id: 1,
  name: 'John Doe',
})

persist('@UserStore', UserStore, {
  storage: AsyncStorage,
  jsonify: true,
})
  1. development iteration 2: model extended with email-property, BUT in AsyncStorage persisted OLD model already and restoration of it will fail due to difference in the structure.
const User = types
.model('User', {
    id: types.number,
    name: types.string,
    email: types.string,                 # added property
  })
...

THOUGHTS:
on the first run of application (after store's structure was upgraded) we should detect such changes and clear storage. on the next runs - we should work with persist as usual. it looks like upgrade of the databases saying in Android (migrations and etc)

@lon9man
Copy link
Author

lon9man commented Jun 11, 2021

any ideas?

@agilgur5 agilgur5 added the kind: question This is a usage or similar question label Jul 19, 2023
@agilgur5 agilgur5 changed the title clear storage on upgrade store Should you clear storage when upgrading your model? Jul 19, 2023
@agilgur5 agilgur5 changed the title Should you clear storage when upgrading your model? Should you clear storage when changing your schema? Jul 19, 2023
@agilgur5
Copy link
Owner

agilgur5 commented Jul 19, 2023

Hey @lon9man sorry for not responding to this. My GitHub status at the time mentioned I was taking a break from OSS due to toxicity and abuse. Coming back now per my current status.

Getting to the question at hand, this is a schema change / migration question. #23 actually talks about multiple ways to do migrations -- very similar to how DBs handle it. I've reworded the title to mention this.

One way could indeed be to clear all storage as you suggest and just start from scratch. If the offline data can be easily retrieved, resetting in that fashion may be a good option.

Another way would be to run a migration when a schema change is detected. Effectively, this would be the same as back-end DB schema migration, but done client-side on pure JSON (or JS objects if #23 is implemented). Each migration would likely have to be hand-coded, there isn't necessarily a simple way to automatically figure out what to do when a schema changes (same as with a DB).
Backward-compatible migrations are a whole topic in DBs with multiple ways of achieving them, so I can't really describe that with the depth it requires in an issue. Some examples include adding defaults, splitting into multiple migrations, etc.

I would recommend thinking of your persisted information as a secondary DB or cache; you can model interactions with persisted data fairly similarly to that of a back-end DB or cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question This is a usage or similar question
Projects
None yet
Development

No branches or pull requests

2 participants