mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-01-08 09:41:17 +00:00
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:
parent
6b828e698f
commit
500cd18f19
@ -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`.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user