pg_cron should be optional for local development#577
Conversation
There was a problem hiding this comment.
Pull request overview
Makes local/dev/test database initialization and migrations tolerant of PostgreSQL servers that don’t have the pg_cron extension installed, while also preventing trigger sync from failing when ORM metadata includes tables that aren’t yet present.
Changes:
- Make
pg_cronextension creation conditional (skip when unavailable) during schema init/rebuild and in the pygeoapi supporting-views migration. - Skip SQLAlchemy-searchable trigger sync for metadata tables that don’t exist in the database yet.
- Keep CI behavior unchanged when
pg_cronis available.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
db/initialization.py |
Skip pg_cron creation when unavailable; avoid trigger sync on non-existent tables. |
core/initializers.py |
Skip pg_cron creation when unavailable during erase/rebuild flow. |
alembic/versions/d5e6f7a8b9c0_create_pygeoapi_supporting_views.py |
Skip creating pg_cron extension when unavailable (but see review comment about scheduling). |
| pg_cron_available = bind.execute( | ||
| text( | ||
| "SELECT EXISTS (" | ||
| "SELECT 1 FROM pg_available_extensions WHERE name = 'pg_cron'" | ||
| ")" | ||
| ) | ||
| ).scalar() | ||
| if not pg_cron_available: | ||
| raise RuntimeError( | ||
| "Cannot schedule nightly pygeoapi materialized view refresh job: " | ||
| "pg_cron extension is not available on this PostgreSQL server." | ||
| ) | ||
| op.execute(text("CREATE EXTENSION IF NOT EXISTS pg_cron")) | ||
| if pg_cron_available: | ||
| op.execute(text("CREATE EXTENSION IF NOT EXISTS pg_cron")) |
There was a problem hiding this comment.
In this migration, pg_cron is only conditionally created, but the upgrade still unconditionally executes _schedule_refresh_job() later. When pg_cron_available is false, the DO block will hit PERFORM cron.schedule(...) and raise undefined_function, causing alembic upgrade to fail (the exception handler only catches insufficient_privilege). Guard the scheduling step behind pg_cron_available and/or extend the PL/pgSQL exception handling to treat undefined_function as a no-op with a NOTICE.
Skip pg_cron extension creation gracefully when unavailable instead of hard-failing, unblocking local dev and test environments. Also skip search vector trigger sync for tables that don't exist yet (e.g. asset) to avoid NoSuchTableError during test setup. Refs #576 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
c56adad to
e4cd4e3
Compare
Summary
db/initialization.py,core/initializers.py, and the pygeoapi migrationassethas no Alembic migration)Refs #576
Test plan
pytestruns without pg_cron installedalembic upgrade headsucceeds locally without pg_cron🤖 Generated with Claude Code