Skip to content

Commit 402488d

Browse files
committed
improve migration error messages
1 parent 049b65c commit 402488d

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- set a custom `search_path`, `application_name` or other variables in PostgreSQL
3333
- create temporary tables that will be available to all SQLPage queries but will not be persisted in the database
3434
- [`ATTACH`](https://www.sqlite.org/lang_attach.html) a database in SQLite to query multiple database files at once
35+
- Better error messages. SQLPage displays a more precise and useful message when an error occurs, and displays the position in the SQL statement where the error occured. Incorrect error messages on invalid migrations are also fixed.
3536

3637
## 0.11.0 (2023-09-17)
3738
- Support for **environment variables** ! You can now read environment variables from sql code using `sqlpage.environment_variable('VAR_NAME')`.

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ panic = "abort"
1717
codegen-units = 2
1818

1919
[dependencies]
20-
sqlx = { package = "sqlx-oldapi", version = "0.6.13", features = ["any", "runtime-actix-rustls", "sqlite", "postgres", "mysql", "mssql", "chrono", "json" ] }
20+
sqlx = { package = "sqlx-oldapi", version = "0.6.14", features = ["any", "runtime-actix-rustls", "sqlite", "postgres", "mysql", "mssql", "chrono", "json" ] }
2121
chrono = "0.4.23"
2222
actix-web = { version = "4", features = ["rustls", "cookies"] }
2323
percent-encoding = "2.2.0"

src/webserver/database/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use sql::ParsedSqlFile;
2222
use sqlx::any::{
2323
AnyArguments, AnyConnectOptions, AnyKind, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo,
2424
};
25-
use sqlx::migrate::Migrator;
25+
use sqlx::migrate::{MigrateError, Migrator};
2626
use sqlx::pool::{PoolConnection, PoolOptions};
2727
use sqlx::query::Query;
2828
use sqlx::{
@@ -82,8 +82,20 @@ pub async fn apply_migrations(db: &Database) -> anyhow::Result<()> {
8282
m.description
8383
);
8484
}
85-
migrator.run(&db.connection).await.with_context(|| {
86-
format!("There is an error in the database migrations in {MIGRATIONS_DIR:?}")
85+
migrator.run(&db.connection).await.map_err(|err| {
86+
match err {
87+
MigrateError::Execute(n, source) => {
88+
let migration = migrator.iter().find(|&m| m.version == n).unwrap();
89+
anyhow::Error::new(source).context(format!(
90+
"Failed to apply migration [{:04}] {:?} {}",
91+
migration.version, migration.migration_type, migration.description
92+
))
93+
}
94+
source => anyhow::Error::new(source),
95+
}
96+
.context(format!(
97+
"Failed to apply database migrations from {MIGRATIONS_DIR:?}"
98+
))
8799
})?;
88100
Ok(())
89101
}

0 commit comments

Comments
 (0)