mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Instead of disabling schema cache throughout the migrations, clear the schema cache after each migration.
This commit is contained in:
1 parent
de4a86ca50
commit
1f3f2d04eb
2 files changed
+8
-26
No files matched your search
@@ -461,22 +461,6 @@ public final class Database {
|
||||
assertValidQueue()
|
||||
columnInfosCache = [:]
|
||||
}
|
||||
|
||||
/// When false, the schema cache is disabled.
|
||||
private var schemaCacheIsEnabled: Bool
|
||||
|
||||
func withDisabledSchemaCache(@noescape block:() throws -> ()) rethrows {
|
||||
assertValidQueue()
|
||||
|
||||
var schemaCacheWasEnabled = schemaCacheIsEnabled
|
||||
defer {
|
||||
schemaCacheIsEnabled = schemaCacheWasEnabled
|
||||
}
|
||||
|
||||
schemaCacheIsEnabled = false
|
||||
clearSchemaCache()
|
||||
try block()
|
||||
}
|
||||
|
||||
/// The last error message
|
||||
var lastErrorMessage: String? { return String.fromCString(sqlite3_errmsg(sqliteConnection)) }
|
||||
@@ -599,15 +583,13 @@ public final class Database {
|
||||
// Cache for columnInfosForTable(named:)
|
||||
private var columnInfosCache: [String: [ColumnInfo]] = [:]
|
||||
private func columnInfosForTable(named tableName: String) -> [ColumnInfo] {
|
||||
if let columnInfos = columnInfosCache[tableName] where schemaCacheIsEnabled {
|
||||
if let columnInfos = columnInfosCache[tableName] {
|
||||
return columnInfos
|
||||
} else {
|
||||
// This pragma is case-insensitive: PRAGMA table_info("PERSONS") and
|
||||
// PRAGMA table_info("persons") yield the same results.
|
||||
let columnInfos = ColumnInfo.fetchAll(self, "PRAGMA table_info(\(tableName.quotedDatabaseIdentifier))")
|
||||
if schemaCacheIsEnabled {
|
||||
columnInfosCache[tableName] = columnInfos
|
||||
}
|
||||
columnInfosCache[tableName] = columnInfos
|
||||
return columnInfos
|
||||
}
|
||||
}
|
||||
@@ -637,7 +619,6 @@ public final class Database {
|
||||
|
||||
init(path: String, configuration: Configuration) throws {
|
||||
self.configuration = configuration
|
||||
self.schemaCacheIsEnabled = true
|
||||
|
||||
// See https://www.sqlite.org/c3ref/open.html
|
||||
var sqliteConnection = SQLiteConnection()
|
||||
|
||||
@@ -85,10 +85,8 @@ public struct DatabaseMigrator {
|
||||
/// - throws: An eventual error thrown by the registered migration blocks.
|
||||
public func migrate(dbQueue: DatabaseQueue) throws {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.withDisabledSchemaCache {
|
||||
try self.setupMigrations(db)
|
||||
try self.runMigrations(db)
|
||||
}
|
||||
try self.setupMigrations(db)
|
||||
try self.runMigrations(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +110,9 @@ public struct DatabaseMigrator {
|
||||
let appliedMigrationIdentifiers = String.fetchAll(db, "SELECT identifier FROM grdb_migrations")
|
||||
try migrations
|
||||
.filter { !appliedMigrationIdentifiers.contains($0.identifier) }
|
||||
.forEach { try $0.run(db) }
|
||||
.forEach {
|
||||
try $0.run(db)
|
||||
db.clearSchemaCache()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user