Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

Migration - removeColumn? #986

Open cprice-kgi opened 3 years ago

cprice-kgi commented 3 years ago

Hello, I'm doing some work within a React Native open source project that uses WatermelonDB. Recently, I made some updates to our app, changing the schema to add a couple of new columns. Until today, I didn't realize how to properly use migrations, so basically I just jammed them in without doing it the correct way.

Now that I understand I should use migrations, I know how -- but I now have some users who have already updated their apps to my "jammed" version, and other users who are still a version behind and have not updated to the newest schema as of yet.

A proper migration with addColumns would work just fine in a rollout to the users who have not yet updated to the "jammed" schema version, but in testing I see that if someone who has already updated to the newest tries to update with a migration, they will receive the "duplicate column" error (because technically, they have already assimilated the column into their local db). If those users uninstall and reinstall entirely, everything is fine, but ideally I'd like to avoid this.

I realize if we had not released the non-migration version into the wild, I would not be facing this issue, but does anyone have advice on how to seamlessly upgrade both those users who are behind and those who are ahead a version? Ideally, migration would recognize the column exists and just increment the schema version in the "newer" cases, and add it and increment the version for the "older" cases, but I'm not sure how to do this. Also, if I could drop those columns and then re-add them all in one migration, it would work for everyone, but I see that there is no drop column functionality in migrations.

A couple more things to note. In my "jammed" first attempt without the migration, I also did not increment the version of the schema (I was a novice). Also, this particular schema is used to cache properties from a server, so I don't care about losing data (on another note, can I force a sync when a migration is performed? That would be awesome.). Another solution is just to blow away the db and re-create, but I can't seem to do that in a seamless way either. Someone online says just increment the version number of the schema, but that causes my app not to load.

The best I can come up with at this time is to push out a properly coded migration. This will work correctly for all users but those who have already updated to the "jammed" schema version. In those cases, an uninstall/reinstall brings them up to date. I'd really like to avoid asking folks to uninstall though.

stale[bot] commented 2 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.