database
: Create connect
feature
This commit is contained in:
parent
afdba0f672
commit
7b6af31ed5
6 changed files with 78 additions and 24 deletions
|
@ -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" />
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
5
acrate_database/src/config.rs
Normal file
5
acrate_database/src/config.rs
Normal 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,
|
||||
}
|
51
acrate_database/src/connect.rs
Normal file
51
acrate_database/src/connect.rs
Normal 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
|
||||
},
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue