diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..41762ef --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,24 @@ +use sqlx::postgres::PgPoolOptions; + +pub mod connection_settings { + pub const MAXCONN: u32 = 10; +} + +pub async fn create_pool() -> Result { + let database_url = icarus_envy::environment::get_db_url().await.value; + println!("Database url: {database_url}"); + + PgPoolOptions::new() + .max_connections(connection_settings::MAXCONN) + .connect(&database_url) + .await +} + +pub async fn migrations(pool: &sqlx::PgPool) { + // Run migrations using the sqlx::migrate! macro + // Assumes your migrations are in a ./migrations folder relative to Cargo.toml + sqlx::migrate!("./migrations") + .run(pool) + .await + .expect("Failed to run migrations"); +} diff --git a/src/main.rs b/src/main.rs index 6048632..d313d3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,9 @@ pub mod auth; +pub mod db; pub mod callers; pub mod config; pub mod repo; -pub mod db { - - use sqlx::postgres::PgPoolOptions; - - pub mod connection_settings { - pub const MAXCONN: u32 = 10; - } - - pub async fn create_pool() -> Result { - let database_url = icarus_envy::environment::get_db_url().await.value; - println!("Database url: {database_url}"); - - PgPoolOptions::new() - .max_connections(connection_settings::MAXCONN) - .connect(&database_url) - .await - } - - pub async fn migrations(pool: &sqlx::PgPool) { - // Run migrations using the sqlx::migrate! macro - // Assumes your migrations are in a ./migrations folder relative to Cargo.toml - sqlx::migrate!("./migrations") - .run(pool) - .await - .expect("Failed to run migrations"); - } -} #[tokio::main] async fn main() {