Skip to content

Commit

Permalink
Provide migrations as lib
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Mar 22, 2024
1 parent 9b6a715 commit 44f8fa9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
24 changes: 24 additions & 0 deletions v-api-installer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn Migration<Pg>>> = MIGRATIONS.migrations().unwrap();

for migration in migrations {
migration.run(&mut conn).unwrap();
}
}

fn db_conn(url: &str) -> PgConnection {
let conn: ConnectionManager<PgConnection> = ConnectionManager::new(url);
conn.connect().unwrap()
}
25 changes: 3 additions & 22 deletions v-api-installer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Migration<Pg>>> = 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<PgConnection> = ConnectionManager::new(url);
conn.connect().unwrap()
}
}

0 comments on commit 44f8fa9

Please sign in to comment.