mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-21 17:09:42 +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 {
|
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(
|
let migrationManager = NSMigrationManager(
|
||||||
sourceModel: sourceModel,
|
sourceModel: sourceModel,
|
||||||
destinationModel: destinationModel
|
destinationModel: destinationModel
|
||||||
@@ -475,21 +487,18 @@ public extension DataStack {
|
|||||||
withIntermediateDirectories: true,
|
withIntermediateDirectories: true,
|
||||||
attributes: nil
|
attributes: nil
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let temporaryFileURL = temporaryDirectoryURL.URLByAppendingPathComponent(fileURL.lastPathComponent!, isDirectory: false)
|
||||||
do {
|
do {
|
||||||
|
|
||||||
try migrationManager.migrateStoreFromURL(
|
try migrationManager.migrateStoreFromURL(
|
||||||
fileURL,
|
fileURL,
|
||||||
type: NSSQLiteStoreType,
|
type: NSSQLiteStoreType,
|
||||||
options: [
|
options: nil,
|
||||||
NSSQLitePragmasOption: ["WAL": "journal_mode"]
|
|
||||||
],
|
|
||||||
withMappingModel: mappingModel,
|
withMappingModel: mappingModel,
|
||||||
toDestinationURL: temporaryDirectoryURL.URLByAppendingPathComponent(fileURL.lastPathComponent!, isDirectory: false),
|
toDestinationURL: temporaryFileURL,
|
||||||
destinationType: NSSQLiteStoreType,
|
destinationType: NSSQLiteStoreType,
|
||||||
destinationOptions: [
|
destinationOptions: nil
|
||||||
NSSQLitePragmasOption: ["WAL": "journal_mode"],
|
|
||||||
NSSQLiteManualVacuumOption: true
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -515,20 +524,19 @@ public extension DataStack {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
let originalDirectoryURL = fileURL.URLByDeletingLastPathComponent!
|
try fileManager.replaceItemAtURL(
|
||||||
for temporaryFileURL in try fileManager.contentsOfDirectoryAtURL(temporaryDirectoryURL, includingPropertiesForKeys: nil, options: .SkipsSubdirectoryDescendants) {
|
fileURL,
|
||||||
|
withItemAtURL: temporaryFileURL,
|
||||||
|
backupItemName: nil,
|
||||||
|
options: [],
|
||||||
|
resultingItemURL: nil
|
||||||
|
)
|
||||||
|
|
||||||
try fileManager.replaceItemAtURL(
|
do {
|
||||||
originalDirectoryURL.URLByAppendingPathComponent(
|
|
||||||
temporaryFileURL.lastPathComponent!,
|
try fileManager.removeItemAtPath(fileURL.path! + "-shm")
|
||||||
isDirectory: false
|
|
||||||
),
|
|
||||||
withItemAtURL: temporaryFileURL,
|
|
||||||
backupItemName: nil,
|
|
||||||
options: [],
|
|
||||||
resultingItemURL: nil
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
catch _ { }
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ public final class DataStack {
|
|||||||
configuration: configuration,
|
configuration: configuration,
|
||||||
URL: fileURL,
|
URL: fileURL,
|
||||||
options: [
|
options: [
|
||||||
NSSQLitePragmasOption: ["WAL": "journal_mode"],
|
NSSQLitePragmasOption: ["journal_mode": "WAL"],
|
||||||
NSInferMappingModelAutomaticallyOption: true,
|
NSInferMappingModelAutomaticallyOption: true,
|
||||||
NSMigratePersistentStoresAutomaticallyOption: automigrating
|
NSMigratePersistentStoresAutomaticallyOption: automigrating
|
||||||
]
|
]
|
||||||
@@ -254,7 +254,7 @@ public final class DataStack {
|
|||||||
configuration: configuration,
|
configuration: configuration,
|
||||||
URL: fileURL,
|
URL: fileURL,
|
||||||
options: [
|
options: [
|
||||||
NSSQLitePragmasOption: ["WAL": "journal_mode"],
|
NSSQLitePragmasOption: ["journal_mode": "WAL"],
|
||||||
NSInferMappingModelAutomaticallyOption: true,
|
NSInferMappingModelAutomaticallyOption: true,
|
||||||
NSMigratePersistentStoresAutomaticallyOption: automigrating
|
NSMigratePersistentStoresAutomaticallyOption: automigrating
|
||||||
]
|
]
|
||||||
@@ -376,4 +376,16 @@ public final class DataStack {
|
|||||||
private let storeMetadataUpdateQueue = GCDQueue.createConcurrent("com.coreStore.persistentStoreBarrierQueue")
|
private let storeMetadataUpdateQueue = GCDQueue.createConcurrent("com.coreStore.persistentStoreBarrierQueue")
|
||||||
private var configurationStoreMapping = [String: NSPersistentStore]()
|
private var configurationStoreMapping = [String: NSPersistentStore]()
|
||||||
private var entityConfigurationsMapping = [String: Set<String>]()
|
private var entityConfigurationsMapping = [String: Set<String>]()
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
|
||||||
|
for store in self.coordinator.persistentStores {
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
try self.coordinator.removePersistentStore(store)
|
||||||
|
}
|
||||||
|
catch _ { }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
B503FAE01AFDC71700F90881 /* ObjectObserverDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */; };
|
B503FAE01AFDC71700F90881 /* ObjectObserverDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */; };
|
||||||
B503FAE11AFDC71700F90881 /* Palette.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADD1AFDC71700F90881 /* Palette.swift */; };
|
B503FAE11AFDC71700F90881 /* Palette.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADD1AFDC71700F90881 /* Palette.swift */; };
|
||||||
B503FAE21AFDC71700F90881 /* PaletteTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */; };
|
B503FAE21AFDC71700F90881 /* PaletteTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */; };
|
||||||
|
B50D67911B4BCFED00124277 /* OrganismV3ToV2.xcmappingmodel in Sources */ = {isa = PBXBuildFile; fileRef = B50D67901B4BCFED00124277 /* OrganismV3ToV2.xcmappingmodel */; };
|
||||||
B52977D91B120B80003D50A5 /* ObserversViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52977D81B120B80003D50A5 /* ObserversViewController.swift */; };
|
B52977D91B120B80003D50A5 /* ObserversViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52977D81B120B80003D50A5 /* ObserversViewController.swift */; };
|
||||||
B52977DD1B120F3B003D50A5 /* TransactionsDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52977DC1B120F3B003D50A5 /* TransactionsDemoViewController.swift */; };
|
B52977DD1B120F3B003D50A5 /* TransactionsDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B52977DC1B120F3B003D50A5 /* TransactionsDemoViewController.swift */; };
|
||||||
B52977DF1B120F83003D50A5 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B52977DE1B120F83003D50A5 /* MapKit.framework */; };
|
B52977DF1B120F83003D50A5 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B52977DE1B120F83003D50A5 /* MapKit.framework */; };
|
||||||
@@ -96,6 +97,7 @@
|
|||||||
B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectObserverDemoViewController.swift; sourceTree = "<group>"; };
|
B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectObserverDemoViewController.swift; sourceTree = "<group>"; };
|
||||||
B503FADD1AFDC71700F90881 /* Palette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Palette.swift; sourceTree = "<group>"; };
|
B503FADD1AFDC71700F90881 /* Palette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Palette.swift; sourceTree = "<group>"; };
|
||||||
B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteTableViewCell.swift; sourceTree = "<group>"; };
|
B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteTableViewCell.swift; sourceTree = "<group>"; };
|
||||||
|
B50D67901B4BCFED00124277 /* OrganismV3ToV2.xcmappingmodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcmappingmodel; name = OrganismV3ToV2.xcmappingmodel; path = "CoreStoreDemo/MIgrations Demo/OrganismV3ToV2.xcmappingmodel"; sourceTree = SOURCE_ROOT; };
|
||||||
B52977D81B120B80003D50A5 /* ObserversViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObserversViewController.swift; sourceTree = "<group>"; };
|
B52977D81B120B80003D50A5 /* ObserversViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObserversViewController.swift; sourceTree = "<group>"; };
|
||||||
B52977DC1B120F3B003D50A5 /* TransactionsDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionsDemoViewController.swift; sourceTree = "<group>"; };
|
B52977DC1B120F3B003D50A5 /* TransactionsDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionsDemoViewController.swift; sourceTree = "<group>"; };
|
||||||
B52977DE1B120F83003D50A5 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
|
B52977DE1B120F83003D50A5 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
|
||||||
@@ -265,6 +267,7 @@
|
|||||||
B5EE258B1B36E40D0000406B /* MigrationsDemoViewController.swift */,
|
B5EE258B1B36E40D0000406B /* MigrationsDemoViewController.swift */,
|
||||||
B5F45A601B4AE5A700831F2F /* OrganismV2ToV3.xcmappingmodel */,
|
B5F45A601B4AE5A700831F2F /* OrganismV2ToV3.xcmappingmodel */,
|
||||||
B560070E1B3EC90F00A9A8F9 /* OrganismV2ToV3MigrationPolicy.swift */,
|
B560070E1B3EC90F00A9A8F9 /* OrganismV2ToV3MigrationPolicy.swift */,
|
||||||
|
B50D67901B4BCFED00124277 /* OrganismV3ToV2.xcmappingmodel */,
|
||||||
);
|
);
|
||||||
path = "Migrations Demo";
|
path = "Migrations Demo";
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -394,6 +397,7 @@
|
|||||||
B5F45A611B4AE5A700831F2F /* OrganismV2ToV3.xcmappingmodel in Sources */,
|
B5F45A611B4AE5A700831F2F /* OrganismV2ToV3.xcmappingmodel in Sources */,
|
||||||
B503FAE11AFDC71700F90881 /* Palette.swift in Sources */,
|
B503FAE11AFDC71700F90881 /* Palette.swift in Sources */,
|
||||||
B503FAE21AFDC71700F90881 /* PaletteTableViewCell.swift in Sources */,
|
B503FAE21AFDC71700F90881 /* PaletteTableViewCell.swift in Sources */,
|
||||||
|
B50D67911B4BCFED00124277 /* OrganismV3ToV2.xcmappingmodel in Sources */,
|
||||||
B560070F1B3EC90F00A9A8F9 /* OrganismV2ToV3MigrationPolicy.swift in Sources */,
|
B560070F1B3EC90F00A9A8F9 /* OrganismV2ToV3MigrationPolicy.swift in Sources */,
|
||||||
B503FADF1AFDC71700F90881 /* ListObserverDemoViewController.swift in Sources */,
|
B503FADF1AFDC71700F90881 /* ListObserverDemoViewController.swift in Sources */,
|
||||||
B54AAD4F1AF4D26E00848AE0 /* AppDelegate.swift in Sources */,
|
B54AAD4F1AF4D26E00848AE0 /* AppDelegate.swift in Sources */,
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ class MigrationsDemoViewController: UITableViewController {
|
|||||||
let navigationItem = self.navigationItem
|
let navigationItem = self.navigationItem
|
||||||
navigationItem.leftBarButtonItem?.enabled = enabled
|
navigationItem.leftBarButtonItem?.enabled = enabled
|
||||||
navigationItem.rightBarButtonItem?.enabled = enabled
|
navigationItem.rightBarButtonItem?.enabled = enabled
|
||||||
navigationItem.backBarButtonItem?.enabled = enabled
|
navigationItem.hidesBackButton = !enabled
|
||||||
|
|
||||||
if let tableView = self.tableView {
|
if let tableView = self.tableView {
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user