Database migration are now automatically applied
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2024-05-14 21:36:49 +02:00
parent d9a4e3249d
commit f167e24c4f
5 changed files with 100 additions and 4 deletions

View File

@ -1054,6 +1054,17 @@ dependencies = [
"syn 2.0.63", "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]] [[package]]
name = "diesel_table_macro_syntax" name = "diesel_table_macro_syntax"
version = "0.1.0" version = "0.1.0"
@ -1400,6 +1411,7 @@ dependencies = [
"bcrypt", "bcrypt",
"clap", "clap",
"diesel", "diesel",
"diesel_migrations",
"env_logger", "env_logger",
"futures-util", "futures-util",
"httpdate", "httpdate",
@ -2110,6 +2122,27 @@ version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" 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]] [[package]]
name = "mime" name = "mime"
version = "0.3.17" version = "0.3.17"
@ -3311,7 +3344,7 @@ dependencies = [
"cfg-expr", "cfg-expr",
"heck", "heck",
"pkg-config", "pkg-config",
"toml", "toml 0.8.12",
"version-compare", "version-compare",
] ]
@ -3472,6 +3505,18 @@ dependencies = [
"tokio", "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]] [[package]]
name = "toml" name = "toml"
version = "0.8.12" version = "0.8.12"
@ -3481,7 +3526,7 @@ dependencies = [
"serde", "serde",
"serde_spanned", "serde_spanned",
"toml_datetime", "toml_datetime",
"toml_edit", "toml_edit 0.22.12",
] ]
[[package]] [[package]]
@ -3493,6 +3538,19 @@ dependencies = [
"serde", "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]] [[package]]
name = "toml_edit" name = "toml_edit"
version = "0.22.12" version = "0.22.12"
@ -3503,7 +3561,7 @@ dependencies = [
"serde", "serde",
"serde_spanned", "serde_spanned",
"toml_datetime", "toml_datetime",
"winnow", "winnow 0.6.8",
] ]
[[package]] [[package]]
@ -3959,6 +4017,15 @@ version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
[[package]]
name = "winnow"
version = "0.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.6.8" version = "0.6.8"

View File

@ -17,6 +17,7 @@ actix-multipart = "0.6.1"
actix-remote-ip = "0.1.0" actix-remote-ip = "0.1.0"
futures-util = "0.3.30" futures-util = "0.3.30"
diesel = { version = "2.1.6", features = ["postgres"] } diesel = { version = "2.1.6", features = ["postgres"] }
diesel_migrations = "2.1.0"
serde = { version = "1.0.198", features = ["derive"] } serde = { version = "1.0.198", features = ["derive"] }
serde_json = "1.0.117" serde_json = "1.0.117"
mailchecker = "6.0.4" mailchecker = "6.0.4"

3
geneit_backend/build.rs Normal file
View File

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

View File

@ -3,7 +3,11 @@
use crate::app_config::AppConfig; use crate::app_config::AppConfig;
use diesel::result::{DatabaseErrorKind, Error}; use diesel::result::{DatabaseErrorKind, Error};
use diesel::{Connection, PgConnection}; use diesel::{Connection, PgConnection};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use std::cell::RefCell; use std::cell::RefCell;
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
thread_local! { thread_local! {
static POSTGRES_CONNECTION: RefCell<Option<PgConnection>> = const { RefCell::new(None) }; static POSTGRES_CONNECTION: RefCell<Option<PgConnection>> = 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(())
}

View File

@ -4,7 +4,7 @@ use actix_remote_ip::RemoteIPConfig;
use actix_web::middleware::Logger; use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use geneit_backend::app_config::AppConfig; use geneit_backend::app_config::AppConfig;
use geneit_backend::connections::s3_connection; use geneit_backend::connections::{db_connection, s3_connection};
use geneit_backend::controllers::{ use geneit_backend::controllers::{
auth_controller, couples_controller, data_controller, families_controller, members_controller, auth_controller, couples_controller, data_controller, families_controller, members_controller,
photos_controller, server_controller, users_controller, photos_controller, server_controller, users_controller,
@ -22,6 +22,10 @@ async fn main() -> std::io::Result<()> {
.await .await
.expect("Failed to initialize S3 bucket!"); .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(|| { HttpServer::new(|| {
App::new() App::new()
.wrap( .wrap(