Skip to content

Commit

Permalink
improve migration error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Oct 1, 2023
1 parent 049b65c commit 402488d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- set a custom `search_path`, `application_name` or other variables in PostgreSQL
- create temporary tables that will be available to all SQLPage queries but will not be persisted in the database
- [`ATTACH`](https://www.sqlite.org/lang_attach.html) a database in SQLite to query multiple database files at once
- 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.

## 0.11.0 (2023-09-17)
- Support for **environment variables** ! You can now read environment variables from sql code using `sqlpage.environment_variable('VAR_NAME')`.
Expand Down
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ panic = "abort"
codegen-units = 2

[dependencies]
sqlx = { package = "sqlx-oldapi", version = "0.6.13", features = ["any", "runtime-actix-rustls", "sqlite", "postgres", "mysql", "mssql", "chrono", "json" ] }
sqlx = { package = "sqlx-oldapi", version = "0.6.14", features = ["any", "runtime-actix-rustls", "sqlite", "postgres", "mysql", "mssql", "chrono", "json" ] }
chrono = "0.4.23"
actix-web = { version = "4", features = ["rustls", "cookies"] }
percent-encoding = "2.2.0"
Expand Down
18 changes: 15 additions & 3 deletions src/webserver/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use sql::ParsedSqlFile;
use sqlx::any::{
AnyArguments, AnyConnectOptions, AnyKind, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo,
};
use sqlx::migrate::Migrator;
use sqlx::migrate::{MigrateError, Migrator};
use sqlx::pool::{PoolConnection, PoolOptions};
use sqlx::query::Query;
use sqlx::{
Expand Down Expand Up @@ -82,8 +82,20 @@ pub async fn apply_migrations(db: &Database) -> anyhow::Result<()> {
m.description
);
}
migrator.run(&db.connection).await.with_context(|| {
format!("There is an error in the database migrations in {MIGRATIONS_DIR:?}")
migrator.run(&db.connection).await.map_err(|err| {
match err {
MigrateError::Execute(n, source) => {
let migration = migrator.iter().find(|&m| m.version == n).unwrap();
anyhow::Error::new(source).context(format!(
"Failed to apply migration [{:04}] {:?} {}",
migration.version, migration.migration_type, migration.description
))
}
source => anyhow::Error::new(source),
}
.context(format!(
"Failed to apply database migrations from {MIGRATIONS_DIR:?}"
))
})?;
Ok(())
}
Expand Down

0 comments on commit 402488d

Please sign in to comment.