From 1dca7190a6084cba85b5fdfe4e8ee8e492c83cb5 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 10:06:34 -0400 Subject: [PATCH 1/9] Adding code to use test database when in debug mode --- src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c3c562c..41e2c90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,13 @@ mod keys { pub mod error { pub const ERROR: &str = "DATABASE_URL must be set in .env"; } + + pub mod test { + pub const DBURL: &str = "TEST_DATABASE_URL"; + pub mod error { + pub const ERROR: &str = "TEST_DATABASE_URL must be set in .env"; + } + } } mod connection_settings { @@ -23,11 +30,20 @@ pub mod db_pool { pub async fn create_pool() -> Result { dotenv::dotenv().ok(); - let database_url = env::var(keys::DBURL).expect(keys::error::ERROR); + let database_url = get_db_url().await; + println!("Database url: {:?}", database_url); PgPoolOptions::new() .max_connections(connection_settings::MAXCONN) .connect(&database_url) .await } + + async fn get_db_url() -> String { + if cfg!(debug_assertions) { + env::var(keys::test::DBURL).expect(keys::test::error::ERROR) + } else { + env::var(keys::DBURL).expect(keys::error::ERROR) + } + } } -- 2.43.0 From 5e7d0cb5afe6b12b0b74b30f9221d4c2ab950494 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 11:41:55 -0400 Subject: [PATCH 2/9] Added service --- .gitea/workflows/workflow.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index bedf50d..0cec358 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -32,6 +32,22 @@ jobs: test: name: Test Suite runs-on: ubuntu-24.04 + # --- Add database service definition --- + services: + postgres: + image: postgres:17.4 # Or pin to a more specific version like 14.9 + env: + # Use secrets for DB init, with fallbacks for flexibility + POSTGRES_USER: ${{ secrets.DB_TEST_USER || 'testuser' }} + POSTGRES_PASSWORD: ${{ secrets.DB_TEST_PASSWORD || 'testpassword' }} + POSTGRES_DB: ${{ secrets.DB_TEST_NAME || 'testdb' }} + # Options to wait until the database is ready + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: - uses: actions/checkout@v4 - uses: actions-rust-lang/setup-rust-toolchain@v1 -- 2.43.0 From 7174962d0b8ece0f507d6ed48b567ae2350e00f0 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 11:47:46 -0400 Subject: [PATCH 3/9] Workflow change --- .gitea/workflows/workflow.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index 0cec358..5992b93 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -67,7 +67,8 @@ jobs: echo "Docker environment check complete." # NOTE: Do NOT use continue-on-error here. # If Docker isn't working as expected, the job SHOULD fail here. - - run: | + - name: Run tests + run: | mkdir -p ~/.ssh echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key chmod 600 ~/.ssh/gitlab_deploy_key -- 2.43.0 From 6d2c77f5720708f4085b69bbf1d6020ef4f330a1 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 11:51:13 -0400 Subject: [PATCH 4/9] Added workflow code for db --- .gitea/workflows/workflow.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index 5992b93..a79714f 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -67,7 +67,33 @@ jobs: echo "Docker environment check complete." # NOTE: Do NOT use continue-on-error here. # If Docker isn't working as expected, the job SHOULD fail here. + # --- Optional but Recommended: Database Migrations Step --- + - name: Run Database Migrations + env: + # Define DATABASE_URL using service details and secrets + DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} + # Make SSH agent available if migrations fetch private dependencies + SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }} + run: | + echo "Running database migrations..." + # ===> IMPORTANT: Replace placeholder below with your actual migration command <=== + # Example: Install and run sqlx-cli + # cargo install sqlx-cli --no-default-features --features native-tls,postgres + # sqlx database setup --database-url $DATABASE_URL + + # Example: Install and run diesel_cli + # cargo install diesel_cli --no-default-features --features postgres + # diesel migration run --database-url $DATABASE_URL + + # echo "[Placeholder] Your migration command goes here." + # ===> End of Placeholder <=== - name: Run tests + env: + # Define DATABASE_URL for tests to use + DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} + RUST_LOG: info # Optional: configure test log level + # Make SSH agent available if tests fetch private dependencies + SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }} run: | mkdir -p ~/.ssh echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key -- 2.43.0 From 71665c68d35159e8ad5987ee1e5fdf0afd5a1373 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 12:00:31 -0400 Subject: [PATCH 5/9] Working on changes to make db accessible in pipelines --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 41e2c90..eefcc6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,9 @@ pub mod db_pool { use crate::{connection_settings, keys}; pub async fn create_pool() -> Result { + #[cfg(debug_assertions)] // Example: Only load .env in debug builds dotenv::dotenv().ok(); + let database_url = get_db_url().await; println!("Database url: {:?}", database_url); -- 2.43.0 From 6a2184f11d16cda2108cf9ea97f521ff7abbcaf5 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 12:06:52 -0400 Subject: [PATCH 6/9] Switched dotenv to dotenvy --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 45a6f29..6567308 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ tracing-subscriber = { version = "0.3.19" } tower = { version = "0.5.2" } hyper = { version = "1.6.0" } sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio-native-tls"] } -dotenv = { version = "0.15" } +dotenvy = { version = "0.15.7" } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" } [dev-dependencies] -- 2.43.0 From 37ecc7fd43c7b1caaa4a89fdca140061cb637b7c Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 12:07:00 -0400 Subject: [PATCH 7/9] Saving changes --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index eefcc6f..1f203de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,7 +30,7 @@ pub mod db_pool { pub async fn create_pool() -> Result { #[cfg(debug_assertions)] // Example: Only load .env in debug builds - dotenv::dotenv().ok(); + dotenvy::dotenv().ok(); let database_url = get_db_url().await; println!("Database url: {:?}", database_url); -- 2.43.0 From e769d98d78b40f68b3d283becc7bcf1a600d8db4 Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 12:10:05 -0400 Subject: [PATCH 8/9] Saving changes --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1f203de..641a39d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,9 +29,6 @@ pub mod db_pool { use crate::{connection_settings, keys}; pub async fn create_pool() -> Result { - #[cfg(debug_assertions)] // Example: Only load .env in debug builds - dotenvy::dotenv().ok(); - let database_url = get_db_url().await; println!("Database url: {:?}", database_url); @@ -42,6 +39,9 @@ pub mod db_pool { } async fn get_db_url() -> String { + #[cfg(debug_assertions)] // Example: Only load .env in debug builds + dotenvy::dotenv().ok(); + if cfg!(debug_assertions) { env::var(keys::test::DBURL).expect(keys::test::error::ERROR) } else { -- 2.43.0 From 34d72e7936035fc48e92a8f11266d161e387cebb Mon Sep 17 00:00:00 2001 From: KD Date: Thu, 3 Apr 2025 12:12:21 -0400 Subject: [PATCH 9/9] Updated variable --- .gitea/workflows/workflow.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index a79714f..bf47367 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -70,8 +70,8 @@ jobs: # --- Optional but Recommended: Database Migrations Step --- - name: Run Database Migrations env: - # Define DATABASE_URL using service details and secrets - DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} + # Define TEST_DATABASE_URL using service details and secrets + TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} # Make SSH agent available if migrations fetch private dependencies SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }} run: | @@ -79,18 +79,18 @@ jobs: # ===> IMPORTANT: Replace placeholder below with your actual migration command <=== # Example: Install and run sqlx-cli # cargo install sqlx-cli --no-default-features --features native-tls,postgres - # sqlx database setup --database-url $DATABASE_URL + # sqlx database setup --database-url $TEST_DATABASE_URL # Example: Install and run diesel_cli # cargo install diesel_cli --no-default-features --features postgres - # diesel migration run --database-url $DATABASE_URL + # diesel migration run --database-url $TEST_DATABASE_URL # echo "[Placeholder] Your migration command goes here." # ===> End of Placeholder <=== - name: Run tests env: - # Define DATABASE_URL for tests to use - DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} + # Define TEST_DATABASE_URL for tests to use + TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} RUST_LOG: info # Optional: configure test log level # Make SSH agent available if tests fetch private dependencies SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }} -- 2.43.0