From c1885210fbb39b272fb1f25da215c1b0ae4c4536 Mon Sep 17 00:00:00 2001 From: Radical Date: Thu, 22 May 2025 16:29:57 +0200 Subject: [PATCH] feat: include migrations in binary Lets us change the schema and not worry about instance admins having to manually update their DB! --- Cargo.toml | 5 +++-- build.rs | 3 +++ src/main.rs | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index e6dcd84..9a91b30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..284ad12 --- /dev/null +++ b/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rerun-if-changed=migrations"); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0a9d493..10da4f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>; +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::::establish(&database_url)?; + + conn.run_pending_migrations(MIGRATIONS); + Ok::<_, Box>(()) + }).await?; /* **Stored for later possible use**