Add Migrator::with_migrations() constructor (#4020)

* Add the helper function with_migrations() to Migrator.

* cargo fmt

* cargo fmt

* cargo fmt

* Improve comments.
This commit is contained in:
Paul Xu 2025-09-09 05:01:23 +08:00 committed by GitHub
parent 6b828e698f
commit 500cd18f19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -69,6 +69,32 @@ impl Migrator {
})
}
/// Creates a new instance with the given migrations.
///
///
/// # Examples
///
/// ```rust,no_run
/// use sqlx::{ SqlSafeStr, migrate::{Migration, MigrationType::*, Migrator}};
///
/// // Define your migrations.
/// // You can also use include_str!("./xxx.sql") instead of hard-coded SQL statements.
/// let migrations = vec![
/// Migration::new(1, "user".into(), ReversibleUp, "create table uesrs ( ... )".into_sql_str(), false),
/// Migration::new(2, "post".into(), ReversibleUp, "create table posts ( ... )".into_sql_str(), false),
/// // add more...
/// ];
/// let m = Migrator::with_migrations(migrations);
/// ```
pub fn with_migrations(mut migrations: Vec<Migration>) -> Self {
// Ensure that we are sorted by version in ascending order.
migrations.sort_by_key(|m| m.version);
Self {
migrations: Cow::Owned(migrations),
..Self::DEFAULT
}
}
/// Override the name of the table used to track executed migrations.
///
/// May be schema-qualified and/or contain quotes. Defaults to `_sqlx_migrations`.