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

Migrating DB #70

Closed
ditiem opened this issue Jul 15, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@ditiem
Copy link

commented Jul 15, 2018

Hello,

I am facing issues when the DB model is updated (example I add a new field). I think this can be easily fixed by adding a "version" option in the config call:

nSQL()
.models[...]
.config({
    id: "myDB" // name of my database
    version: "1.0.1" 
})

and a new migrate method. Examples of use:
1- nSQL( 'table' ).migrate( (old_version, new_version) => { ... update this table and return a promise ... } )
2- nSQL( ).migrate( (old_version, new_version) => { ... here you can update the whole database ... } )

There could be another option for migrate (I am commenting it for documentation purposes) that it is more declarative:

.migrateTo( "1.0.1", ( from_version ) => ... ) 
.migrateTo( "1.0.2", ( from_version should be "1.0.1" ) => ... )

It allows to descentralice migration code (I think it is not a good idea, as you may need to do the update in certain order), and requires que execute migrations in order (from 1.0.0 to 1.0.1, once finished, from 1.0.1 to 1.0.2...), which looks "cool" but you can shoot are your feet some times - imaging a huge table is removed in 1.0.3, you do huge updates from .1 to .2, to simply be destroyed in .3

So in summary, if a new "version" is detected, nSQL could simply call the migrate method and wait till all of them are completed before returning in the connect method.

I do not know if there are any plans for this.

@sebastianmacias

This comment has been minimized.

Copy link

commented Jul 15, 2018

For reference this is how dexie handles migrations http://dexie.org/docs/Dexie/Dexie.version(). The upgrade method to modify your existing data during a migration is very helpful.

@ditiem

This comment has been minimized.

Copy link
Author

commented Jul 16, 2018

Thank you @sebastianmacias for the reference.

I see some cavities, for instance how do you migrate from version 1 to 3? As I only read the example there is probably a way to do it, but it is not clear to me.

@sebastianmacias

This comment has been minimized.

Copy link

commented Jul 17, 2018

It runs all migrations. 1,2,3

@ClickSimply

This comment has been minimized.

Copy link
Owner

commented Jul 20, 2018

Just implemented this feature in 1.71, which should be live on NPM in the next few hours.

There are two new config options, version and onVersionUpdate.

The onVersionUpdate is a function that receives the current version as it's argument and returns a promise. The promise must return the version that it's been migrated to.

nSQL()
.config({
    version: 8,
    onVersionUpdate: (currentV: number) => {
        return new Promise((res, rej) => {
            switch (currentV) {
                case 1:
                    // migrate from 1 to 2
                    res(2);
                case 2:
                    // migrate from 2 to 3
                    res(3);
                break;
                case 3:
                    // migrate from 3 to 8
                    res(8);
                break;
                default:
                   rej("What kinda version is this?");
            }
        });
    }
});

The onVersionUpdate will be called until the promise returned matches the version provided in the config.

Hope that's everything you were after!

@ditiem

This comment has been minimized.

Copy link
Author

commented Jul 20, 2018

Thank a lot Scott!

@s-devaney

This comment has been minimized.

Copy link

commented Oct 14, 2018

This is the feature which convinced me to adopt Nano for my next Cordova app. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.