Skip to content

Commit

Permalink
Merge pull request #1653 from Jnosh/schema-changes-readonly
Browse files Browse the repository at this point in the history
Fix DatabaseMigrator.hasSchemaChanges failing for readonly connections
  • Loading branch information
groue authored Oct 13, 2024
2 parents 2c49a69 + a741fb3 commit 8aaeb25
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions GRDB/Migration/DatabaseMigrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ public struct DatabaseMigrator: Sendable {
// Make sure the temporary database is configured
// just as the migrated database
var tmpConfig = db.configuration
tmpConfig.readonly = false // We need write access
tmpConfig.targetQueue = nil // Avoid deadlocks
tmpConfig.writeTargetQueue = nil // Avoid deadlocks
tmpConfig.label = "GRDB.DatabaseMigrator.temporary"
Expand Down
36 changes: 36 additions & 0 deletions Tests/GRDBTests/DatabaseMigratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,42 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
}

func testHasSchemaChangesWorksWithReadonlyConfig() throws {
// 1st version of the migrator
var migrator1 = DatabaseMigrator()
migrator1.registerMigration("1") { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
t.column("name", .text)
}
}

// 2nd version of the migrator
var migrator2 = DatabaseMigrator()
migrator2.registerMigration("1") { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
t.column("name", .text)
t.column("score", .integer) // <- schema change, because reasons (development)
}
}

let dbName = ProcessInfo.processInfo.globallyUniqueString
let dbQueue = try makeDatabaseQueue(filename: dbName)

try XCTAssertFalse(dbQueue.read(migrator1.hasSchemaChanges))
try migrator1.migrate(dbQueue)
try XCTAssertFalse(dbQueue.read(migrator1.hasSchemaChanges))
try dbQueue.close()

// check that the migrator doesn't fail for a readonly connection
dbConfiguration.readonly = true
let readonlyQueue = try makeDatabaseQueue(filename: dbName)

try XCTAssertFalse(readonlyQueue.read(migrator1.hasSchemaChanges))
try XCTAssertTrue(readonlyQueue.read(migrator2.hasSchemaChanges))
}

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

0 comments on commit 8aaeb25

Please sign in to comment.