mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-17 06:27:02 +01:00
reversible migrations
This commit is contained in:
@@ -446,6 +446,18 @@ public extension DataStack {
|
||||
|
||||
private func startMigrationForSQLiteStore(fileURL fileURL: NSURL, sourceModel: NSManagedObjectModel, destinationModel: NSManagedObjectModel, mappingModel: NSMappingModel) throws {
|
||||
|
||||
autoreleasepool {
|
||||
|
||||
let journalUpdatingCoordinator = NSPersistentStoreCoordinator(managedObjectModel: sourceModel)
|
||||
let store = try! journalUpdatingCoordinator.addPersistentStoreWithType(
|
||||
NSSQLiteStoreType,
|
||||
configuration: nil,
|
||||
URL: fileURL,
|
||||
options: [NSSQLitePragmasOption: ["journal_mode": "DELETE"]]
|
||||
)
|
||||
try! journalUpdatingCoordinator.removePersistentStore(store)
|
||||
}
|
||||
|
||||
let migrationManager = NSMigrationManager(
|
||||
sourceModel: sourceModel,
|
||||
destinationModel: destinationModel
|
||||
@@ -475,21 +487,18 @@ public extension DataStack {
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
|
||||
let temporaryFileURL = temporaryDirectoryURL.URLByAppendingPathComponent(fileURL.lastPathComponent!, isDirectory: false)
|
||||
do {
|
||||
|
||||
try migrationManager.migrateStoreFromURL(
|
||||
fileURL,
|
||||
type: NSSQLiteStoreType,
|
||||
options: [
|
||||
NSSQLitePragmasOption: ["WAL": "journal_mode"]
|
||||
],
|
||||
options: nil,
|
||||
withMappingModel: mappingModel,
|
||||
toDestinationURL: temporaryDirectoryURL.URLByAppendingPathComponent(fileURL.lastPathComponent!, isDirectory: false),
|
||||
toDestinationURL: temporaryFileURL,
|
||||
destinationType: NSSQLiteStoreType,
|
||||
destinationOptions: [
|
||||
NSSQLitePragmasOption: ["WAL": "journal_mode"],
|
||||
NSSQLiteManualVacuumOption: true
|
||||
]
|
||||
destinationOptions: nil
|
||||
)
|
||||
}
|
||||
catch {
|
||||
@@ -515,20 +524,19 @@ public extension DataStack {
|
||||
|
||||
do {
|
||||
|
||||
let originalDirectoryURL = fileURL.URLByDeletingLastPathComponent!
|
||||
for temporaryFileURL in try fileManager.contentsOfDirectoryAtURL(temporaryDirectoryURL, includingPropertiesForKeys: nil, options: .SkipsSubdirectoryDescendants) {
|
||||
try fileManager.replaceItemAtURL(
|
||||
fileURL,
|
||||
withItemAtURL: temporaryFileURL,
|
||||
backupItemName: nil,
|
||||
options: [],
|
||||
resultingItemURL: nil
|
||||
)
|
||||
|
||||
do {
|
||||
|
||||
try fileManager.replaceItemAtURL(
|
||||
originalDirectoryURL.URLByAppendingPathComponent(
|
||||
temporaryFileURL.lastPathComponent!,
|
||||
isDirectory: false
|
||||
),
|
||||
withItemAtURL: temporaryFileURL,
|
||||
backupItemName: nil,
|
||||
options: [],
|
||||
resultingItemURL: nil
|
||||
)
|
||||
try fileManager.removeItemAtPath(fileURL.path! + "-shm")
|
||||
}
|
||||
catch _ { }
|
||||
}
|
||||
catch {
|
||||
|
||||
|
||||
@@ -199,7 +199,7 @@ public final class DataStack {
|
||||
configuration: configuration,
|
||||
URL: fileURL,
|
||||
options: [
|
||||
NSSQLitePragmasOption: ["WAL": "journal_mode"],
|
||||
NSSQLitePragmasOption: ["journal_mode": "WAL"],
|
||||
NSInferMappingModelAutomaticallyOption: true,
|
||||
NSMigratePersistentStoresAutomaticallyOption: automigrating
|
||||
]
|
||||
@@ -254,7 +254,7 @@ public final class DataStack {
|
||||
configuration: configuration,
|
||||
URL: fileURL,
|
||||
options: [
|
||||
NSSQLitePragmasOption: ["WAL": "journal_mode"],
|
||||
NSSQLitePragmasOption: ["journal_mode": "WAL"],
|
||||
NSInferMappingModelAutomaticallyOption: true,
|
||||
NSMigratePersistentStoresAutomaticallyOption: automigrating
|
||||
]
|
||||
@@ -376,4 +376,16 @@ public final class DataStack {
|
||||
private let storeMetadataUpdateQueue = GCDQueue.createConcurrent("com.coreStore.persistentStoreBarrierQueue")
|
||||
private var configurationStoreMapping = [String: NSPersistentStore]()
|
||||
private var entityConfigurationsMapping = [String: Set<String>]()
|
||||
|
||||
deinit {
|
||||
|
||||
for store in self.coordinator.persistentStores {
|
||||
|
||||
do {
|
||||
|
||||
try self.coordinator.removePersistentStore(store)
|
||||
}
|
||||
catch _ { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user