1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-10-18 07:22:39 +00:00

Run pending migrations

This commit is contained in:
Mo 2022-12-24 20:29:44 +01:00
parent 2f83f1f207
commit 0aba8b543f
3 changed files with 16 additions and 2 deletions

View file

@ -16,6 +16,7 @@ axum-extra = { version = "0.4", features = ["spa"] }
bytes = "1.3" bytes = "1.3"
chrono = { version = "0.4", default-features = false, features = ["clock"] } chrono = { version = "0.4", default-features = false, features = ["clock"] }
diesel = { version = "2.0", features = ["r2d2", "sqlite", "returning_clauses_for_sqlite_3_35", "without-deprecated"] } diesel = { version = "2.0", features = ["r2d2", "sqlite", "returning_clauses_for_sqlite_3_35", "without-deprecated"] }
diesel_migrations = { version = "2.0.0", features = ["sqlite"] }
hex = "0.4" hex = "0.4"
hmac = "0.12" hmac = "0.12"
lettre = { version = "0.10", default-features = false, features = ["smtp-transport", "hostname", "rustls-tls", "pool", "builder"] } lettre = { version = "0.10", default-features = false, features = ["smtp-transport", "hostname", "rustls-tls", "pool", "builder"] }

View file

@ -1,9 +1,11 @@
use anyhow::{Context, Result}; use anyhow::{anyhow, Context, Result};
use chrono::offset::Local; use chrono::offset::Local;
use diesel::{ use diesel::{
prelude::*, prelude::*,
r2d2::{ConnectionManager, Pool, PooledConnection}, r2d2::{ConnectionManager, Pool, PooledConnection},
sqlite::Sqlite,
}; };
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use std::env; use std::env;
use tracing::error; use tracing::error;
@ -13,10 +15,19 @@ use crate::{
schema::hooklog, schema::hooklog,
}; };
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
type DBConnectionManager = ConnectionManager<SqliteConnection>; type DBConnectionManager = ConnectionManager<SqliteConnection>;
pub type DBPool = Pool<DBConnectionManager>; pub type DBPool = Pool<DBConnectionManager>;
type DBConnection = PooledConnection<DBConnectionManager>; type DBConnection = PooledConnection<DBConnectionManager>;
pub fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<()> {
match connection.run_pending_migrations(MIGRATIONS) {
Err(e) => Err(anyhow!("{e:?}")),
Ok(_) => Ok(()),
}
}
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)
@ -29,7 +40,7 @@ pub fn establish_connection_pool() -> Result<DBPool> {
.context("Could not build database connection pool!") .context("Could not build database connection pool!")
} }
fn get_conn(pool: &DBPool) -> Result<DBConnection> { pub fn get_conn(pool: &DBPool) -> Result<DBConnection> {
pool.get().context("Could not get database pool!") pool.get().context("Could not get database pool!")
} }

View file

@ -37,6 +37,8 @@ async fn init() -> Result<()> {
let app_state = states::AppState::new(config, mailer)?; let app_state = states::AppState::new(config, mailer)?;
db::run_migrations(&mut db::get_conn(&app_state.db.pool)?)?;
let api_routes = Router::new().route("/trigger", post(routes::trigger)); let api_routes = Router::new().route("/trigger", post(routes::trigger));
let routes = Router::new() let routes = Router::new()
.route("/", get(routes::index)) .route("/", get(routes::index))