Versioned storage with migrations #49

Closed
opened 2015-11-22 20:31:08 +00:00 by steida · 6 comments
steida commented 2015-11-22 20:31:08 +00:00 (Migrated from github.com)

Hi,

I am just curious if you thought about store migrations? What if we change structure? I think it should be responsibility of every reducer reviver.

Hi, I am just curious if you thought about store migrations? What if we change structure? I think it should be responsibility of every reducer reviver.
michaelcontento commented 2015-11-23 07:54:58 +00:00 (Migrated from github.com)

Hi,

currently there is no built-in functionality to handle migrations at all. But it should be pretty easy to create a custom engine decorator for this purpose. Here is a short (untested!) draft:

// The engine as you like
import createEngine from 'redux-storage/engines/reactNativeAsyncStorage';
let engine = createEngine('my-save-key');

// Now add the new migrations decorator
import migrationDecorator from './migrationDecorator';
engine = migrationDecorator(engine, 'v3');
engine.addMigration('v1', (state) => { /* migration step for v1 */ return state; });
engine.addMigration('v2', (state) => { /* migration step for v2 */ return state; });
engine.addMigration('v3', (state) => { /* migration step for v3 */ return state; });

All you have to do in the migrationDecorator is to ....

  • store the current version (second argument to migrationDecorator) in the state tree on save()
  • compare the current version with the loaded one on load()
  • apply all required migration callbacks

So for the example in the draft above: if the current version is v3 and the loaded one is v1, we need to apply callbacks for v2 and v3.

Is this a good starting point for you?

Feel free to submit such a decorator as a PR! I'm thankful for every contribution :)

Hi, currently there is no built-in functionality to handle migrations at all. But it should be pretty easy to create a custom engine decorator for this purpose. Here is a short (untested!) draft: ``` js // The engine as you like import createEngine from 'redux-storage/engines/reactNativeAsyncStorage'; let engine = createEngine('my-save-key'); // Now add the new migrations decorator import migrationDecorator from './migrationDecorator'; engine = migrationDecorator(engine, 'v3'); engine.addMigration('v1', (state) => { /* migration step for v1 */ return state; }); engine.addMigration('v2', (state) => { /* migration step for v2 */ return state; }); engine.addMigration('v3', (state) => { /* migration step for v3 */ return state; }); ``` All you have to do in the `migrationDecorator` is to .... - store the current version (second argument to `migrationDecorator`) in the state tree on `save()` - compare the current version with the loaded one on `load()` - apply all required migration callbacks So for the example in the draft above: if the current version is `v3` and the loaded one is `v1`, we need to apply callbacks for `v2` and `v3`. Is this a good starting point for you? Feel free to submit such a decorator as a PR! I'm thankful for every contribution :)
steida commented 2015-11-23 17:15:55 +00:00 (Migrated from github.com)

Awesome answer, thank you! Once I will implemented it, I will send you a PR.

Awesome answer, thank you! Once I will implemented it, I will send you a PR.
michaelcontento commented 2015-11-23 18:37:38 +00:00 (Migrated from github.com)

Thanks! I'll close this issue for now :)

Thanks! I'll close this issue for now :)
steida commented 2015-11-30 04:47:25 +00:00 (Migrated from github.com)

Here is it: https://gist.github.com/steida/f5e53c69c4e3b423c15d

I simplified api, and I don't think we need explicit current version, since it's defined by migrations array.

Unfortunately, because cognitive overhead, I'm not able to send PR for

indent_size = 4
indent_style = space

Two spaces ftw.

Here is it: https://gist.github.com/steida/f5e53c69c4e3b423c15d I simplified api, and I don't think we need explicit current version, since it's defined by migrations array. Unfortunately, because `cognitive overhead`, I'm not able to send PR for ``` indent_size = 4 indent_style = space ``` Two spaces ftw.
michaelcontento commented 2016-01-26 08:15:43 +00:00 (Migrated from github.com)

Re-opened as a reminder. If someone want to contribute @steida's gist as a PR, I'd be very happy 😃

Re-opened as a reminder. If someone want to contribute @steida's gist as a PR, I'd be very happy :smiley:
steida commented 2016-01-26 15:26:11 +00:00 (Migrated from github.com)

That's what I am using https://gist.github.com/steida/85443aac1dcb6daaa245 No time to send PR yet, maybe later :-)

That's what I am using https://gist.github.com/steida/85443aac1dcb6daaa245 No time to send PR yet, maybe later :-)
This discussion has been locked. Commenting is limited to contributors.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MichaelContento/redux-storage#49
No description provided.