Skip to content

Commit

Permalink
improve inital sqlite connection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan0xC committed Oct 28, 2022
1 parent 108ddea commit 7bed13d
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/db/mod.rs
Expand Up @@ -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)
Expand Down

0 comments on commit 7bed13d

Please sign in to comment.