mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
Question: How to create a new entity relationship during CoreStoreObject migration? #233
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @scogeo on GitHub (Oct 11, 2018).
I'm looking at CoreStore for a future project and really like what I see so far, but I have a question about more complex migrations when using CoreStoreObjects rather than model files.
I created a quick test project that has three schema versions. The first version, V1, is pretty straightforward, and the second version, V2, just adds an additional field so migration is automatic.
However in the 3rd version, the data model is refactored to take the image attribute in the V2 data model and move it into a new child entity. This is straight forward to do in Core Data with a mapping file. However, I can't seem to find a way to do it with CoreStore.
I'm looking at what it would take to support this in CoreStore, but before I dig in further I wanted to make sure I wasn't missing something obvious.
I've coded up below what I would like to happen in the
attachment_v2_to_v3_mapping. Essentially, I want to migrate the Notes over and delete theimageattribute using theCustomMapping.inferredTransformation. Then run through all the Notes again and this time migrate them to Attachments if they have an image in the source object. Again, this is possible with a mapping model file. With CoreStore, this produces an assertion error:Any advice?
@JohnEstropia commented on GitHub (Oct 12, 2018):
Ah, right now the custom mappings declaration is designed so that we don't need to write inferrable mappings. This is contrary to
.xcmappingmodelwhere all migrations are declared, including inferred transforms.I'm not sure this would be easy to support in the migrator's current form. Multiple destinations for a single source may be possible by removing the asserts and commenting out a few lines of code, but setting the relationship would be particularly difficult. For example,
We'll need a second pass for relationships where we can fetch created objects during the first pass.
If you really need to do this now, I'm afraid the only option is to process new objects manually right after CoreStore's migration completes. You can also opt to keep using
.xcmappingmodelusingXcodeDataModelSchemaandXcodeSchemaMappingProvider, albeit you'll be usingNSManagedObjects instead ofCoreStoreObjects.Also, just a heads up,
the
initial:argument here is static. Meaning theDate()instance will be used for all new instance of the object and will not be the date during the object's creation time.@scogeo commented on GitHub (Oct 12, 2018):
Thanks for the response. I really like the simplicity of using
CoreStoreObjects, so I think I will still pursue that path and go with a multi-pass migration if something like this comes up as the project evolves.I suppose in this example, I could create a
V2.5schema which contains the new entity and relationship but doesn't delete any attributes in 'Note', and allow the migration to proceed automatically. I could run my own code to hook up the relationships on theV2.5database, then finally migrate it over toV3.I'll code it up in my prototype and post a snippet here for reference in case anyone else has a similar issue.
Also thanks for pointing out the problem with
Date()in theinitial:. This is just a toy prototype for playing with CoreStore, but good to know for my real project.@scogeo commented on GitHub (Oct 16, 2018):
I prototyped a more complex migration example using two
DataStacks and an additional migration schema. The basic idea is to migrate the first stack to the migration schema and add it to aDataStackperform some DB operations to link up the new relationships. Then close that stack and load a new stack which migrates to the final schema.I've included a few snippets below in case anyone else finds them useful. Otherwise, this issue can probably be closed.
Here's a migration schema that contains the additions from the
V3schema without any deletions.Then the code to perform the two-step migration:
And a few helper functions: