Make database connection lazy and adjust config execution order

This commit is contained in:
2025-08-21 15:00:10 +03:00
parent f4fefb3391
commit d26587cfc3
8 changed files with 129 additions and 173 deletions

View File

@@ -25,13 +25,13 @@ import DataLiteCore
public protocol DatabaseServiceKeyProvider: AnyObject, Sendable {
/// Returns the encryption key for the specified database service.
///
/// May return `nil` if the encryption key is currently unavailable or if the database
/// does not require encryption.
/// This method must either return a valid encryption key or throw an error if
/// the key cannot be retrieved.
///
/// - Parameter service: The service requesting the key.
/// - Returns: The encryption key or `nil`.
/// - Returns: The encryption key.
/// - Throws: An error if the key cannot be retrieved.
func databaseService(keyFor service: DatabaseServiceProtocol) throws -> Connection.Key?
func databaseService(keyFor service: DatabaseServiceProtocol) throws -> Connection.Key
/// Indicates whether the service should attempt to reconnect if applying the key fails.
///

View File

@@ -17,11 +17,6 @@ import DataLiteCore
///
/// - ``DatabaseServiceKeyProvider``
/// - ``keyProvider``
/// - ``applyKeyProvider()``
///
/// ### Connection Management
///
/// - ``reconnect()``
///
/// ### Database Operations
///
@@ -47,24 +42,6 @@ public protocol DatabaseServiceProtocol: AnyObject, Sendable {
/// encountered while applying a key.
var keyProvider: DatabaseServiceKeyProvider? { get set }
/// Applies the encryption key from the current provider.
///
/// Calls the configured ``keyProvider`` to obtain a key and applies
/// it to the active connection. If the key is unavailable or an
/// error occurs while applying it, the method throws.
///
/// - Throws: An error if the key cannot be retrieved or applied.
func applyKeyProvider() throws
/// Reopens the database connection.
///
/// Creates a new connection using the provider and applies the
/// encryption key if ``keyProvider`` is set. Typically used when
/// the previous connection has become invalid.
///
/// - Throws: An error if the new connection cannot be created or the key cannot be applied.
func reconnect() throws
/// Executes the given closure with an active connection.
///
/// The closure receives the connection and may perform any
@@ -73,7 +50,7 @@ public protocol DatabaseServiceProtocol: AnyObject, Sendable {
/// - Parameter closure: The closure that accepts a connection.
/// - Returns: The value returned by the closure.
/// - Throws: An error if one occurs during closure execution.
func perform<T>(_ closure: Perform<T>) rethrows -> T
func perform<T>(_ closure: Perform<T>) throws -> T
/// Executes the given closure within a transaction.
///
@@ -86,5 +63,5 @@ public protocol DatabaseServiceProtocol: AnyObject, Sendable {
/// - closure: The closure that accepts a connection.
/// - Returns: The value returned by the closure.
/// - Throws: An error if one occurs during closure execution.
func perform<T>(in transaction: TransactionType, closure: Perform<T>) rethrows -> T
func perform<T>(in transaction: TransactionType, closure: Perform<T>) throws -> T
}

View File

@@ -15,16 +15,6 @@ public protocol MigrationServiceProtocol: AnyObject, Sendable {
/// Encryption key provider for the database service.
var keyProvider: DatabaseServiceKeyProvider? { get set }
/// Applies an encryption key to the current database connection.
///
/// - Throws: Any error that occurs while retrieving or applying the key.
func applyKeyProvider() throws
/// Recreates the database connection and reapplies the encryption key if available.
///
/// - Throws: Any error that occurs while creating the connection or applying the key.
func reconnect() throws
/// Registers a migration to be executed by the service.
///
/// - Parameter migration: The migration to register.