diff --git a/src/db/mod.rs b/src/db/mod.rs index 53012844f2..8d04fb6cd3 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -435,11 +435,29 @@ mod sqlite_migrations { } } - use diesel::{Connection, RunQueryDsl}; - // Make sure the database is up to date (create if it doesn't exist, or run the migrations) - let mut connection = diesel::sqlite::SqliteConnection::establish(&crate::CONFIG.database_url())?; + use diesel::{result::ConnectionError::BadConnection, Connection, RunQueryDsl}; + // Establish a connection to the sqlite database (this will create a new one, if it does + // not exist, and exit if there is an error). + let mut connection = match diesel::sqlite::SqliteConnection::establish(&url) { + Ok(conn) => { + debug!("Connected to SQLite database {url}"); + conn + }, + Err(e) => match e { + BadConnection(reason) => { + error!("Could not establish a connection to `{url}`: {reason}"); + std::process::exit(1); + } + _ => { + // print the error and exit + error!("{e:?}"); + std::process::exit(1); + } + }, + }; + + // Run the migrations after successfully establishing a connection // Disable Foreign Key Checks during migration - // Scoped to a connection. diesel::sql_query("PRAGMA foreign_keys = OFF") .execute(&mut connection)