From 7ffb7e23c6701845b75a6d9d360e8d815dc23b41 Mon Sep 17 00:00:00 2001 From: Dawson <101001810+Dawsoncodes@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:49:25 +0300 Subject: [PATCH] Minor fixes (#2955) * doc link fix variable declaration moved for better readability * migration version abstraction --- sqlx-cli/src/migrate.rs | 18 +++++++++--------- sqlx-core/src/migrate/migrator.rs | 5 +++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/sqlx-cli/src/migrate.rs b/sqlx-cli/src/migrate.rs index ef18051369..1ab8929fc1 100644 --- a/sqlx-cli/src/migrate.rs +++ b/sqlx-cli/src/migrate.rs @@ -109,11 +109,6 @@ pub async fn add( ) -> anyhow::Result<()> { fs::create_dir_all(migration_source).context("Unable to create migrations directory")?; - // if the migrations directory is empty - let has_existing_migrations = fs::read_dir(migration_source) - .map(|mut dir| dir.next().is_some()) - .unwrap_or(false); - let migrator = Migrator::new(Path::new(migration_source)).await?; // Type of newly created migration will be the same as the first one // or reversible flag if this is the first migration @@ -144,6 +139,11 @@ pub async fn add( )?; } + // if the migrations directory is empty + let has_existing_migrations = fs::read_dir(migration_source) + .map(|mut dir| dir.next().is_some()) + .unwrap_or(false); + if !has_existing_migrations { let quoted_source = if migration_source != "migrations" { format!("{migration_source:?}") @@ -163,7 +163,7 @@ sqlx::migrate!({}).run(<&your_pool OR &mut your_connection>).await?; Note that the compiler won't pick up new migrations if no Rust source files have changed. You can create a Cargo build script to work around this with `sqlx migrate build-script`. -See: https://docs.rs/sqlx/0.5/sqlx/macro.migrate.html +See: https://docs.rs/sqlx/latest/sqlx/macro.migrate.html "#, quoted_source ); @@ -228,7 +228,7 @@ pub async fn info(migration_source: &str, connect_opts: &ConnectOpts) -> anyhow: ), ); println!( - "local migration has checksum {}", + "local migration has checksum {}", short_checksum(&migration.checksum) ) } @@ -268,7 +268,7 @@ pub async fn run( ) -> anyhow::Result<()> { let migrator = Migrator::new(Path::new(migration_source)).await?; if let Some(target_version) = target_version { - if !migrator.iter().any(|m| target_version == m.version) { + if !migrator.version_exists(target_version) { bail!(MigrateError::VersionNotPresent(target_version)); } } @@ -363,7 +363,7 @@ pub async fn revert( ) -> anyhow::Result<()> { let migrator = Migrator::new(Path::new(migration_source)).await?; if let Some(target_version) = target_version { - if target_version != 0 && !migrator.iter().any(|m| target_version == m.version) { + if target_version != 0 && !migrator.version_exists(target_version) { bail!(MigrateError::VersionNotPresent(target_version)); } } diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 585d6d9d86..9a4d7a7a9f 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -86,6 +86,11 @@ impl Migrator { self.migrations.iter() } + /// Check if a migration version exists. + pub fn version_exists(&self, version: i64) -> bool { + self.iter().any(|m| m.version == version) + } + /// Run any pending migrations against the database; and, validate previously applied migrations /// against the current migration source to detect accidental changes in previously-applied migrations. ///