Skip to content

Merged Migrations#1818

Merged
groue merged 1 commit intodevelopmentfrom
dev/obsolete-migrations
Oct 2, 2025
Merged

Merged Migrations#1818
groue merged 1 commit intodevelopmentfrom
dev/obsolete-migrations

Conversation

@groue
Copy link
Copy Markdown
Owner

@groue groue commented Sep 26, 2025

The new registerMigration(_:foreignKeyChecks:merging:migrate) method registers a migration that merges and replaces a set of migrations defined in a previous version of the application. For example, to merge the migrations "v1", "v2" and "v3", redefine the "v3" migration so that it merges "v1" and "v2", as in the example below.

The second argument of the migration closure is the set of merged migrations that has been applied when the merged migration runs.

// Old code
migrator.registerMigration("v1") { db in
    // Apply schema version 1
}
migrator.registerMigration("v2") { db in
    // Apply schema version 2
}
migrator.registerMigration("v3") { db in
    // Apply schema version 3
}

// New code:
// - Migrations v1 and v2 are deleted.
// - Migration v3 is redefined and merges v1 and v2:
migrator.registerMigration("v3", merging: ["v1", "v2"]) { db, appliedIDs in
    if !appliedIDs.contains("v1") {
        // Apply schema version 1
    }
    if !appliedIDs.contains("v2") {
        // Apply schema version 2
    }
    // Apply schema version 3
}

See #1817 for a longer discussion.

@groue groue force-pushed the dev/obsolete-migrations branch 2 times, most recently from 9c42b18 to 9a811e1 Compare September 27, 2025 19:42
@groue groue changed the title Support for migrations that obsolete previous migrations Support for migrations that merge previous migrations Sep 27, 2025
@groue groue changed the base branch from master to development September 27, 2025 20:27
@groue groue force-pushed the dev/obsolete-migrations branch from 9a811e1 to 4ec82f0 Compare September 28, 2025 07:46
@groue
Copy link
Copy Markdown
Owner Author

groue commented Sep 30, 2025

@bok- I'm about to merge this pull request, because I think it covers all use cases I could foresee. Apps will have all the information they need in order to implement the logic relevant to their use case.

Your own use case is slightly specific because:

anyone who hasn't updated the app in more than 12 months would get a slightly worse experience when we delete and re-create the database

Maybe you'll write something like:

struct NeedsEraseDatabaseError: Error { }

migrator.registerMigration("v3", merging: ["v1", "v2"]) { db, appliedIDs in
    if appliedIDs.isEmpty == false {
        // The merged migrations were v1 and/or v2 are already applied.
        // We do not support this scenario because we can only
        // apply the v3 schema in its entirety: so let's just reset
        // the database.
        throw NeedsEraseDatabaseError()
    }
    // Apply full schema v3
}

...

do {
    try migrator.migrate(writer)
} catch is NeedsEraseDatabaseError {
    try writer.erase()
    try migrator.migrate(writer)
}

@groue groue changed the title Support for migrations that merge previous migrations Merged Migrations Oct 1, 2025
@groue groue force-pushed the dev/obsolete-migrations branch from 4ec82f0 to 85ad232 Compare October 1, 2025 15:24
@groue groue merged commit 76e0815 into development Oct 2, 2025
15 of 16 checks passed
@groue groue deleted the dev/obsolete-migrations branch October 2, 2025 06:06
@groue
Copy link
Copy Markdown
Owner Author

groue commented Oct 2, 2025

⛵ Shipped in v7.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant