Skip to content

pg_cron should be optional for local development#577

Merged
jirhiker merged 1 commit intostagingfrom
fix/pg-cron-optional
Mar 2, 2026
Merged

pg_cron should be optional for local development#577
jirhiker merged 1 commit intostagingfrom
fix/pg-cron-optional

Conversation

@kbighorse
Copy link
Contributor

Summary

  • Make pg_cron extension creation graceful (skip when unavailable) in db/initialization.py, core/initializers.py, and the pygeoapi migration
  • Skip search vector trigger sync for tables that don't exist yet (e.g. asset has no Alembic migration)
  • Unblocks local dev and test environments that don't have pg_cron installed

Refs #576

Test plan

  • CI passes (pg_cron available in CI Docker container — behavior unchanged)
  • Local pytest runs without pg_cron installed
  • alembic upgrade head succeeds locally without pg_cron

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings March 2, 2026 20:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_cron extension 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_cron is 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).

Comment on lines 310 to +318
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"))
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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>
@kbighorse kbighorse force-pushed the fix/pg-cron-optional branch from c56adad to e4cd4e3 Compare March 2, 2026 20:16
@jirhiker jirhiker merged commit 31facf7 into staging Mar 2, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants