Skip to content

Commit 2cc3e0f

Browse files
authored
Minor fixes (#2955)
* doc link fix variable declaration moved for better readability * migration version abstraction
1 parent cdcb2f0 commit 2cc3e0f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

sqlx-cli/src/migrate.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ pub async fn add(
109109
) -> anyhow::Result<()> {
110110
fs::create_dir_all(migration_source).context("Unable to create migrations directory")?;
111111

112-
// if the migrations directory is empty
113-
let has_existing_migrations = fs::read_dir(migration_source)
114-
.map(|mut dir| dir.next().is_some())
115-
.unwrap_or(false);
116-
117112
let migrator = Migrator::new(Path::new(migration_source)).await?;
118113
// Type of newly created migration will be the same as the first one
119114
// or reversible flag if this is the first migration
@@ -144,6 +139,11 @@ pub async fn add(
144139
)?;
145140
}
146141

142+
// if the migrations directory is empty
143+
let has_existing_migrations = fs::read_dir(migration_source)
144+
.map(|mut dir| dir.next().is_some())
145+
.unwrap_or(false);
146+
147147
if !has_existing_migrations {
148148
let quoted_source = if migration_source != "migrations" {
149149
format!("{migration_source:?}")
@@ -163,7 +163,7 @@ sqlx::migrate!({}).run(<&your_pool OR &mut your_connection>).await?;
163163
Note that the compiler won't pick up new migrations if no Rust source files have changed.
164164
You can create a Cargo build script to work around this with `sqlx migrate build-script`.
165165
166-
See: https://docs.rs/sqlx/0.5/sqlx/macro.migrate.html
166+
See: https://docs.rs/sqlx/latest/sqlx/macro.migrate.html
167167
"#,
168168
quoted_source
169169
);
@@ -228,7 +228,7 @@ pub async fn info(migration_source: &str, connect_opts: &ConnectOpts) -> anyhow:
228228
),
229229
);
230230
println!(
231-
"local migration has checksum {}",
231+
"local migration has checksum {}",
232232
short_checksum(&migration.checksum)
233233
)
234234
}
@@ -268,7 +268,7 @@ pub async fn run(
268268
) -> anyhow::Result<()> {
269269
let migrator = Migrator::new(Path::new(migration_source)).await?;
270270
if let Some(target_version) = target_version {
271-
if !migrator.iter().any(|m| target_version == m.version) {
271+
if !migrator.version_exists(target_version) {
272272
bail!(MigrateError::VersionNotPresent(target_version));
273273
}
274274
}
@@ -363,7 +363,7 @@ pub async fn revert(
363363
) -> anyhow::Result<()> {
364364
let migrator = Migrator::new(Path::new(migration_source)).await?;
365365
if let Some(target_version) = target_version {
366-
if target_version != 0 && !migrator.iter().any(|m| target_version == m.version) {
366+
if target_version != 0 && !migrator.version_exists(target_version) {
367367
bail!(MigrateError::VersionNotPresent(target_version));
368368
}
369369
}

sqlx-core/src/migrate/migrator.rs

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ impl Migrator {
8686
self.migrations.iter()
8787
}
8888

89+
/// Check if a migration version exists.
90+
pub fn version_exists(&self, version: i64) -> bool {
91+
self.iter().any(|m| m.version == version)
92+
}
93+
8994
/// Run any pending migrations against the database; and, validate previously applied migrations
9095
/// against the current migration source to detect accidental changes in previously-applied migrations.
9196
///

0 commit comments

Comments
 (0)