DatabaseMigrator
A DatabaseMigrator
registers and applies database migrations.
struct DatabaseMigrator
For an overview of database migrations and DatabaseMigrator
usage, see Migrations.
Creating a DatabaseMigrator
init(
) A new migrator.
Registering Migrations
func registerMigration(String, foreignKeyChecks: ForeignKeyChecks, migrate: @escaping (Database) throws -> Void
) Registers a migration.
enum ForeignKeyChecks
Controls how a migration handle foreign keys constraints.
Configuring a DatabaseMigrator
var eraseDatabaseOnSchemaChange: Bool
A boolean value indicating whether the migrator recreates the whole database from scratch if it detects a change in the definition of migrations.
func disablingDeferredForeignKeyChecks(
) -> DatabaseMigrator Returns a migrator that disables foreign key checks in all newly registered migrations.
Migrating a Database
func asyncMigrate(any DatabaseWriter, completion: @escaping (Result<Database, Error>) -> Void
) Schedules unapplied migrations for execution, and returns immediately.
func migrate(any DatabaseWriter
) throws Runs all unapplied migrations, in the same order as they were registered.
func migrate(any DatabaseWriter, upTo: String
) throws Runs all unapplied migrations, in the same order as they were registered, up to the target migration identifier (included).
migratePublisher(_:receiveOn:)
Querying Migrations
var migrations: [String]
The list of registered migration identifiers, in the same order as they have been registered.
func appliedIdentifiers(Database
) throws -> Set<String> Returns the applied migration identifiers, even unregistered ones.
func appliedMigrations(Database
) throws -> [String] Returns the identifiers of registered and applied migrations, in the order of registration.
func completedMigrations(Database
) throws -> [String] Returns the identifiers of registered and completed migrations, in the order of registration.
func hasBeenSuperseded(Database
) throws -> Bool A boolean value indicating whether the database refers to unregistered migrations.
func hasCompletedMigrations(Database
) throws -> Bool A boolean value indicating whether all registered migrations, and only registered migrations, have been applied.
Detecting Schema Changes
func hasSchemaChanges(Database
) throws -> Bool Returns a boolean value indicating whether the migrator detects a change in the definition of migrations.