sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Add migration parameters or the possibility of passing migrations by string

Open vszakd opened this issue 1 year ago • 9 comments

Is your feature request related to a problem? Please describe.

When running migrations from Rust code, the only way (as far as I know) is passing the path to the migrations directory. After this, it is not possible to touch the migrations contents. This makes it impossible to parameterize the migrations. E.g. if a migration needs to be run for multiple schemas.

Describe the solution you'd like

Two complementary solutions could be applied.

The first (easier) step would be modifying the Migrator trait to allow for manual insertion of migrations via string. This way, migration contents would be pushed by the application with arbitrary content. This would solve the problem because the substitution step would be done by the application before passing the migration contents to Migrator.

The second (more complex) step would be to implement in sqlx a variable substitution feature for migrations. So that the application can register the variables and their replacement to the Migrator, which can continue to work with a Path, given that it will take care of variable substitution.

The two solutions could then live side by side for maximum flexibility.

Describe alternatives you've considered

I am trying to work around the problem by manually replacing the content of the migrations field of the Migrator struct, but that does not look like a very clean solution.

vszakd avatar Apr 06 '24 08:04 vszakd

We're interested in this feature as well. Might even be able to implement it if there's buy in from maintainers.

adriangb avatar Aug 14 '24 17:08 adriangb

Also interested. I was intending on providing end users our application binary and the same set of SQL migrations to run as we run on our tech stack. Ideally these would be identical for consistency, but then how might ourselves or end users configure e.g. the password of a particular role? Some substitution of an environment variable in this case would be a solution

peter-lockhart-pub avatar Sep 12 '24 13:09 peter-lockhart-pub