-
Notifications
You must be signed in to change notification settings - Fork 4
Feature collection Views #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
98cb0ad
0ba9e54
a18c1d8
924652c
b33700d
c21c254
cbd7449
73f26bf
9d24983
72c6b00
6510d43
945d140
0d51464
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,6 @@ | |
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
| REFRESH_FUNCTION_NAME = "refresh_pygeoapi_materialized_views" | ||
| REFRESH_JOB_NAME = "refresh_pygeoapi_matviews_nightly" | ||
| REFRESH_SCHEDULE = "0 3 * * *" | ||
|
|
||
| THING_COLLECTIONS = [ | ||
| ("water_wells", "water well"), | ||
|
|
@@ -73,9 +71,7 @@ def _create_thing_view(view_id: str, thing_type: str) -> str: | |
| SELECT | ||
| t.id, | ||
| t.name, | ||
| t.thing_type, | ||
| t.first_visit_date, | ||
| t.spring_type, | ||
| t.nma_pk_welldata, | ||
| t.well_depth, | ||
| t.hole_depth, | ||
|
|
@@ -89,6 +85,7 @@ def _create_thing_view(view_id: str, thing_type: str) -> str: | |
| t.formation_completion_code, | ||
| t.nma_formation_zone, | ||
| t.release_status, | ||
| l.elevation, | ||
| l.point | ||
| FROM thing AS t | ||
| JOIN latest_location AS ll ON ll.thing_id = t.id | ||
|
|
@@ -154,7 +151,7 @@ def _create_avg_tds_view() -> str: | |
| SELECT | ||
| csi.thing_id, | ||
| mc.id AS major_chemistry_id, | ||
| mc."AnalysisDate" AS analysis_date, | ||
| COALESCE(mc."AnalysisDate", csi."CollectionDate")::date AS observation_date, | ||
| mc."SampleValue" AS sample_value, | ||
| mc."Units" AS units | ||
| FROM "NMA_MajorChemistry" AS mc | ||
|
|
@@ -178,8 +175,8 @@ def _create_avg_tds_view() -> str: | |
| t.thing_type, | ||
| COUNT(to2.major_chemistry_id)::integer AS tds_observation_count, | ||
| AVG(to2.sample_value)::double precision AS avg_tds_value, | ||
| MIN(to2.analysis_date) AS first_tds_observation_datetime, | ||
| MAX(to2.analysis_date) AS latest_tds_observation_datetime, | ||
| MIN(to2.observation_date) AS first_tds_observation_date, | ||
| MAX(to2.observation_date) AS last_tds_observation_date, | ||
| l.point | ||
| FROM tds_obs AS to2 | ||
| JOIN thing AS t ON t.id = to2.thing_id | ||
|
|
@@ -231,68 +228,6 @@ def _create_refresh_function() -> str: | |
| """ | ||
|
|
||
|
|
||
| def _schedule_refresh_job() -> str: | ||
| return f""" | ||
| DO $do$ | ||
| BEGIN | ||
| BEGIN | ||
| -- Avoid direct SELECT on cron.job because managed Postgres | ||
| -- environments may deny access to the cron schema table. | ||
| PERFORM cron.unschedule('{REFRESH_JOB_NAME}'); | ||
| EXCEPTION | ||
| WHEN undefined_function THEN | ||
| NULL; | ||
| WHEN invalid_parameter_value THEN | ||
| NULL; | ||
| WHEN internal_error THEN | ||
| -- Some pg_cron builds raise internal_error when the named | ||
| -- job does not exist. Treat this as already-unscheduled. | ||
| NULL; | ||
| WHEN insufficient_privilege THEN | ||
| RAISE NOTICE | ||
| 'Skipping pg_cron unschedule for % due to insufficient privileges.', | ||
| '{REFRESH_JOB_NAME}'; | ||
| RETURN; | ||
| END; | ||
|
|
||
| PERFORM cron.schedule( | ||
| '{REFRESH_JOB_NAME}', | ||
| '{REFRESH_SCHEDULE}', | ||
| $cmd$SELECT public.{REFRESH_FUNCTION_NAME}();$cmd$ | ||
| ); | ||
| EXCEPTION | ||
| WHEN insufficient_privilege THEN | ||
| RAISE NOTICE | ||
| 'Skipping pg_cron schedule for % due to insufficient privileges.', | ||
| '{REFRESH_JOB_NAME}'; | ||
| END | ||
| $do$; | ||
| """ | ||
|
|
||
|
|
||
| def _unschedule_refresh_job() -> str: | ||
| return f""" | ||
| DO $do$ | ||
| BEGIN | ||
| BEGIN | ||
| PERFORM cron.unschedule('{REFRESH_JOB_NAME}'); | ||
| EXCEPTION | ||
| WHEN undefined_function THEN | ||
| NULL; | ||
| WHEN invalid_parameter_value THEN | ||
| NULL; | ||
| WHEN internal_error THEN | ||
| NULL; | ||
| WHEN insufficient_privilege THEN | ||
| RAISE NOTICE | ||
| 'Skipping pg_cron unschedule for % due to insufficient privileges.', | ||
| '{REFRESH_JOB_NAME}'; | ||
| END; | ||
| END | ||
| $do$; | ||
| """ | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| bind = op.get_bind() | ||
| inspector = inspect(bind) | ||
|
|
@@ -307,16 +242,6 @@ def upgrade() -> None: | |
| f"tables are missing: {missing_tables_str}" | ||
| ) | ||
|
|
||
| pg_cron_available = bind.execute( | ||
| text( | ||
| "SELECT EXISTS (" | ||
| "SELECT 1 FROM pg_available_extensions WHERE name = 'pg_cron'" | ||
| ")" | ||
| ) | ||
| ).scalar() | ||
| if pg_cron_available: | ||
| op.execute(text("CREATE EXTENSION IF NOT EXISTS pg_cron")) | ||
|
|
||
| for view_id, thing_type in THING_COLLECTIONS: | ||
| safe_view_id = _safe_view_id(view_id) | ||
| op.execute(text(f"DROP VIEW IF EXISTS ogc_{safe_view_id}")) | ||
|
|
@@ -360,21 +285,9 @@ def upgrade() -> None: | |
| _create_matview_indexes() | ||
|
|
||
| op.execute(text(_create_refresh_function())) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After this change, the migration still creates Useful? React with 👍 / 👎. |
||
| if pg_cron_available: | ||
| op.execute(text(_schedule_refresh_job())) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| bind = op.get_bind() | ||
| pg_cron_available = bind.execute( | ||
| text( | ||
| "SELECT EXISTS (" | ||
| "SELECT 1 FROM pg_available_extensions WHERE name = 'pg_cron'" | ||
| ")" | ||
| ) | ||
| ).scalar() | ||
| if pg_cron_available: | ||
| op.execute(text(_unschedule_refresh_job())) | ||
| op.execute(text(f"DROP FUNCTION IF EXISTS public.{REFRESH_FUNCTION_NAME}()")) | ||
| _drop_view_or_materialized_view("ogc_avg_tds_wells") | ||
| _drop_view_or_materialized_view("ogc_latest_depth_to_water_wells") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit changes the SQL projection in revision
d5e6f7a8b9c0(for example addingl.elevationhere and removing prior columns), but Alembic will never re-run a revision that is already applied. Environments that had already migrated tod5e6f7a8b9c0keep the oldogc_*thing-view schemas, while fresh installs get the new schema, and later revisions in this commit only recreate TDS/trend/summary views. That creates divergent schemas at the same head revision and inconsistent OGC collection payloads across deployments.Useful? React with 👍 / 👎.