From 8e138689cfdf40d80cdba1805be52e9271a2fd9f Mon Sep 17 00:00:00 2001 From: david Date: Fri, 1 May 2026 18:08:35 +0300 Subject: [PATCH] unique db action run --- .github/workflows/backend.yml | 37 +++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index 307810a..b2e24a6 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -15,14 +15,16 @@ on: jobs: backend-test: runs-on: [docker] - + env: + UNIQUE_DB: test_db_${{ github.run_id }} + services: postgres: image: postgres:15-alpine env: POSTGRES_USER: test POSTGRES_PASSWORD: test - POSTGRES_DB: test_db + POSTGRES_DB: test_db_${{ github.run_id }}_${{ github.run_attempt }} options: >- --health-cmd pg_isready --health-interval 10s @@ -39,13 +41,35 @@ jobs: - name: Set up Python run: | python --version - + - name: Install dependencies run: | cd backend python -m pip install --upgrade pip pip install --cache-dir /tmp/pip-cache -r requirements/dev.txt - + + - name: Create Unique Test Database + env: + DATABASE_URL: postgresql://test:test@postgres:5432/postgres + run: | + # Install postgresql-client if not present in the alpine image to run psql + # Or use python to create the db + cd backend + python -c " + import os + from sqlalchemy import create_engine, text + db_url = os.environ['DATABASE_URL'] + engine = create_engine(db_url) + new_db = os.environ['UNIQUE_DB'] + # Connect to default 'postgres' db to create the new one + with engine.connect() as conn: + conn.execute(text('COMMIT')) # Close any open transactions + conn.execute(text(f'DROP DATABASE IF EXISTS {new_db}')) + conn.execute(text(f'CREATE DATABASE {new_db}')) + print(f'Created database: {new_db}') + " + + - name: Debug cache run: | echo "Listing PIP cache files:" @@ -59,8 +83,9 @@ jobs: - name: Run tests env: - TEST_DATABASE_URL: postgresql://test:test@postgres:5432/test_db - DATABASE_URL: postgresql://test:test@postgres:5432/test_db + UNIQUE_DB: test_db_${{ github.run_id }} + TEST_DATABASE_URL: postgresql://test:test@postgres:5432/${{ env.UNIQUE_DB }} + DATABASE_URL: postgresql://test:test@postgres:5432/${{ env.UNIQUE_DB }} SECRET_KEY: test-secret-key JWT_SECRET_KEY: test-jwt-secret FLASK_ENV: test