adc7c61ee2
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
//! Database access. All SQL lives in this crate.
|
|
|
|
pub mod audit;
|
|
pub mod authority;
|
|
pub mod catalog;
|
|
pub mod fields;
|
|
pub mod seed;
|
|
pub mod vocab;
|
|
|
|
use sqlx::postgres::{PgPool, PgPoolOptions};
|
|
|
|
/// A handle to the organization's PostgreSQL database.
|
|
#[derive(Clone)]
|
|
pub struct Db {
|
|
pool: PgPool,
|
|
}
|
|
|
|
impl Db {
|
|
/// Connect to the database at `database_url`, opening a connection pool.
|
|
pub async fn connect(database_url: &str) -> Result<Self, sqlx::Error> {
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(5)
|
|
.connect(database_url)
|
|
.await?;
|
|
|
|
Ok(Self { pool })
|
|
}
|
|
|
|
/// Build a handle from an existing pool (used in tests).
|
|
pub fn from_pool(pool: PgPool) -> Self {
|
|
Self { pool }
|
|
}
|
|
|
|
/// Borrow the underlying pool for repository modules.
|
|
pub fn pool(&self) -> &PgPool {
|
|
&self.pool
|
|
}
|
|
|
|
/// Readiness check: run a trivial query to confirm the database answers.
|
|
pub async fn ping(&self) -> Result<(), sqlx::Error> {
|
|
sqlx::query_scalar::<_, i32>("SELECT 1")
|
|
.fetch_one(&self.pool)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Apply all pending schema migrations (embedded at compile time).
|
|
///
|
|
/// Pre-1.0 the migration files are rewritten freely and dev databases are
|
|
/// recreated; this is the schema-bootstrap mechanism, not forward-migration
|
|
/// discipline.
|
|
pub async fn migrate(&self) -> Result<(), sqlx::migrate::MigrateError> {
|
|
sqlx::migrate!().run(&self.pool).await
|
|
}
|
|
}
|