Skip to content

Commit 500cd18

Browse files
authored
Add Migrator::with_migrations() constructor (#4020)
* Add the helper function with_migrations() to Migrator. * cargo fmt * cargo fmt * cargo fmt * Improve comments.
1 parent 6b828e6 commit 500cd18

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

sqlx-core/src/migrate/migrator.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ impl Migrator {
6969
})
7070
}
7171

72+
/// Creates a new instance with the given migrations.
73+
///
74+
///
75+
/// # Examples
76+
///
77+
/// ```rust,no_run
78+
/// use sqlx::{ SqlSafeStr, migrate::{Migration, MigrationType::*, Migrator}};
79+
///
80+
/// // Define your migrations.
81+
/// // You can also use include_str!("./xxx.sql") instead of hard-coded SQL statements.
82+
/// let migrations = vec![
83+
/// Migration::new(1, "user".into(), ReversibleUp, "create table uesrs ( ... )".into_sql_str(), false),
84+
/// Migration::new(2, "post".into(), ReversibleUp, "create table posts ( ... )".into_sql_str(), false),
85+
/// // add more...
86+
/// ];
87+
/// let m = Migrator::with_migrations(migrations);
88+
/// ```
89+
pub fn with_migrations(mut migrations: Vec<Migration>) -> Self {
90+
// Ensure that we are sorted by version in ascending order.
91+
migrations.sort_by_key(|m| m.version);
92+
Self {
93+
migrations: Cow::Owned(migrations),
94+
..Self::DEFAULT
95+
}
96+
}
97+
7298
/// Override the name of the table used to track executed migrations.
7399
///
74100
/// May be schema-qualified and/or contain quotes. Defaults to `_sqlx_migrations`.

0 commit comments

Comments
 (0)