sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Targeting a specific migration in Migrator

Open rick-de-water opened this issue 11 months ago • 2 comments

I have found these related issues/pull requests

None

Description

I want to write tests for my migrations, making sure that each up and down migration has the expected result. For this I want to target a specific migration, seeds some data, up and/or down a migration, and validate the resulting data and/or schema.

Migrator::run however does not allow you to target a specific migration. It always applies all of the migrations. This is different for Migrator::undo, which can target a specific version.

Prefered solution

Add a function that allows me to select a target version. Perhaps even singular up and down methods which up/down the next/current migration, which are then composed into run, undo and a version of run with a target version.

Is this a breaking change? Why or why not?

Only if the interface of run is updated. Breaking changes can be avoided by adding a separate method, but this will clutter the public API.

rick-de-water avatar Mar 11 '25 17:03 rick-de-water

      - name: Test migrations and revert
        run: |
          sqlx migrate run
          sqlx migrate revert
          sqlx migrate run

Does a simple run-ever and run-again in your test CI does not do the job checking if revert is valid ?

jonatansalemes avatar Mar 19 '25 00:03 jonatansalemes

Does a simple run-ever and run-again in your test CI does not do the job checking if revert is valid ?

No, that only validates if migration script was able to run, not if the original schema was restored.

What I do for testing is query schema information from the database (tables, columns, constraints, etc), do an up followed by a down, query the schema information again and compare it. I do the same for data as well, but allow migrations to specify when they have columns that are lossy.

I've worked around the issue for now by basically writing my own migrator around the Migrate trait, but it would be a lot better if the build in migrator could do the job as well.

rick-de-water avatar Mar 24 '25 22:03 rick-de-water