mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Configuration.prepareDatabase in now a method
This commit is contained in:
1 parent
5820b30cc6
commit
886f75bbea
4 files changed
+26
-7
No files matched your search
@@ -103,16 +103,23 @@ public struct Configuration {
|
||||
|
||||
// MARK: - Managing SQLite Connections
|
||||
|
||||
/// A function that is run when an SQLite connection is opened, before the
|
||||
/// connection is made available for database access methods.
|
||||
private var setups: [(Database) throws -> Void] = []
|
||||
|
||||
/// The function argument is run when an SQLite connection is opened,
|
||||
/// before the connection is made available for database access methods.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// var config = Configuration()
|
||||
/// config.prepareDatabase = { db in
|
||||
/// config.prepareDatabase { db in
|
||||
/// try db.execute(sql: "PRAGMA kdf_iter = 10000")
|
||||
/// }
|
||||
public var prepareDatabase: ((Database) throws -> Void)?
|
||||
///
|
||||
/// This method can be called several times. The setup functions are run in
|
||||
/// the same order.
|
||||
public mutating func prepareDatabase(_ setup: @escaping (Database) throws -> Void) {
|
||||
setups.append(setup)
|
||||
}
|
||||
|
||||
// MARK: - Transactions
|
||||
|
||||
@@ -207,6 +214,12 @@ public struct Configuration {
|
||||
return threadingMode.SQLiteOpenFlags | readWriteFlags
|
||||
}
|
||||
|
||||
func setUp(_ db: Database) throws {
|
||||
for f in setups {
|
||||
try f(db)
|
||||
}
|
||||
}
|
||||
|
||||
func identifier(defaultLabel: String, purpose: String? = nil) -> String {
|
||||
(self.label ?? defaultLabel) + (purpose.map { "." + $0 } ?? "")
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
// MARK: - Database Setup
|
||||
|
||||
/// This method must be called after database initialization
|
||||
func setup() throws {
|
||||
func setUp() throws {
|
||||
setupBusyMode()
|
||||
setupDoubleQuotedStringLiterals()
|
||||
try setupForeignKeys()
|
||||
@@ -226,7 +226,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
#endif
|
||||
|
||||
// Last step before we can start accessing the database.
|
||||
try configuration.prepareDatabase?(self)
|
||||
try configuration.setUp(self)
|
||||
|
||||
try validateFormat()
|
||||
configuration.SQLiteConnectionDidOpen?()
|
||||
|
||||
@@ -53,7 +53,7 @@ final class SerializedDatabase {
|
||||
SchedulingWatchdog.allowDatabase(db, onQueue: queue)
|
||||
try queue.sync {
|
||||
do {
|
||||
try db.setup()
|
||||
try db.setUp()
|
||||
} catch {
|
||||
db.close()
|
||||
throw error
|
||||
|
||||
@@ -48,6 +48,12 @@ extension AssociationAggregate {
|
||||
}
|
||||
|
||||
extension Configuration {
|
||||
@available(*, unavailable, message: "Replace the assignment with a method call: prepareDatabase { db in ... }")
|
||||
public var prepareDatabase: ((Database) throws -> Void)? {
|
||||
get { preconditionFailure() }
|
||||
set { preconditionFailure() }
|
||||
}
|
||||
|
||||
@available(*, unavailable, message: "Use Database.trace(options:_:) in Configuration.prepareDatabase instead.")
|
||||
public var trace: TraceFunction? {
|
||||
get { preconditionFailure() }
|
||||
|
||||
Reference in new issue
Block a user