Configuration.prepareDatabase in now a method

This commit is contained in:
Gwendal Roué committed 2020-09-06 13:15:51 +02:00
1 parent 5820b30cc6
commit 886f75bbea
4 files changed
+26 -7

No files matched your search

+17 -4
View File
@@ -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 } ?? "")
}
+2 -2
View File
@@ -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?()
+1 -1
View File
@@ -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
+6
View File
@@ -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() }