From 2bb48cb00ceb88a2af41202d8b3545c9f37be495 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 28 Jan 2025 16:42:36 +0100 Subject: [PATCH 1/2] Upgrade PostgreSQL to 15 --- .github/workflows/ci.yaml | 6 +++--- docker-compose.yaml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6b191eb6..6dad4957 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,7 +17,7 @@ jobs: runs-on: ubuntu-latest services: postgres: - image: postgres:12 + image: postgres:15 env: POSTGRES_PASSWORD: postgres POSTGRES_HOST_AUTH_METHOD: trust @@ -50,7 +50,7 @@ jobs: env: CTMS_DB_URL: postgresql://postgres:postgres@172.17.0.1:5432/postgres # pragma: allowlist secret CTMS_SECRET_KEY: secret_${{ github.sha }} # pragma: allowlist secret - + build_production_image: runs-on: ubuntu-latest steps: @@ -58,7 +58,7 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Build production image + - name: Build production image uses: docker/build-push-action@v6 with: context: . diff --git a/docker-compose.yaml b/docker-compose.yaml index 7cb3b044..10233aef 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -22,7 +22,7 @@ services: timeout: 1s retries: 10 postgres: - image: postgres:12 + image: postgres:15 ports: - 5432:5432 environment: From c8f897f62e8eb92f90e68b20a7fbdffa637b7e6e Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Wed, 29 Jan 2025 16:43:55 +0700 Subject: [PATCH 2/2] Update `count_total_contacts` to have a fallback This was added for cases where the table has not yet been analyzed and the estimate is not available... mainly in unit tests. --- ctms/crud.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ctms/crud.py b/ctms/crud.py index 0896f4f5..9a5382ee 100644 --- a/ctms/crud.py +++ b/ctms/crud.py @@ -59,12 +59,18 @@ def count_total_contacts(db: Session) -> int: This metadata is refreshed on `VACUUM` or `ANALYSIS` which is run regularly by default on our database instances. """ - query = text( - "SELECT reltuples AS estimate " - "FROM pg_class " - f"where relname = '{Email.__tablename__}'" - ) - result = db.execute(query).scalar() + result = db.execute( + text( + "SELECT reltuples AS estimate " + "FROM pg_class " + f"where relname = '{Email.__tablename__}'" + ) + ).scalar() + if result is None or result < 0: + # Fall back to a full count if the estimate is not available. + result = db.execute( + text(f"SELECT COUNT(*) FROM {Email.__tablename__}") + ).scalar() if result is None: return -1 return int(result)