database: Create connect feature

This commit is contained in:
Steffo 2024-12-13 02:43:06 +01:00
parent afdba0f672
commit 7b6af31ed5
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
6 changed files with 78 additions and 24 deletions

View file

@ -11,6 +11,7 @@
<sourceFolder url="file://$MODULE_DIR$/acrate_rd/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/acrate_mime/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/acrate_rdserver/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/acrate_database/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

View file

@ -21,7 +21,9 @@ pretty_env_logger = { version = "0.5.0", optional = true }
uuid = "1.11.0"
[features]
bin = ["diesel_migrations", "micronfig", "pretty_env_logger"]
default = ["connect"]
bin = ["diesel_migrations", "pretty_env_logger", "connect"]
connect = ["micronfig"]
[lib]
name = "acrate_database"

View file

@ -15,36 +15,23 @@
use std::process::exit;
use diesel::{Connection, PgConnection};
use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
mod config {
micronfig::config! {
ACRATE_DATABASE_DATABASE_URL: String,
}
}
use acrate_database::connect::managed_connect_sync;
pub const MIGRATIONS: EmbeddedMigrations = diesel_migrations::embed_migrations!();
fn main() {
pretty_env_logger::init();
log::info!("Logging initialized successfully!");
log::trace!("Determining database URL...");
let db = config::ACRATE_DATABASE_DATABASE_URL();
log::debug!("Connecting to: {db:?}");
let mut db = match PgConnection::establish(db) {
Err(e) => {
log::error!("Failed to connect to the PostgreSQL database: {e:#?}");
exit(1);
}
Ok(db) => db,
};
fn managed_run_migrations(mut db: PgConnection) {
log::debug!("Running migrations...");
if let Err(e) = db.run_pending_migrations(MIGRATIONS) {
log::error!("Failed to perform migration: {e:#?}");
exit(2);
};
log::info!("Migrations applied successfully!")
}
fn main() {
pretty_env_logger::init();
log::info!("Logging initialized successfully!");
let db = managed_connect_sync();
managed_run_migrations(db);
}

View file

@ -0,0 +1,5 @@
//! Configuration relative to the database that applies to all crates making use of it.
micronfig::config! {
ACRATE_DATABASE_URL: String,
}

View file

@ -0,0 +1,51 @@
use diesel::{Connection, ConnectionResult, PgConnection};
use diesel_async::{AsyncConnection, AsyncPgConnection};
use crate::config;
/// Create a [`PgConnection`] to the configured database.
pub fn connect_sync() -> ConnectionResult<PgConnection> {
log::trace!("Determining the database URL for a sync connection...");
let database_url = config::ACRATE_DATABASE_URL();
log::trace!("Establishing a sync connection to the database at: {database_url:?}");
PgConnection::establish(database_url)
}
/// Create an [`AsyncPgConnection`] to the configured database.
pub async fn connect_async() -> ConnectionResult<AsyncPgConnection> {
log::trace!("Determining the database URL for an async connection...");
let database_url = config::ACRATE_DATABASE_URL();
log::trace!("Establishing an async connection to the database at: {database_url:?}");
AsyncPgConnection::establish(database_url).await
}
/// Run [`connect_sync`], then handle errors by logging the error and exiting the process with `1`.
pub fn managed_connect_sync() -> PgConnection {
log::trace!("Attempting a managed sync connection to the database...");
match connect_sync() {
Err(e) => {
log::error!("Failed to connect to the database: {e:#?}");
std::process::exit(1);
},
Ok(conn) => {
conn
},
}
}
/// Run [`connect_async`], then handle errors by logging the error and exiting the process with `1`.
pub async fn managed_connect_async() -> AsyncPgConnection {
log::trace!("Attempting a managed async connection to the database...");
match connect_async().await {
Err(e) => {
log::error!("Failed to connect to the database: {e:#?}");
std::process::exit(1);
},
Ok(conn) => {
conn
},
}
}

View file

@ -6,7 +6,15 @@
mod schema;
pub mod meta;
mod macros;
#[cfg(feature = "connect")]
mod config;
#[cfg(feature = "connect")]
pub mod connect;
pub use diesel;
pub use diesel_async;