From 6bfce2df6c64cf0bc5b2989593d26d4b29b1b903 Mon Sep 17 00:00:00 2001 From: Paul Xu <284524239@qq.com> Date: Mon, 8 Sep 2025 15:17:50 +0800 Subject: [PATCH 1/5] Add the helper function with_migrations() to Migrator. --- sqlx-core/src/migrate/migrator.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 1ae4813106..8dbb2f5088 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -69,6 +69,30 @@ impl Migrator { }) } + /// Creates a new instance with the given migrations. + /// + /// # Examples + /// + /// ```rust,no_run + /// use sqlx::{ SqlSafeStr, migrate::{Migration, MigrationType::*, Migrator}}; + /// + /// // Define your migrations. + /// let migrations = vec![ + /// Migration::new(1, "init".into(), ReversibleUp, include_str!("./s1.sql").into_sql_str(), false), + /// Migration::new(2, "change".into(), ReversibleUp, include_str!("./s2.sql").into_sql_str(), false), + /// // add more... + /// ]; + /// let m = Migrator::with_migrations(migrations); + /// ``` + pub fn with_migrations(mut migrations: Vec) -> 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`. From 0a32b2047174ed8649d89fdfd624ef3122091237 Mon Sep 17 00:00:00 2001 From: Paul Xu <284524239@qq.com> Date: Mon, 8 Sep 2025 15:38:45 +0800 Subject: [PATCH 2/5] cargo fmt --- sqlx-core/src/migrate/migrator.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 8dbb2f5088..238c61a8c2 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -75,11 +75,11 @@ impl Migrator { /// /// ```rust,no_run /// use sqlx::{ SqlSafeStr, migrate::{Migration, MigrationType::*, Migrator}}; - /// + /// /// // Define your migrations. /// let migrations = vec![ - /// Migration::new(1, "init".into(), ReversibleUp, include_str!("./s1.sql").into_sql_str(), false), - /// Migration::new(2, "change".into(), ReversibleUp, include_str!("./s2.sql").into_sql_str(), false), + /// Migration::new(1, "user".into(), ReversibleUp, include_str!("./migrations/1_user.sql").into_sql_str(), false), + /// Migration::new(2, "post".into(), ReversibleUp, include_str!("./migrations/2_post.sql").into_sql_str(), false), /// // add more... /// ]; /// let m = Migrator::with_migrations(migrations); From 11c2195d4b494cc6c4a3cf7a5b0c5549cf402261 Mon Sep 17 00:00:00 2001 From: Paul Xu <284524239@qq.com> Date: Mon, 8 Sep 2025 15:59:44 +0800 Subject: [PATCH 3/5] cargo fmt --- sqlx-core/src/migrate/migrator.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 238c61a8c2..2f590a249a 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -71,15 +71,17 @@ 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!() instead of hard-coded SQL statements. /// let migrations = vec![ - /// Migration::new(1, "user".into(), ReversibleUp, include_str!("./migrations/1_user.sql").into_sql_str(), false), - /// Migration::new(2, "post".into(), ReversibleUp, include_str!("./migrations/2_post.sql").into_sql_str(), false), + /// 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); From e03c2d5dd9522a09ed7a7478d52838f4c7fe4f9f Mon Sep 17 00:00:00 2001 From: Paul Xu <284524239@qq.com> Date: Mon, 8 Sep 2025 16:02:23 +0800 Subject: [PATCH 4/5] cargo fmt --- sqlx-core/src/migrate/migrator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 2f590a249a..f0c826d82b 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -80,8 +80,8 @@ impl Migrator { /// // Define your migrations. /// // You can also use include_str!() 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), + /// 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); From 7ea581ef1df64a6c06b445e36d5b894e7447c2dc Mon Sep 17 00:00:00 2001 From: Paul Xu <284524239@qq.com> Date: Mon, 8 Sep 2025 16:24:24 +0800 Subject: [PATCH 5/5] Improve comments. --- sqlx-core/src/migrate/migrator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index f0c826d82b..375c2af3fd 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -78,7 +78,7 @@ impl Migrator { /// use sqlx::{ SqlSafeStr, migrate::{Migration, MigrationType::*, Migrator}}; /// /// // Define your migrations. - /// // You can also use include_str!() instead of hard-coded SQL statements. + /// // 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),