From 7b6af31ed55c19e27d44e21b5673a357469e83ee Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 13 Dec 2024 02:43:06 +0100 Subject: [PATCH] `database`: Create `connect` feature --- .idea/acrate.iml | 1 + acrate_database/Cargo.toml | 4 +- .../src/bin/acrate_database_migrate.rs | 33 ++++-------- acrate_database/src/config.rs | 5 ++ acrate_database/src/connect.rs | 51 +++++++++++++++++++ acrate_database/src/lib.rs | 8 +++ 6 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 acrate_database/src/config.rs create mode 100644 acrate_database/src/connect.rs diff --git a/.idea/acrate.iml b/.idea/acrate.iml index ac0db0c..8ef4dcf 100644 --- a/.idea/acrate.iml +++ b/.idea/acrate.iml @@ -11,6 +11,7 @@ + diff --git a/acrate_database/Cargo.toml b/acrate_database/Cargo.toml index d2afc81..f920aef 100644 --- a/acrate_database/Cargo.toml +++ b/acrate_database/Cargo.toml @@ -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" diff --git a/acrate_database/src/bin/acrate_database_migrate.rs b/acrate_database/src/bin/acrate_database_migrate.rs index c678882..b99a4a9 100644 --- a/acrate_database/src/bin/acrate_database_migrate.rs +++ b/acrate_database/src/bin/acrate_database_migrate.rs @@ -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); +} diff --git a/acrate_database/src/config.rs b/acrate_database/src/config.rs new file mode 100644 index 0000000..e2d0bfe --- /dev/null +++ b/acrate_database/src/config.rs @@ -0,0 +1,5 @@ +//! Configuration relative to the database that applies to all crates making use of it. + +micronfig::config! { + ACRATE_DATABASE_URL: String, +} diff --git a/acrate_database/src/connect.rs b/acrate_database/src/connect.rs new file mode 100644 index 0000000..5127692 --- /dev/null +++ b/acrate_database/src/connect.rs @@ -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 { + 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 { + 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 + }, + } +} diff --git a/acrate_database/src/lib.rs b/acrate_database/src/lib.rs index 42af5be..66a61d0 100644 --- a/acrate_database/src/lib.rs +++ b/acrate_database/src/lib.rs @@ -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;