-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
105 changed files
with
3,427 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
### 1.1.0 | ||
- Added migration between model versions. | ||
- Refinements. | ||
|
||
### 1.0.0 | ||
|
||
- Refinements. | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// CoreDataPlus | ||
// | ||
// Copyright © 2016-2018 Tinrobots. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html | ||
// https://developer.apple.com/documentation/coredata/heavyweight_migration | ||
// https://www.objc.io/issues/4-core-data/core-data-migration/ | ||
|
||
import CoreData | ||
|
||
public struct Migration { | ||
|
||
private init() { } | ||
|
||
/// **CoreDataPlus** | ||
/// | ||
/// Migrates a store to a given version. | ||
/// | ||
/// - Parameters: | ||
/// - sourceURL: the current store URL. | ||
/// - targetVersion: the ModelVersion to which the store is needed to migrate to. | ||
/// - progress: a Progress instance to monitor the migration. | ||
/// - Throws: It throws an error in cases of failure. | ||
public static func migrateStore<Version: ModelVersion>(at sourceURL: URL, targetVersion: Version, progress: Progress? = nil) throws { | ||
try migrateStore(from: sourceURL, to: sourceURL, targetVersion: targetVersion, deleteSource: false, progress: progress) | ||
} | ||
|
||
/// **CoreDataPlus** | ||
/// | ||
/// Migrates a store to a given version. | ||
/// | ||
/// - Parameters: | ||
/// - sourceURL: the current store URL. | ||
/// - targetURL: the store URL after the migration phase. | ||
/// - targetVersion: the ModelVersion to which the store is needed to migrate to. | ||
/// - deleteSource: if `true` the initial store will be deleted after the migration phase. | ||
/// - progress: a Progress instance to monitor the migration. | ||
/// - Throws: It throws an error in cases of failure. | ||
public static func migrateStore<Version: ModelVersion>(from sourceURL: URL, to targetURL: URL, targetVersion: Version, deleteSource: Bool = false, progress: Progress? = nil) throws { | ||
guard let sourceVersion = Version(persistentStoreURL: sourceURL as URL) else { | ||
fatalError("A ModelVersion for the store at URL \(sourceURL) could not be found.") | ||
} | ||
|
||
do { | ||
guard try sourceVersion.isMigrationPossible(for: sourceURL, to: targetVersion) else { | ||
return | ||
} | ||
|
||
var currentURL = sourceURL | ||
let steps = sourceVersion.migrationSteps(to: targetVersion) | ||
|
||
guard steps.count > 0 else { | ||
return | ||
} | ||
|
||
var migrationProgress: Progress? | ||
|
||
if let progress = progress { | ||
migrationProgress = Progress(totalUnitCount: Int64(steps.count), parent: progress, pendingUnitCount: progress.totalUnitCount) | ||
} | ||
|
||
for step in steps { | ||
try autoreleasepool { | ||
migrationProgress?.becomeCurrent(withPendingUnitCount: 1) | ||
let manager = NSMigrationManager(sourceModel: step.sourceModel, destinationModel: step.destinationModel) | ||
migrationProgress?.resignCurrent() | ||
|
||
let destinationURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(UUID().uuidString) | ||
|
||
for mapping in step.mappings { | ||
try manager.migrateStore(from: currentURL, | ||
sourceType: NSSQLiteStoreType, | ||
options: nil, | ||
with: mapping, | ||
toDestinationURL: destinationURL, | ||
destinationType: NSSQLiteStoreType, | ||
destinationOptions: nil) | ||
} | ||
|
||
if currentURL != sourceURL { | ||
try NSPersistentStoreCoordinator.destroyStore(at: currentURL) | ||
} | ||
currentURL = destinationURL | ||
} | ||
} | ||
|
||
try NSPersistentStoreCoordinator.replaceStore(at: targetURL, withStoreAt: currentURL) | ||
|
||
if currentURL != sourceURL { | ||
try NSPersistentStoreCoordinator.destroyStore(at: currentURL) | ||
} | ||
|
||
if targetURL != sourceURL && deleteSource { | ||
try NSPersistentStoreCoordinator.destroyStore(at: sourceURL) | ||
} | ||
|
||
} catch { | ||
throw CoreDataPlusError.migrationFailed(error: error) | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// CoreDataPlus | ||
// | ||
// Copyright © 2016-2018 Tinrobots. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import CoreData | ||
|
||
/// **CoreDataPlus** | ||
/// | ||
/// Represents a single step during the migration process. | ||
public final class MigrationStep { | ||
var sourceModel: NSManagedObjectModel | ||
var destinationModel: NSManagedObjectModel | ||
var mappings: [NSMappingModel] | ||
|
||
init(source: NSManagedObjectModel, destination: NSManagedObjectModel, mappings: [NSMappingModel]) { | ||
self.sourceModel = source | ||
self.destinationModel = destination | ||
self.mappings = mappings | ||
} | ||
} |
Oops, something went wrong.