feat: include migrations in binary

Lets us change the schema and not worry about instance admins having to manually update their DB!
This commit is contained in:
Radical 2025-05-22 16:29:57 +02:00
parent 2e1382c1d4
commit c1885210fb
3 changed files with 21 additions and 5 deletions

View file

@ -30,8 +30,9 @@ random-string = "1.1"
actix-ws = "0.3.0"
futures-util = "0.3.31"
deadpool = "0.12"
diesel = "2.2"
diesel-async = { version = "0.5", features = ["deadpool", "postgres"] }
diesel = { version = "2.2", features = ["uuid"] }
diesel-async = { version = "0.5", features = ["deadpool", "postgres", "async-connection-wrapper"] }
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
[dependencies.tokio]
version = "1.44"

3
build.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("cargo:rerun-if-changed=migrations");
}

View file

@ -5,14 +5,16 @@ use clap::Parser;
use simple_logger::SimpleLogger;
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::RunQueryDsl;
use std::time::SystemTime;
mod config;
use config::{Config, ConfigBuilder};
mod api;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
type Conn = deadpool::managed::Object<AsyncDieselConnectionManager<diesel_async::AsyncPgConnection>>;
mod api;
pub mod structs;
pub mod utils;
pub mod schema;
@ -55,8 +57,18 @@ async fn main() -> Result<(), Error> {
let cache_pool = redis::Client::open(config.cache_database.url())?;
let mut conn = pool.get().await?;
let database_url = config.database.url();
tokio::task::spawn_blocking(move || {
use diesel::prelude::Connection;
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
let mut conn = AsyncConnectionWrapper::<diesel_async::AsyncPgConnection>::establish(&database_url)?;
conn.run_pending_migrations(MIGRATIONS);
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(())
}).await?;
/*
**Stored for later possible use**