Skip to content

Commit

Permalink
Merge pull request #744 from groue/dev/fix-741
Browse files Browse the repository at this point in the history
Fix DatabaseMigrator deadlock with serial target queues
  • Loading branch information
groue authored Mar 29, 2020
2 parents e831c64 + 97a84c6 commit ba0c771
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion GRDB/Migration/DatabaseMigrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public struct DatabaseMigrator {
//
// Create a temporary witness database (on disk, just in case
// migrations would involve a lot of data).
let witness = try DatabaseQueue(path: "", configuration: writer.configuration)
var witnessConfiguration = writer.configuration
witnessConfiguration.targetQueue = nil // Avoid deadlocks
let witness = try DatabaseQueue(path: "", configuration: witnessConfiguration)

// Grab schema of migrated witness database
let witnessSchema: SchemaInfo = try witness.writeWithoutTransaction { db in
Expand Down
14 changes: 14 additions & 0 deletions Tests/GRDBTests/DatabaseMigratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,20 @@ class DatabaseMigratorTests : GRDBTestCase {
try XCTAssertTrue(dbQueue.read(newMigrator.hasCompletedMigrations))
}

// Regression test for https://github.com/groue/GRDB.swift/issues/741
func testEraseDatabaseOnSchemaChangeDoesNotDeadLock() throws {
dbConfiguration.targetQueue = DispatchQueue(label: "target")
let dbQueue = try makeDatabaseQueue()

var migrator = DatabaseMigrator()
migrator.eraseDatabaseOnSchemaChange = true
migrator.registerMigration("1", migrate: { _ in })
try migrator.migrate(dbQueue)

migrator.registerMigration("2", migrate: { _ in })
try migrator.migrate(dbQueue)
}

func testEraseDatabaseOnSchemaChange() throws {
// 1st version of the migrator
var migrator1 = DatabaseMigrator()
Expand Down

0 comments on commit ba0c771

Please sign in to comment.