sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Add run_through_version method for versioned migrations

Open breuhan opened this issue 7 months ago • 2 comments

Description:

The sqlx library already has a feature in sqlx-cli https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/src/migrate.rs To run migration till a specific version. Unfortunately this functionality isn't yet exposed to the library.

Therefore, this PR introduces three new public functions to the Migrator struct:

  • latest_version(): Returns the highest version number among all migrations defined in the Migrator's source.
  • latest_applied_version(): Retrieves the highest version number of migrations that have already been successfully applied to the database.
  • run_through_version(target: i64): Allows running "up" migrations against the database specifically up to a target version. This is enabling users to apply only a subset of pending migrations.

I also added tests to run the new functionality against different database backends.

Is this a breaking change?

no, everything is backwards and forwards compatible. Only added function

breuhan avatar May 28 '25 11:05 breuhan

I already have a similar change in https://github.com/launchbadge/sqlx/pull/3383/files#diff-93b9b9f2d9c2f7fe607f3fca79da6def6664450fe4105f0f3d1c3478fd5d2a32R160

abonander avatar Jun 03 '25 02:06 abonander

Yes it is a similar change, but on a deeper level. What is missing from that PR is the query for the latest applied migration and the query for the latest_version() from the Migration source. I'm also happy if that could be integrated.

breuhan avatar Jun 03 '25 15:06 breuhan