diff --git a/src/db/init.rs b/src/db/init.rs new file mode 100644 index 0000000..06184dc --- /dev/null +++ b/src/db/init.rs @@ -0,0 +1,24 @@ + + + +use sqlx::postgres::PgPoolOptions; + +pub async fn create_pool() -> Result { + let database_url = textsender_models::envy::environment::get_db_url().await.value; + println!("Database url: {database_url}"); + + PgPoolOptions::new() + .max_connections(super::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/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..823d2d7 --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,5 @@ +pub mod init; + +mod connection_settings { + pub const MAXCONN: u32 = 5; +}