More helpful error messages when creating adaptors

This commit is contained in:
Ben Grant 2023-05-15 20:11:41 +10:00
parent 2ba96cc9cb
commit b586b4c88d
5 changed files with 22 additions and 20 deletions

View file

@ -158,23 +158,28 @@ async fn get_stats_row(db: &DatabaseConnection) -> Result<stats::ActiveModel, Db
impl SqlAdaptor {
pub async fn new() -> Self {
let connection_string = env::var("DATABASE_URL").unwrap();
let connection_string =
env::var("DATABASE_URL").expect("Expected DATABASE_URL environment variable");
// Connect to the database
let db = Database::connect(&connection_string).await.unwrap();
let db = Database::connect(&connection_string)
.await
.expect("Failed to connect to SQL database");
println!(
"{} Connected to database at {}",
match db {
DatabaseConnection::SqlxMySqlPoolConnection(_) => "🐬",
DatabaseConnection::SqlxPostgresPoolConnection(_) => "🐘",
DatabaseConnection::SqlxSqlitePoolConnection(_) => "🪶",
DatabaseConnection::Disconnected => panic!("Failed to connect"),
DatabaseConnection::Disconnected => panic!("Failed to connect to SQL database"),
},
connection_string
);
// Setup tables
Migrator::up(&db, None).await.unwrap();
Migrator::up(&db, None)
.await
.expect("Failed to set up tables in the database");
Self { db }
}