Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DatabaseMigrator deadlock with serial target queues #744

Merged
merged 2 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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