sqlx
sqlx copied to clipboard
[sqlx-cli] Migration hooks
The migration system of sqlx-cli has become usable now that it supports revertible migrations. However such migrations can sometimes be badly written and contain potential errors. As such I usually backup the DB before running any migration but doing so manually is tedious and more importantly, error-prone. As such what I currently do is add this saving mechanism to the migration files. This works but I regret the repetition of said saving mechanism in every migration file. Instead I'd much rather have the ability to create migration hooks (much like git hooks) that would allow to run arbitrary SQL queries before or after running a migration, automatically.
I don't see a compelling reason to have this in SQLx.
If you embed migrations in your application, you can have it execute any commands you want before running them.
If you're using migrations exclusively from the command line, why not write a shell script to do the same? The CLI should be returning a nonzero status on failure, I suppose we could standardize the codes so a shell script or Ansible recipe or whatever can tell which error condition it is.
Right I forgot about embed migrations. And yes I was using a script that just ran custom saving scripts whenever a migrate
or database
command is ran.
I agree that there are other solutions, mainly using embed migrations. But I would still find a way to automatically run some SQL query when a migration is applied useful. If making a hook system is too much for what it's worth I'll simply create a migration that adds a save
function which I will call in my migrations. Less reliable but it should work.
Speaking of which it'd be great for there to be a way to re-order migrations. Currently we have to manually do it and update the checksum in _sqlx_migrations
.