Instance Methodgrdb 7.3.0GRDB

prepareDatabase(_:)

Defines a function to run whenever an SQLite connection is opened.

Configuration.swift:238
mutating func prepareDatabase(_ setup: @escaping (Database) throws -> Void)

The preparation function is run before the connection is made available for database access methods.

This method can be called several times. The preparation functions are run in the same order.

For example:

var config = Configuration()
config.prepareDatabase { db in
    // Prints all SQL statements
    db.trace { print("SQL >", $0) }
}

When you use a DatabasePool, preparation functions are called for the writer connection and all reader connections. You can distinguish them by querying db.configuration.readonly:

var config = Configuration()
config.prepareDatabase { db in
    if db.configuration.readonly {
        // reader connection
    } else {
        // writer connection
    }
}

On newly created databases files, DatabasePool activates the WAL mode after the preparation functions have run.