launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.4k stars 1.27k forks source link

Allow for user-provided schema version storage and retrivial #3588

Open Shirogaki opened 4 days ago

Shirogaki commented 4 days ago

Reading the documentation, the migration table is hardcoded to _sqlx_migrations with an implementation defined schema.

Since I'm already going to have a small table for unique key-value pairs, I'd like to store the version schema in that table. If possible, I think an interface like this would be ideal:

let current_schema_version: i64; // User code to fetch the current version.
let new_schema_version = migrate_from!(current_version).await?;
// User code that saves the new version somewhere.

Otherwise if reusing the Migrator struct is preferable:

let current_schema_version: i64; // User code to fetch the current version.
let new_schema_version = migrate!()
    .with_current(current_version)
    .run(&pool)
    .await?;
// User code that saves the new version somewhere.

Describe alternatives you've considered Right now the Migrator object seem to offer a way for the user to define their own migration source with the MigrationSource trait, but it doesn't seem to offer an API to load and retrieve the current version.

abonander commented 4 days ago

If you want to get the current applied version, you can call Migrate::list_applied_migrations() on a connection and look at the last migration in the list.