-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Rust migration scripts #1105
Comments
I'll be honest, when I first read the title of this issue in the Github notification email, I was absolutely disgusted at the idea. You have to admit, it's kind of ridiculous at face value. After thinking on it for a bit it's started to grow on me, however still the implementation wouldn't exactly be trivial. For embedded migrations it would be relatively straightforward as we could just read the migration file and spit out the Rust code at the site of the However, to make it work with
I'm leaning towards 2 as it's simple and easy to implement, and it would allow CI to compile-check migrations without needing The other issue is that the current migration API isn't designed to invoke a function as written and the function would probably need to generate and return a So we'd probably have to move a bunch of stuff around in migrations in order to accommodate this feature to actually have the desired flexibility of just running arbitrary commands on the database procedurally. |
Couldn't you just generate a single migrator binary that compiles in all the migrations, and runs one specified via the command-line? Then you'd only need one |
That is certainly an option, yeah, although the binary code would need to be modified every time a new migration is added which would still be causing a file to inflate over time, just a different one. The Cargo.toml would be easier to append to, though. |
// 20210314225352_name.rs
#[sqlx::migration] // generates fn main() that calls this migration function
pub async fn migrate_default_materials(
txn: &mut SqliteTransaction,
) -> Result<()> {
// Create tables and data
Ok(())
} |
Not all conversions can happen in SQL, especially those involving multiple tables, complex filtering, etc. Rust code migrations would be awesome. I've used similar techniques in other languages/frameworks. |
I've made an experimental library that explores this idea with build-time code generation instead of macros. |
Bumping for interest. Some libraries that allow a similar approach: |
https://github.com/iamsauravsharma/sqlx_migrator is kinda this, I believe? Would be awesome to have it work with |
It would be helpful to have the option to define migrations in Rust instead of SQL, eg:
Creating a file something like this:
Is this something that could be added to sqlx migrations?
Edit: Adjusting the format of the generated rust file.
The text was updated successfully, but these errors were encountered: