From f167e24c4f4c65b10fdc88db6d454295fde112e1 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 14 May 2024 21:36:49 +0200 Subject: [PATCH] Database migration are now automatically applied --- geneit_backend/Cargo.lock | 73 ++++++++++++++++++- geneit_backend/Cargo.toml | 1 + geneit_backend/build.rs | 3 + .../src/connections/db_connection.rs | 21 ++++++ geneit_backend/src/main.rs | 6 +- 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 geneit_backend/build.rs diff --git a/geneit_backend/Cargo.lock b/geneit_backend/Cargo.lock index bcd07a9..63c901a 100644 --- a/geneit_backend/Cargo.lock +++ b/geneit_backend/Cargo.lock @@ -1054,6 +1054,17 @@ dependencies = [ "syn 2.0.63", ] +[[package]] +name = "diesel_migrations" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6036b3f0120c5961381b570ee20a02432d7e2d27ea60de9578799cf9156914ac" +dependencies = [ + "diesel", + "migrations_internals", + "migrations_macros", +] + [[package]] name = "diesel_table_macro_syntax" version = "0.1.0" @@ -1400,6 +1411,7 @@ dependencies = [ "bcrypt", "clap", "diesel", + "diesel_migrations", "env_logger", "futures-util", "httpdate", @@ -2110,6 +2122,27 @@ version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +[[package]] +name = "migrations_internals" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f23f71580015254b020e856feac3df5878c2c7a8812297edd6c0a485ac9dada" +dependencies = [ + "serde", + "toml 0.7.8", +] + +[[package]] +name = "migrations_macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cce3325ac70e67bbab5bd837a31cae01f1a6db64e0e744a33cb03a543469ef08" +dependencies = [ + "migrations_internals", + "proc-macro2", + "quote", +] + [[package]] name = "mime" version = "0.3.17" @@ -3311,7 +3344,7 @@ dependencies = [ "cfg-expr", "heck", "pkg-config", - "toml", + "toml 0.8.12", "version-compare", ] @@ -3472,6 +3505,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.19.15", +] + [[package]] name = "toml" version = "0.8.12" @@ -3481,7 +3526,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit", + "toml_edit 0.22.12", ] [[package]] @@ -3493,6 +3538,19 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap 2.2.6", + "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.5.40", +] + [[package]] name = "toml_edit" version = "0.22.12" @@ -3503,7 +3561,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "winnow", + "winnow 0.6.8", ] [[package]] @@ -3959,6 +4017,15 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + [[package]] name = "winnow" version = "0.6.8" diff --git a/geneit_backend/Cargo.toml b/geneit_backend/Cargo.toml index 16a3514..c133f20 100644 --- a/geneit_backend/Cargo.toml +++ b/geneit_backend/Cargo.toml @@ -17,6 +17,7 @@ actix-multipart = "0.6.1" actix-remote-ip = "0.1.0" futures-util = "0.3.30" diesel = { version = "2.1.6", features = ["postgres"] } +diesel_migrations = "2.1.0" serde = { version = "1.0.198", features = ["derive"] } serde_json = "1.0.117" mailchecker = "6.0.4" diff --git a/geneit_backend/build.rs b/geneit_backend/build.rs new file mode 100644 index 0000000..3a8149e --- /dev/null +++ b/geneit_backend/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rerun-if-changed=migrations"); +} diff --git a/geneit_backend/src/connections/db_connection.rs b/geneit_backend/src/connections/db_connection.rs index 0fb543f..c3e3e82 100644 --- a/geneit_backend/src/connections/db_connection.rs +++ b/geneit_backend/src/connections/db_connection.rs @@ -3,7 +3,11 @@ use crate::app_config::AppConfig; use diesel::result::{DatabaseErrorKind, Error}; use diesel::{Connection, PgConnection}; +use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use std::cell::RefCell; + +const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); + thread_local! { static POSTGRES_CONNECTION: RefCell> = const { RefCell::new(None) }; } @@ -39,3 +43,20 @@ where } } } + +/// Initialize database connection +pub fn initialize_conn() -> anyhow::Result<()> { + // Run pending diesel migrations + execute(|db| { + let res = db + .run_pending_migrations(MIGRATIONS) + .expect("Failed to run database migration!"); + + for migration in res { + log::info!("Executed database migration {migration}") + } + + Ok(()) + })?; + Ok(()) +} diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index 3825999..711766b 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -4,7 +4,7 @@ use actix_remote_ip::RemoteIPConfig; use actix_web::middleware::Logger; use actix_web::{web, App, HttpServer}; use geneit_backend::app_config::AppConfig; -use geneit_backend::connections::s3_connection; +use geneit_backend::connections::{db_connection, s3_connection}; use geneit_backend::controllers::{ auth_controller, couples_controller, data_controller, families_controller, members_controller, photos_controller, server_controller, users_controller, @@ -22,6 +22,10 @@ async fn main() -> std::io::Result<()> { .await .expect("Failed to initialize S3 bucket!"); + // Initialize database connection + log::debug!("Initialize database connection"); + db_connection::initialize_conn().expect("Failed to initialize database connection!"); + HttpServer::new(|| { App::new() .wrap(