Skip to content

Commit

Permalink
Minor fixes (#2955)
Browse files Browse the repository at this point in the history
* doc link fix
variable declaration moved for better readability

* migration version abstraction
  • Loading branch information
dcodesdev authored Jan 16, 2024
1 parent cdcb2f0 commit 2cc3e0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions sqlx-cli/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:?}")
Expand All @@ -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
);
Expand Down Expand Up @@ -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)
)
}
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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));
}
}
Expand Down
5 changes: 5 additions & 0 deletions sqlx-core/src/migrate/migrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 2cc3e0f

Please sign in to comment.