Manually initiated Core Data migration fails if existing store was created with WAL journaling on, as is default on 10.9

Originator:armadsen
Number:rdar://15555487 Date Originated:11/27/13
Status:Open Resolved:
Product:OS X SDK Product Version:10.9
Classification: Reproducible:Always
 
Summary:
It appears that on 10.9, the WAL journal mode has become the default for SQLite files created by Core Data. In this mode, SQLite creates a -wal file (and a -shm file). If the main DB file is migrated, but the existing wal (and shm) files are left in place, adding the migrated DB to the persistent store coordinator fails with an exception.

Steps to Reproduce:
When trying to migrate a store created using WAL mode to a new data model version we do the following:

1. Determine if migration is needed.
2. Load the source and destination models, mapping model, and create a migration model.
3. Migrate from the existing store to a new store in a temporary location using NSMigrationManager's -migrateStoreFromURL:type:... method.
4. Backup the existing store file temporarily.
5. Copy the new (migrated) store from its temporary location to the same path the original store file had been at.
6. Delete the temporary back from step 4.
7. Add a persistent store for the new, migrated store file.

Expected Results:
Assuming all previous steps are successful, step 7 should also succeed.

Actual Results:
If the original store was created or has been saved with WAL mode enabled, step 7 above throws an exception and the following errors are logged to the console:

CoreData: error: failure reading metadata plist with data bytes: <33>
Unable to open database. Error: The file couldn’t be opened because it isn’t in the correct format.
Info: {
    NSUnderlyingException = "An error <null> occurred converting the metadata plist data: <33>";
}

The exception has the following stack trace (truncated to fit):

* thread #1: tid = 0x3b9627, 0x00007fff8e44ee4a libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread, stop reason = breakpoint 3.1
    frame #0: 0x00007fff8e44ee4a libobjc.A.dylib`objc_exception_throw
    frame #1: 0x00007fff8e00535f CoreData`-[NSSQLConnection fetchMetadata] + 719
    frame #2: 0x00007fff8e00257f CoreData`-[NSSQLCore _loadAndSetMetadata] + 175
    frame #3: 0x00007fff8e00236d CoreData`-[NSSQLCore loadMetadata:] + 77
    frame #4: 0x00007fff8dff99b6 CoreData`-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 1078


Version:
Xcode 5.0.1
OS X 10.9

Notes:
Looking for and deleting the -wal and -shm file after step 6 will prevent the exception from being thrown.

Configuration:


Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!