1
0
Fork 0
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:
Mo 2022-12-16 18:16:59 +01:00
parent afb9f087b3
commit 05bd6576a9

View file

@ -1,7 +1,9 @@
use anyhow::{Context, Result};
use chrono::offset::Local;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
use diesel::{
prelude::*,
r2d2::{ConnectionManager, Pool, PooledConnection},
};
use std::env;
use tracing::error;
@ -9,21 +11,23 @@ use crate::config::Hook;
use crate::models::{HookLog, NewHookLog};
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> {
let database_url_var = "DATABASE_URL";
let database_url = env::var(database_url_var)
.with_context(|| format!("Environment variable {database_url_var} missing!"))?;
let manager = ConnectionManager::<SqliteConnection>::new(database_url);
let manager = DBConnectionManager::new(database_url);
Pool::builder()
.build(manager)
.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!")
}