Repository navigation
Upgrade DB images to PostgreSQL 18 / PostGIS 3.7 + upgrade runbook - #2964
atalyaalon wants to merge 26 commits into
Conversation
DB dumps moved from daily to monthly (data-for-change/dfc-iac#24), so a dump dated today usually won't exist. When DBRESTORE_FILE_NAME is not set, list the bucket and restore the newest date that has both the partial and the partial_schema dump, each at least 1 KB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
36a2cbc to
97fd68e
Compare
- '|| true' on the listing pipeline: a failed 'aws s3 ls' now reaches the 'no complete partial dump found' message instead of stopping the sourced script silently under set -e / pipefail - 'NF == 4' in awk: a key like '..._anyway_partial.pgdump.gz copy' no longer counts as a second file for that date Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
8ad202b to
098e3ca
Compare
|
@ziv17, answers to your two questions: 1. Export and import instead of an in-place upgrade?Yes. That's exactly what this plan does: Most of the runbook's length (
The bare-minimum version is:
That works too. You just get no timing rehearsal and no side-by-side check before cutting over. 2. Running the tests locally after the PRs merge
docker compose -f docker-compose-tests.yml up -d --build
until docker exec db-tests psql -U anyway -d anyway -c "select 1" >/dev/null 2>&1; do sleep 5; done
docker exec anyway-tests alembic upgrade head
docker exec anyway-tests ./main.py process cities
docker exec anyway-tests ./main.py process streets
docker exec anyway-tests ./main.py process registered-vehicles
docker exec anyway-tests ./main.py process cbs --source local_dir_for_tests_only
docker exec anyway-tests ./main.py process road-segments
docker exec anyway-tests ./main.py process junctions
docker exec anyway-tests pip install -r ./test_requirements.txt
docker exec anyway-tests pytest -v tests -m "not browser"Notes:
If something in Generated by Claude Code |
A valid schema dump of a few tables gzips to well under 1 KB (a realistic 6-table one is about 1.6 KB), so the 1 KB floor could reject a good dump and fall back to an older date. 200 bytes still rejects an empty or cut-off upload: a gzipped empty file is about 20 bytes. A header-only dump (about 400 bytes, no tables) also passes; it restores cleanly. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
- db: postgis/postgis:18-3.7 (trixie), backup: postgres:18-trixie, both pinned by digest - install-aws-cli.sh: drop bullseye archive workaround (not needed on trixie) - restore_db.sh: DBRESTORE_SKIP switch, skip (not abort) on missing AWS creds, collect psql errors and print a summary (based on #2961) - compose: mount volumes at /var/lib/postgresql (PG18 layout), rename volumes - runbook: branch info, postgis_tiger_geocoder removal in PostGIS 3.7, filtered pg_restore Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
PG18 stores NOT NULL constraints in pg_constraint (contype 'n'), so the 'drop all non-PK constraints' loop tried to drop users_id_not_null and failed with 'column "id" is in a primary key'. Exclude 'n' too, which matches the pre-18 behavior exactly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
…sh roles Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
…docs - row-count check skips tiger tables (not restored on purpose) - DROP DATABASE ... WITH (FORCE) in the migration window - rollback: note that reverting also restarts the temporary db18 service - document that a failed auto-restore leaves an empty DB after restart - dev docs: rebuild the db image and drop the old PG15 volume Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
- dump from the old server with DBDUMP_PASSWORD (POSTGRES_PASSWORD only applied at initdb) - re-hash MD5 roles on the 15 server before any dump, so globals.sql carries SCRAM - rehearsal: avoid/pause the weekly cbs DAG (TRUNCATE vs pg_dump locks) - error checks: grep -i error + pg_restore exit code - optional search_path cleanup (tiger schema gone), sudo rm for the cron file Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Backups may become monthly (dfc-iac #24), so after switching the backup image (step 3) and after the cutover (step 8) run the backup job by hand right away. The newest-complete-dump restore itself comes from #2965. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
- restore_db.sh: use #2965's newest *complete* partial dump selection (both files >= 1 KB) instead of the newest partial alone; add '|| true' so a failed 'aws s3 ls' prints the warning instead of silently aborting the entrypoint; if no complete dump is found, warn and start empty (return 0) - runbook: merge order of all related PRs; cbs DAG warning for manual backups; remove the pg18test container; pause/resume the uptime monitor (dfc-iac #25) in the window; don't change dfc2's instance type (#26) between rehearsal and window; pre-pg18-final dump expires after 365 days (#24) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
- restore_db.sh: skip S3 keys with spaces (NF == 4) in the newest-dump selection; keep errexit from stopping on a failed last step so the psql error summary still prints and the failure is reported - runbook step 3: test-restore via the image's restore_db.sh with the S3 reader creds from secrets/db.env (quotes stripped for docker --env-file) - runbook step 7: no :latest fallback for PIN_DB_IMAGE (fail if unset) - merge order: rebase note also covers rebase-merge, plus force-push Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Adds the DB init steps CI runs (cities, streets, road-segments, junctions), a wait loop, the new db_tests_data_pg18 volume, the Overpass 429 retry tip, and that Twitter/Maps tests skip without keys. Checked against the repo and Tests.yml, not run end to end locally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
…S.md waits - restore_db.sh: take #2965's 200-byte minimum dump size. Run the password step with ON_ERROR_STOP and collect its stderr, so an SQL error there (no anyway role, a quote in the password) is counted in the summary and reported as failed. Unset the exported AWS keys and the helper function after a successful restore, because the entrypoint sources this file. - runbook step 3: the test restore keeps its data dir and /tmp on /data (bind mounts, chmod 1777 for /tmp, --shm-size=1g) and cleans them up with docker rm -fv plus literal paths. - runbook: rebase note uses #2965's new head e4d92d0 and git fetch first; wording fixes (what #2964 changes on top of #2965, the app image note applies to master only). - docs/TESTS.md: wait for the DB over TCP, so the init-time temporary server doesn't count; wait for the app container before the init steps, because its entrypoint already runs alembic upgrade head. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
6118b50 to
1bd5141
Compare
|
@tkalir @ziv17, a question before we plan the PostgreSQL 18 migration: which hostnames, and which server, do we want to keep? The answer decides which database we migrate. It also affects data-for-change/dfc-iac#28, which shuts down the old dfc server, and #29, which shrinks dfc2's disk. Today, from dfc-iac
|
| Hostname | Served by |
|---|---|
anyway.co.il (apex), plus www.anyway.co.il → redirect to the apex (since dfc-iac b144ce7, 6 Nov) |
dfc2 |
safety-data.anyway.co.il |
dfc2 |
airflow.anyway.co.il, airflow-data.anyway.co.il, reports.anyway.co.il, ws.anyway.co.il |
old dfc |
www.dfc2., airflow.dfc2., airflow-data.dfc2., reports.dfc2., safety-data.dfc2.anyway.co.il |
dfc2 |
Each server runs its own Airflow, ETL and DB. The public site reads dfc2's DB, while airflow.anyway.co.il still points at the old dfc server's Airflow, which writes to its own DB. dfc-iac#28 adds airflow, airflow-data and reports.anyway.co.il as extra names on dfc2, ahead of moving DNS.
Questions
- Long-term names: do we keep the original names (
anyway.co.il,airflow.anyway.co.il,reports.anyway.co.il…) and drop the*.dfc2.anyway.co.ilones? The other way round? Or both, as aliases? And should the canonical site be the apexanyway.co.ilorwww.anyway.co.il? - Which DB is authoritative now: is dfc2's DB the real production data? Does anything still use the old dfc server's DB or its Airflow: Redash, ETL alerts, people working in
airflow.anyway.co.il? - Old dfc server's DB: if it's no longer needed, the PG 18 migration would be done only on dfc2, and the old dfc server would simply be shut down by dfc-iac#28 without migrating its DB. Does that sound right?
What I'd suggest, if you agree
- Treat dfc2 as production for the PG 18 runbook. The parallel
db18rehearsal on dfc2 (runbook step 5) is the real rehearsal. - Do the PG 18 migration before dfc-iac#29 shrinks
/data. The migration needs room for the old and new data directories plus a dump at the same time. - Don't start any DB step until the 1 Dec monthly backup exists and has been checked.
I'll update the runbook once we agree.
Generated by Claude Code
- dfc2 serves anyway.co.il since 6 Nov (checked with dig/curl on 2026-11-30); nobody relies on the old dfc server's Airflow/Reports/DB, which dfc-iac #28 shuts down, so the migration is done on dfc2 only - wait for and check the first monthly backup (1 Dec) before any DB step - merge order: dfc-iac #27, #24, #30, #33, #34 merged; add #28 and #29 (after PG 18); #2966 independent; Terraform 1.17.2; --no-deploy on #2965 only works with a squash merge Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
…measure free space before the migration dfc-iac#29 is replaced by #39 (the unused data disk was removed). The migration needs about data dir + compressed dump + margin (~35 GB), which most likely fits after dfc-iac#37's cleanup; grow the root disk only if short. The cutover snapshot is of the root volume. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
… backup image Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau
|
Regarding running the tests locally. Can I just checkout the PR's branch and try running the tests? should it work? |
|
…ment a clean restart bin/init_test_db.sh CONTAINER runs the migrations and the 6 data loads. CI (Tests.yml, container anyway) and docs/TESTS.md (anyway-tests) both call it, so the two can't drift apart again. TESTS.md now says the loads need an empty test DB and how to start clean (docker compose -f docker-compose-tests.yml down -v). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
…moves to a stacked PR Reverts 48be8ba's script and Tests.yml change (they move to their own PR on top of this one) and keeps the docs/TESTS.md note on starting from an empty test DB (docker compose -f docker-compose-tests.yml down -v). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
|
Thanks @ziv17, great that the tests run locally now. Tests.yml vs TESTS.md: in this PR they load exactly the same data (cities, streets, registered-vehicles, cbs To keep them from drifting apart again, #2972 (stacked on this PR) moves these steps into one script, "Accidents already exist": that happens when the loads run again on a test DB that already has data. This PR now documents a clean start in TESTS.md: It removes only the test environment's containers and its Generated by Claude Code |
|
Hi @atalyaalon , Thanks! |
@ziv17 Sorry about that! Do you have any other thoughts / concerns about the plan? |
Please review the code and the runbook. Merging this changes no running DB. dfc-iac pins
PIN_DB_IMAGE/PIN_DB_BACKUP_IMAGEby hand, and the server migration followsdocs/POSTGRES_18_UPGRADE.md, on dfc2 only.Tasks
Merge order
mainmain(job-levelif:added in dfc-iac#24).terraform apply, use Terraform 1.17.2 (dfc-iac#34) and targeted applies only.wwwAPI/login without redirect; Terraform 1.17.2; workflow keepalive; Airflow log and Docker image cleanup; pinned checkout; DKIM records/datawas always on the 100 GB root disk, so the migration's disk space comes from there. Runbook step 1 measures it; grow the root disk only if short.https://anyway.co.il; deploy images toghcr.io; CI actions pinnednew-cbs-format. #2965 is deployed to dfc2: the app and nginx were recreated, and the DB container wasn't touched. #2967 still publishesghcr.io/data-for-change/anyway/db:sha-<7>anddb_backup:sha-<7>.hasadna→data-for-change/anyway/db_backup:sha-a2e64b9(still PG 15)PIN_DB_BACKUP_IMAGEline to this PR's PG 18 tag.PIN_DB_*. After merging, note thedb:sha-<7>/db_backup:sha-<7>tags, then close #2961 (see Tasks).The same table is in the runbook, under "Merge order".
Why
postgis/postgis:15-3.3image only builds with the archive workaround from Share APT and AWS CLI setup across database images #2962. workaround due to Bullseye's long-term support ended on 2026-08-31. #2961 proposed staying on 15 by moving to bookworm.Changes
db_docker/Dockerfile:postgis/postgis:18-3.7pinned by digest (PostgreSQL 18.7, PostGIS 3.7.0, Debian 13 trixie).db_docker/backup.Dockerfile:postgres:18-trixiepinned by digest. The bullseyeca-certificatesbootstrap is removed.db_docker/install-aws-cli.sh: the bullseye archivesedlines are removed. They would fail on trixie, which has no/etc/apt/sources.list.db_docker/restore_db.sh:DBRESTORE_SKIP=yesskips the restore. The production migration needs this, so a new empty data dir doesn't auto-restore a partial dump.exit 1. Before, theexit 1aborted the first start, and CI and local dev only came up because ofrestart: always.DBRESTORE_FILE_NAME, it restores Restore the latest available partial DB dump instead of today's #2965's newest complete partial dump: the newest date with both_anyway_partialand_anyway_partial_schemafiles, each at least 200 bytes. On top of Restore the latest available partial DB dump instead of today's #2965, this PR only changesexit 1toreturn 0when there's no complete dump, so the DB starts empty with a warning. This file is sourced by the postgres entrypoint, andexitwould abort it.anywayrole password) now reports its errors. It runs withON_ERROR_STOP, and its stderr goes into the same summary. So if it fails, for a connection error or an SQL error (noanywayrole, a quote in the password), the error is counted in the summary and the script printsfailed. Errexit doesn't stop the script before the summary.AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEYand the helper function are unset, because the entrypoint sources this file.alembic/versions/a1b2c3d4e5f6_add_safety_data_user_system.py: the "drop all non-PK constraints" loop now also skips NOT NULL constraints (contype NOT IN ('p', 'n')). PG 18 stores NOT NULL as named constraints, so on a fresh 18 DB the loop tried to dropusers.id's NOT NULL and failed withcolumn "id" is in a primary key. On older versions the behavior is unchanged. Production already ran this migration.docker-compose.yml,docker-compose-tests.yml:/var/lib/postgresql. The PG 18 image refuses a mount at/var/lib/postgresql/data.db_data_pg18/db_tests_data_pg18, so nobody mounts an old PG 15 volume.docs/DOCKER.md: new volume name, and how to switch from the old PG 15 setup.docs/TESTS.md: the steps match CI's (Tests.yml) and the PG 18 test volume. Two waits avoid a race:psql -h 127.0.0.1). Over the unix socket it also succeeded against the temporary server the image runs during init.anyway-tests) before the init steps. Its entrypoint already runsalembic upgrade head, and a manual one at the same time made one of the two fail. The manual line stays as a no-op, to match CI.docs/POSTGRES_18_UPGRADE.md: step-by-step migration runbook for dfc2 (production), covering this repo and dfc-iac. Five independent reviews' findings are applied:DBDUMP_PASSWORD), and re-hash MD5 roles on the 15 server before any dump.pg_restoreand out of the row-count check.cbsDAG: don't dump while it runs, including the manual backups in steps 3 and 8.grep -i,pg_restoreexit code)./datais on the 100 GB root disk. Step 1 measures free space: the DB data dir is about 22 GB, and the migration needs about 35 GB free (the new data dir, the dump and a margin). That should fit after dfc-iac#37's cleanup; grow the root disk only if short. The step 6 EBS snapshot is of dfc2's root volume.restore_db.sh, with the S3 reader credentials fromsecrets/db.env. The quotes are stripped, becausedocker run --env-filekeeps them./tmpare bind-mounted from/data/anyway/pg18testand/data/anyway/pg18test-tmp, with--shm-size=1g, so nothing lands in an anonymous Docker volume that's hard to find and clean up./tmpdirectory needschmod 1777, because the restore runs as thepostgresuser.docker rm -fv pg18testandsudo rm -rfof the two literal paths.dbservice: it has no:latestfallback, so a missingPIN_DB_IMAGEfails instead of pulling an old image.pre-pg18-final_dump expires after 365 days under dfc-iac#24.psycopg2==2.8.5follow-up applies tomasteronly (new-cbs-formatalready haspython:3.10-slim-bookwormandpsycopg22.9).Things to know
postgis_tiger_geocoder. Old DBs have it, because the 15-3.3 image installed it. Restoring a dump therefore logs tiger errors, which can be ignored. The app doesn't use tiger: geocoding is through Google Maps. The runbook's production restore filters tiger out ofpg_restore.docker compose build db. Your DB then starts fresh in a new volume, restored from the newest complete partial dump if your.envhas theDBRESTORE_AWS_*keys.docker volume rm anyway_db_dataremoves the old volume.Testing
alembic upgrade head, data load, pytest), Black and Pylint. Some runs failed inprocess citiesbecause the public OSM Overpass API returned 429 or couldn't be reached, which is unrelated to this PR; re-runs passed. The latest commits change only docs.postgisandpostgis_topology3.7.0./var/lib/postgresql/data: refused by the image, as expected.DBRESTORE_SKIP=yes: skips the restore even with creds set.dumpdb.sh, then restored byrestore_db.sh. Data and views came over. The tiger errors were listed in the summary.return 0), with a fake S3 in the PG 18 image:… copykey for the newest date → ignored, and the previous complete date is used.failedis printed, and the container exits 1.anywayrole →role "anyway" does not existis in the summary, andfailedis printed.--shm-size=1g. Withchmod 1777on the/tmpdirectory, the restore completes and no anonymous volume is created.docker-compose-tests.yml, theDockerfileanddocker-entrypoint.sh.docker compose configwith${PIN_DB_IMAGE:?…}fails clearly when the pin is unset.pg_dump -Fd/pg_restore -Lpath, with tiger filtered out, gave zero errors. A second review re-ran it end to end, with MD5 roles, a GiST index and a materialized view, and got identical row counts.digandcurlconfirmed thatanyway.co.ilandwwwpoint to dfc2, thatwwwanswers308→https://anyway.co.il/, and that/api/onwwwanswers200.200from all 12 hostnames.Related: #2961, #2962, #2965, #2966, #2967, #2968, data-for-change/dfc-iac#24, #26, #27, #28, #30, #33, #34, #37, #39, #42
🤖 Generated with Claude Code
https://claude.ai/code/session_01XtN7GgrUR4pVhFnxNornau