Refactoring

This commit is contained in:
2025-11-07 20:38:09 +02:00
parent 5566f6f6ba
commit 5bbb722b20
26 changed files with 1097 additions and 770 deletions

View File

@@ -1,48 +1,58 @@
import Foundation
/// Protocol for managing and executing database schema migrations.
/// A type that manages and executes database schema migrations.
///
/// Conforming types are responsible for registering migrations, applying
/// encryption keys (if required), and executing pending migrations in
/// ascending version order.
/// ## Overview
///
/// Migrations ensure that the database schema evolves consistently across
/// application versions without requiring manual intervention.
/// Conforming types are responsible for registering migration steps, applying encryption keys
/// (if required), and executing pending migrations in ascending version order. Migrations ensure
/// that the database schema evolves consistently across application versions without manual
/// intervention.
///
/// ## Topics
///
/// ### Associated Types
/// - ``Version``
///
/// ### Properties
/// - ``keyProvider``
///
/// ### Instance Methods
/// - ``add(_:)``
/// - ``migrate()``
/// - ``migrate()-18x5r``
public protocol MigrationServiceProtocol: AnyObject, Sendable {
/// Type representing the schema version used for migrations.
/// The type representing a schema version used for migrations.
associatedtype Version: VersionRepresentable
/// Encryption key provider for the database service.
var keyProvider: DatabaseServiceKeyProvider? { get set }
/// The provider responsible for supplying encryption keys to the service.
var keyProvider: ConnectionServiceKeyProvider? { get set }
/// Registers a migration to be executed by the service.
///
/// - Parameter migration: The migration to register.
/// - Throws: ``MigrationError/duplicateMigration(_:)`` if a migration with
/// the same version or script URL is already registered.
/// - Throws: ``MigrationError/duplicateMigration(_:)`` if a migration with the same version
/// or script URL is already registered.
func add(_ migration: Migration<Version>) throws(MigrationError<Version>)
/// Executes all pending migrations in ascending version order.
///
/// - Throws: ``MigrationError/emptyMigrationScript(_:)`` if a migration
/// script is empty.
/// - Throws: ``MigrationError/migrationFailed(_:_:)`` if a script execution
/// or version update fails.
/// - Throws: ``MigrationError/emptyMigrationScript(_:)`` if a migration script is empty.
/// - Throws: ``MigrationError/migrationFailed(_:_:)`` if a migration step fails to execute
/// or update the stored version.
func migrate() throws(MigrationError<Version>)
}
@available(iOS 13.0, *)
@available(macOS 10.15, *)
@available(iOS 13.0, macOS 10.15, *)
public extension MigrationServiceProtocol {
/// Asynchronously executes all pending migrations in ascending order.
/// Asynchronously executes all pending migrations in ascending version order.
///
/// Performs the same logic as ``migrate()``, but runs asynchronously
/// on a background task with `.utility` priority.
/// Performs the same logic as ``migrate()``, but runs asynchronously on a background task with
/// `.utility` priority.
///
/// - Throws: ``MigrationError/emptyMigrationScript(_:)`` if a migration
/// script is empty.
/// - Throws: ``MigrationError/migrationFailed(_:_:)`` if a script execution
/// or version update fails.
/// - Throws: ``MigrationError/emptyMigrationScript(_:)`` if a migration script is empty.
/// - Throws: ``MigrationError/migrationFailed(_:_:)`` if a migration step fails to execute
/// or update the stored version.
func migrate() async throws {
try await Task(priority: .utility) {
try self.migrate()