diff --git a/v-api-installer/src/lib.rs b/v-api-installer/src/lib.rs new file mode 100644 index 0000000..5609466 --- /dev/null +++ b/v-api-installer/src/lib.rs @@ -0,0 +1,24 @@ +use diesel::{ + migration::{Migration, MigrationSource}, + pg::Pg, + r2d2::{ConnectionManager, ManageConnection}, + PgConnection, +}; +use diesel_migrations::{embed_migrations, EmbeddedMigrations}; + +const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../v-model/migrations"); + +pub fn run_migrations(url: &str) { + let mut conn = db_conn(&url); + + let migrations: Vec>> = MIGRATIONS.migrations().unwrap(); + + for migration in migrations { + migration.run(&mut conn).unwrap(); + } +} + +fn db_conn(url: &str) -> PgConnection { + let conn: ConnectionManager = ConnectionManager::new(url); + conn.connect().unwrap() +} diff --git a/v-api-installer/src/main.rs b/v-api-installer/src/main.rs index 8d5e328..dd80f2f 100644 --- a/v-api-installer/src/main.rs +++ b/v-api-installer/src/main.rs @@ -2,31 +2,12 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use diesel::{ - migration::{Migration, MigrationSource}, - pg::Pg, - r2d2::{ConnectionManager, ManageConnection}, - PgConnection, -}; -use diesel_migrations::{embed_migrations, EmbeddedMigrations}; - -const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../v-model/migrations"); +use v_api_installer::run_migrations; fn main() { if let Ok(url) = std::env::var("DATABASE_URL") { - let mut conn = db_conn(&url); - - let migrations: Vec>> = MIGRATIONS.migrations().unwrap(); - - for migration in migrations { - migration.run(&mut conn).unwrap(); - } + run_migrations(&url); } else { println!("DATABASE_URL environment variable must be specified to run migrations and must be in the form of a connection string") } -} - -fn db_conn(url: &str) -> PgConnection { - let conn: ConnectionManager = ConnectionManager::new(url); - conn.connect().unwrap() -} +} \ No newline at end of file