Minor fixes (#2955)

* doc link fix
variable declaration moved for better readability

* migration version abstraction
This commit is contained in:
Dawson 2024-01-17 00:49:25 +03:00 committed by GitHub
parent cdcb2f0f2b
commit 2cc3e0f90b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View file

@ -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));
}
}

View file

@ -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.
///