From bd8b27a9ad7316b144f42c6096a6abe089299983 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sun, 4 Feb 2024 23:33:46 -0500 Subject: [PATCH] Add database migration function --- src/database.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/database.rs b/src/database.rs index a90de44..22b9652 100644 --- a/src/database.rs +++ b/src/database.rs @@ -12,6 +12,12 @@ use diesel::{ r2d2::Pool, }; +use diesel_migrations::{ + embed_migrations, + EmbeddedMigrations, + MigrationHarness, +}; + // See https://leward.eu/notes-on-diesel-a-rust-orm/ // Define some types to make it easier to work with Diesel @@ -47,5 +53,15 @@ pub fn get_db_conn() -> PgPooledConn { DB_POOL.get().expect("Failed to get a database connection from the pool.") } +/// Embedded database migrations into the binary +const DB_MIGRATIONS: EmbeddedMigrations = embed_migrations!(); + +/// Run any pending migrations in the database +/// Always safe to call, as it will only run migrations that have not already been run +pub fn migrate() { + let db_con = &mut get_db_conn(); + db_con.run_pending_migrations(DB_MIGRATIONS).expect("Could not run database migrations"); +} + } }