mirror of
https://codeberg.org/Mo8it/git-webhook-client
synced 2024-11-21 11:06:32 +00:00
Run pending migrations
This commit is contained in:
parent
2f83f1f207
commit
0aba8b543f
3 changed files with 16 additions and 2 deletions
|
@ -16,6 +16,7 @@ axum-extra = { version = "0.4", features = ["spa"] }
|
|||
bytes = "1.3"
|
||||
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_migrations = { version = "2.0.0", features = ["sqlite"] }
|
||||
hex = "0.4"
|
||||
hmac = "0.12"
|
||||
lettre = { version = "0.10", default-features = false, features = ["smtp-transport", "hostname", "rustls-tls", "pool", "builder"] }
|
||||
|
|
15
src/db.rs
15
src/db.rs
|
@ -1,9 +1,11 @@
|
|||
use anyhow::{Context, Result};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use chrono::offset::Local;
|
||||
use diesel::{
|
||||
prelude::*,
|
||||
r2d2::{ConnectionManager, Pool, PooledConnection},
|
||||
sqlite::Sqlite,
|
||||
};
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
use std::env;
|
||||
use tracing::error;
|
||||
|
||||
|
@ -13,10 +15,19 @@ use crate::{
|
|||
schema::hooklog,
|
||||
};
|
||||
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
||||
|
||||
type DBConnectionManager = ConnectionManager<SqliteConnection>;
|
||||
pub type DBPool = Pool<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> {
|
||||
let database_url_var = "DATABASE_URL";
|
||||
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!")
|
||||
}
|
||||
|
||||
fn get_conn(pool: &DBPool) -> Result<DBConnection> {
|
||||
pub fn get_conn(pool: &DBPool) -> Result<DBConnection> {
|
||||
pool.get().context("Could not get database pool!")
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,8 @@ async fn init() -> Result<()> {
|
|||
|
||||
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 routes = Router::new()
|
||||
.route("/", get(routes::index))
|
||||
|
|
Loading…
Reference in a new issue