amplify-swift icon indicating copy to clipboard operation
amplify-swift copied to clipboard

Local data gets lost on DataStore migration

Open valzevul opened this issue 3 years ago • 8 comments

Describe the bug

Hey folks, every time models for Amplify are regenerated, let version in amplify/generated/models/AmplifyModels.swift changes (which is expected).

However, whenever I update the app, Amplify SDK compares the versions and if they are different migrates the database by removing the local copy and running a full sync against the remote copy.

This process doesn't trigger conflict resolution flows and docs mention that local migrations are not supported.

If that's the case, what are the best practices around the process? The app I am working on is offline-forward, so when users update there is a high chance they have lots of data not yet pushed to the servers, and losing it is less than ideal.

If that could be solved by selective sync (i.e as mentioned in #867), is there any timeline on its introduction to the iOS SDK?

Steps To Reproduce

Update an app with one version of AmplifyModels to another version of AmplifyModels, while having uncommited changes in the local database.

Expected behavior

The developer gets a chance to manually migrate / push uncommited changes.

Amplify Framework Version

1.18.1

Amplify Categories

DataStore

Dependency manager

Cocoapods

Swift version

5

CLI version

4.5.0

Xcode version

13.0

Relevant log output

No response

Is this a regression?

No

Regression additional context

No response

Device

iPhone 13 Pro - Simulator

iOS Version

iOS 15

Specific to simulators

No response

Additional context

No response

valzevul avatar Jan 14 '22 10:01 valzevul

Thanks @valzevul! I'm also interested on that. Looking forward for it

dsaliberti avatar Jan 18 '22 12:01 dsaliberti

hey @valzevul @dsaliberti, we are aware of the limitations imposed by the lack of support for migrations of unsynced data and we're evaluating different options. We're not actively working on it but it's in our roadmap and something we want to address.

diegocstn avatar Jan 22 '22 02:01 diegocstn

Hi @diegocstn, thanks for the heads up!

Good to know that this is on the roadmap, in the meantime I managed to temporarily address it on my side, although I don't yet know how reliable would that approach be:

  • On launch and before Amplify.configure() I check the let version in /generated/AmplifyModels.swift, and if it differs from the previous version I backup the db file used by DataStore
  • Once the remote migration is done I parse the backed up DB file, more specifically its MutationEvent table, and if there is anything there convert them back into Amplify models and insert / delete from the DataStore.

It seems to do the job but I guess there might be some edge cases I have missed (also relying on the private table might be less from ideal).

valzevul avatar Jan 25 '22 10:01 valzevul

Any good news on this one please? 😃

dsaliberti avatar Oct 10 '22 14:10 dsaliberti

One may wonder what real-world use cases is the Amplify Datastore designed for. To my knowledge, the offline-first solutions consider offline scenarios as their primary goal which means the local data should be treated as important and not just being thrown away.

altick avatar Sep 05 '23 07:09 altick

@valzevul Would it be possible for you to share the parsing code of the internal amplify db table?

fzy-github avatar Feb 19 '24 08:02 fzy-github

@fzy-spyro I've added a slightly simplified gist here, do let me know if there are any questions.

valzevul avatar Feb 26 '24 15:02 valzevul