π OpenHands: AI-Driven Development
PLTF-2895: Enterprise Apply Migrations on Test DB (#14652)
aivong-openhands568beb1196ac
diff --git a/.github/workflows/enterprise-apply-migrations-test-db.yml b/.github/workflows/enterprise-apply-migrations-test-db.ymlnew file mode 100644index 0000000000..a19fca65bd--- /dev/null+++ b/.github/workflows/enterprise-apply-migrations-test-db.yml@@ -0,0 +1,98 @@+name: Enterprise Apply Migrations on Test DB++# Applies the full enterprise migration chain against a throwaway test Postgres, using the+# SAME driver as production (pg8000) and the real migrations/env.py harness+# (advisory lock + transaction wrapping). This catches migration bugs before+# merge/deploy instead of in the staging migrate-db init container β e.g. a+# CREATE INDEX CONCURRENTLY in an autocommit_block, which crashes under env.py.++on:+ pull_request:+ paths:+ - 'enterprise/migrations/**'+ - 'enterprise/alembic.ini'+ - '.github/workflows/enterprise-apply-migrations-test-db.yml'++# Group is per-PR-branch, so this only cancels a superseded run of the SAME PR+# (e.g. after a new push) β never another PR. cancel-in-progress is safe here+# because each run uses its own throwaway Postgres service that is destroyed with+# the runner: cancelling mid-migration leaves no shared/persistent state behind.+concurrency:+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}+ cancel-in-progress: true++jobs:+ apply-migrations:+ name: Apply migrations on test DB+ runs-on: ubuntu-24.04+ strategy:+ matrix:+ # Test both the production driver (pg8000) and the psycopg2 fallback.+ # The CLOUD_SQL_POSTGRES_VERSION env var holds the canonical Cloud SQL+ # major version β update this job's image tag whenever that variable+ # changes in the deploy repo. If CLOUD_SQL_POSTGRES_VERSION is unset,+ # postgres:16 is used as a reasonable fallback (check deploy repo for+ # the current cloudsql-postgres image tag and keep them in sync).+ db-driver: [pg8000, ""] # [production driver, psycopg2 fallback]+ # Safety timeout: large schemas with many indexes/tables can take 15+ minutes+ # to migrate. Adjust if schema growth outpaces this estimate.+ timeout-minutes: 30+ services:+ postgres:+ image: postgres:16+ env:+ POSTGRES_USER: openhands+ POSTGRES_PASSWORD: openhands+ POSTGRES_DB: openhands+ ports:+ - 5432:5432+ options: >-+ --health-cmd "pg_isready -U openhands"+ --health-interval 5s+ --health-timeout 5s+ --health-retries 10+ env:+ DB_HOST: localhost+ DB_PORT: "5432"+ DB_USER: openhands+ DB_PASS: openhands+ DB_NAME: openhands+ DB_DRIVER: ${{ matrix.db-driver }} # pg8000 for prod parity, "" for psycopg2+ steps:+ - uses: actions/checkout@v6+ - name: Install poetry via pipx+ run: pipx install poetry+ - name: Set up Python+ uses: actions/setup-python@v6+ with:+ python-version: "3.12"+ cache: "poetry"+ - name: Install Python dependencies using Poetry+ working-directory: ./enterprise+ run: poetry install --with dev,test+ - name: Show migration history+ working-directory: ./enterprise+ run: poetry run alembic history --verbose+ - name: Apply full migration chain (upgrade head)+ working-directory: ./enterprise+ run: |+ echo "::group::alembic upgrade head (from empty DB)"+ START=$(date +%s)+ poetry run alembic upgrade head+ echo "upgrade head took $(( $(date +%s) - START ))s"+ echo "::endgroup::"+ poetry run alembic current+ - name: Round-trip the newest migration (downgrade -1, upgrade head)+ working-directory: ./enterprise+ # Fail fast if downgrade fails. The database is throwaway, and the+ # final reporting step still prints the current revision for debugging.+ run: |+ echo "::group::downgrade -1 then re-upgrade"+ poetry run alembic downgrade -1+ poetry run alembic upgrade head+ echo "::endgroup::"+ poetry run alembic current+ - name: Report migration state+ if: always()+ working-directory: ./enterprise+ run: poetry run alembic current || truediff --git a/enterprise/enterprise_local/README.md b/enterprise/enterprise_local/README.mdindex a7e201566f..758093f7d7 100644--- a/enterprise/enterprise_local/README.md+++ b/enterprise/enterprise_local/README.md@@ -160,7 +160,11 @@ A Local redis instance is required for clustered communication between server noA Local postgres instance is required. I used the official docker image:`docker run -p 5432:5432 --name my-postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=openhands -d postgres`Run the alembic migrations:-`poetry run alembic upgrade head `+`poetry run alembic upgrade head`++> **Note:** By default, migrations use the `pg8000` driver (matching production,+> which connects through the Cloud SQL connector on pg8000). To use psycopg2 instead,+> set `DB_DRIVER=''` before running alembic commands.#### VSCode launch.jsondiff --git a/enterprise/migrations/env.py b/enterprise/migrations/env.pyindex bd0f2a6bfe..9ffd52fe29 100644--- a/enterprise/migrations/env.py+++ b/enterprise/migrations/env.py@@ -24,6 +24,11 @@ DB_PASS = os.getenv('DB_PASS', 'postgres')DB_HOST = os.getenv('DB_HOST', 'localhost')DB_PORT = os.getenv('DB_PORT', '5432')DB_NAME = os.getenv('DB_NAME', 'openhands')+# Driver for the non-Cloud-SQL (DB_HOST) path used by feature/local/CI. Production+# connects through the Cloud SQL connector on pg8000, so we default to pg8000 here+# too: this keeps every environment on the same driver and lets pg8000-specific+# migration failures surface before deploy. Set DB_DRIVER='' to use psycopg2.+DB_DRIVER = os.getenv('DB_DRIVER', 'pg8000')GCP_DB_INSTANCE = os.getenv('GCP_DB_INSTANCE')GCP_PROJECT = os.getenv('GCP_PROJECT')@@ -56,7 +61,8 @@ def get_engine(database_name=DB_NAME):pool_pre_ping=True,)else:- url = f'postgresql://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{database_name}'+ scheme = f'postgresql+{DB_DRIVER}' if DB_DRIVER else 'postgresql'+ url = f'{scheme}://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{database_name}'return create_engine(url,pool_size=POOL_SIZE,