diff --git a/Cargo.toml b/Cargo.toml index 32d26f1..d1da36b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "covclaim" version = "0.1.0" edition = "2021" +build = "build.rs" [profile.release] strip = true @@ -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" @@ -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"] } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d8f91cb --- /dev/null +++ b/build.rs @@ -0,0 +1,3 @@ +fn main() { + built::write_built_file().expect("Failed to acquire build-time information"); +} diff --git a/src/db/mod.rs b/src/db/mod.rs index 0260570..77e07fd 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -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; @@ -9,7 +14,18 @@ mod schema; pub type DatabaseConnection = SqliteConnection; pub type Pool = r2d2::Pool>; -pub fn establish_connection(url: &str) -> Result { +pub fn establish_connection(url: &str) -> Result> { + run_migrations(&mut SqliteConnection::establish(url)?)?; + let manager = ConnectionManager::::new(url); - Pool::builder().build(manager) + let pool = Pool::builder().build(manager)?; + + Ok(pool) +} + +fn run_migrations( + connection: &mut impl MigrationHarness, +) -> Result<(), Box> { + connection.run_pending_migrations(MIGRATIONS)?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 1328dfa..1c14b48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(