mirror of
https://codeberg.org/Mo8it/git-webhook-client
synced 2024-11-21 11:06:32 +00:00
Define DB types
This commit is contained in:
parent
afb9f087b3
commit
05bd6576a9
1 changed files with 9 additions and 5 deletions
14
src/db.rs
14
src/db.rs
|
@ -1,7 +1,9 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use chrono::offset::Local;
|
use chrono::offset::Local;
|
||||||
use diesel::prelude::*;
|
use diesel::{
|
||||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
prelude::*,
|
||||||
|
r2d2::{ConnectionManager, Pool, PooledConnection},
|
||||||
|
};
|
||||||
use std::env;
|
use std::env;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
@ -9,21 +11,23 @@ use crate::config::Hook;
|
||||||
use crate::models::{HookLog, NewHookLog};
|
use crate::models::{HookLog, NewHookLog};
|
||||||
use crate::schema::hooklog;
|
use crate::schema::hooklog;
|
||||||
|
|
||||||
pub type DBPool = Pool<ConnectionManager<SqliteConnection>>;
|
type DBConnectionManager = ConnectionManager<SqliteConnection>;
|
||||||
|
pub type DBPool = Pool<DBConnectionManager>;
|
||||||
|
type DBConnection = PooledConnection<DBConnectionManager>;
|
||||||
|
|
||||||
pub fn establish_connection_pool() -> Result<DBPool> {
|
pub fn establish_connection_pool() -> Result<DBPool> {
|
||||||
let database_url_var = "DATABASE_URL";
|
let database_url_var = "DATABASE_URL";
|
||||||
let database_url = env::var(database_url_var)
|
let database_url = env::var(database_url_var)
|
||||||
.with_context(|| format!("Environment variable {database_url_var} missing!"))?;
|
.with_context(|| format!("Environment variable {database_url_var} missing!"))?;
|
||||||
|
|
||||||
let manager = ConnectionManager::<SqliteConnection>::new(database_url);
|
let manager = DBConnectionManager::new(database_url);
|
||||||
|
|
||||||
Pool::builder()
|
Pool::builder()
|
||||||
.build(manager)
|
.build(manager)
|
||||||
.context("Could not build database connection pool!")
|
.context("Could not build database connection pool!")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_conn(pool: &DBPool) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>> {
|
fn get_conn(pool: &DBPool) -> Result<DBConnection> {
|
||||||
pool.get().context("Could not get database pool!")
|
pool.get().context("Could not get database pool!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue