angelokh
angelokh

Reputation: 9428

recognize data model versioning and execute certain actions

I want to remote all entities when user upgrade to newer version with newer model version. For example,

App version 1.0 & Model version 1 Table X Table Y

When upgrade to app version 1.1 & Model version 2, I add two tables Table H Table K

Table X and Y should be removed.

How to recognize the current data model version and execute these removal actions?

Upvotes: 0

Views: 116

Answers (1)

svena
svena

Reputation: 2779

You do the migration with a custom migration policy and a mapping model.

  1. You have a model version 1.0 with Table A and version 1.1 with Table B, and no Table A
  2. You create a mapping model from version 1.0 as your source to 1.1 as your destination.
  3. You create a new file, a subclass of NSEntityMigrationPolicy, but you do not have to implement any methods if you are not planning to do some tricky conversion in code.
  4. In your mapping model you have entity mapping and you set Table A as your source and Table B as your destination. Type will be automatically set to custom.
  5. You type into custom policy field the NSEntityMigrationPolicy subclass name. It's possible that you can use NSEntityMigrationPolicy itself since we did not override anything, but I have not tried it.
  6. at the attribute mappings tab you see value expression field for all of your fields in Table A. You type $source.attributeNameInYourOldTableGoesHere into each field.
  7. You change the NSInferMappingModelAutomaticallyOption to NO and NSMigratePersistentStoresAutomaticallyOption to YES when initializing your persistent store.

Best regards,

sven.

Upvotes: 1

Related Questions