Skip to content

Commit

Permalink
feat: embed database migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Feb 15, 2024
1 parent 288691c commit 6baea0a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "covclaim"
version = "0.1.0"
edition = "2021"
build = "build.rs"

[profile.release]
strip = true
Expand All @@ -14,6 +15,7 @@ panic = "abort"
tokio = { version = "1.36.0", features = ["full"] }
axum = "0.7.4"
diesel = { version = "2.1.4", features = ["sqlite", "r2d2", "chrono"] }
diesel_migrations = "2.1.0"
dotenvy = "0.15.7"
env_logger = "0.11.2"
log = "0.4.20"
Expand All @@ -30,3 +32,6 @@ crossbeam-channel = "0.5.11"
r2d2 = "0.8.10"
rayon = "1.8.1"
num_cpus = "1.16.0"

[build-dependencies]
built = { version = "0.7.1", features = ["git2"] }
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
}
22 changes: 19 additions & 3 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use diesel::r2d2::ConnectionManager;
use diesel::SqliteConnection;
use diesel::sqlite::Sqlite;
use diesel::{Connection, SqliteConnection};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use std::error::Error;

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");

pub mod helpers;

Expand All @@ -9,7 +14,18 @@ mod schema;
pub type DatabaseConnection = SqliteConnection;
pub type Pool = r2d2::Pool<ConnectionManager<DatabaseConnection>>;

pub fn establish_connection(url: &str) -> Result<Pool, r2d2::Error> {
pub fn establish_connection(url: &str) -> Result<Pool, Box<dyn Error + Send + Sync>> {
run_migrations(&mut SqliteConnection::establish(url)?)?;

let manager = ConnectionManager::<DatabaseConnection>::new(url);
Pool::builder().build(manager)
let pool = Pool::builder().build(manager)?;

Ok(pool)
}

fn run_migrations(
connection: &mut impl MigrationHarness<Sqlite>,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
connection.run_pending_migrations(MIGRATIONS)?;
Ok(())
}
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ mod chain;
mod claimer;
mod db;

pub mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

#[tokio::main]
async fn main() {
dotenv().expect("could not read env file");
env_logger::init();

info!(
"Starting covclaim v{}-{}",
built_info::PKG_VERSION,
built_info::GIT_VERSION.unwrap_or("")
);
debug!(
"Compiled with {} for {}",
built_info::RUSTC_VERSION,
built_info::TARGET
);

let network_params = get_address_params();

let db = match db::establish_connection(
Expand Down

0 comments on commit 6baea0a

Please sign in to comment.