Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions .github/workflows/db-backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ jobs:
cf login -u ${{ secrets.CF_USERNAME }} -p ${{ secrets.CF_PASSWORD }} -a api.fr.cloud.gov -o doi-onrr -s "$CF_SPACE"

- name: Backup database
working-directory: ${{ github.workspace }}
run: |
cg-manage-rds export -o "-F p --no-owner --no-acl" -f backup.sql "$DB_SERVICE" 2>&1 | cat

- name: Verify backup file
run: ls -lh backup.sql
uses: nick-fields/retry@v3
with:
timeout_minutes: 30
max_attempts: 3
retry_wait_seconds: 30
shell: bash
command: |
set -eo pipefail
cd "${{ github.workspace }}"
rm -f backup.sql
cg-manage-rds export -o "-F p --no-owner --no-acl" -f backup.sql "$DB_SERVICE" 2>&1
test -s backup.sql
ls -lh backup.sql

- name: Compress backup
run: |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
databaseChangeLog:
- changeSet:
id: commodity-alias-cy-utilization-paren
author: Jeff Schwartz
changes:
- sql:
dbms: 'postgresql'
splitStatements: false
sql: |
-- The CY CSV carries a hybrid product spelling,
-- "Geothermal - direct utilization (<units>)", that combines the
-- historical "utilization" wording with the parenthetical unit
-- style. Neither existing alias matches it: commodity_alias has
-- "Geothermal - direct use (<units>)" (paren style, "use") and
-- "Geothermal - Direct Utilization, <Units>" (comma style,
-- "utilization"), but nothing with "utilization" AND parens. So
-- the inner join in load_production_calendar_year silently dropped
-- these rows (32 hundreds-of-gallons + 23 millions-of-btus = 55).
--
-- commodity_id is resolved per environment from the post-rename
-- target product (mineral_lease_type=''), matching the approach in
-- commodity-alias-cy-renames, so an environment skew raises an
-- exception instead of silently dropping CY rows again.
DO $$
DECLARE
r RECORD;
v_cid INTEGER;
BEGIN
FOR r IN
SELECT *
FROM (VALUES
('production_calendar_year', 'Geothermal - direct utilization (hundreds of gallons)', 'Geothermal - direct use (hundreds of gallons)', 'CY CSV: utilization + paren-unit hybrid spelling'),
('production_calendar_year', 'Geothermal - direct utilization (millions of btus)', 'Geothermal - direct use (millions of btus)', 'CY CSV: utilization + paren-unit hybrid spelling')
) AS v(source, alias_product, target_product, notes)
LOOP
SELECT commodity_id
INTO v_cid
FROM commodity
WHERE mineral_lease_type = ''
AND product = r.target_product;

IF v_cid IS NULL THEN
RAISE EXCEPTION
'commodity-alias-cy-utilization: no commodity row for (mineral_lease_type='''', product=%); cannot resolve alias_product=%',
r.target_product, r.alias_product;
END IF;

INSERT INTO commodity_alias (source, alias_product, commodity_id, notes)
VALUES (r.source, r.alias_product, v_cid, r.notes)
ON CONFLICT DO NOTHING;
END LOOP;
END $$;
- sql:
dbms: 'postgresql'
sql: |
-- Reload CY production now that the aliases exist so the previously
-- dropped rows are backfilled, then refresh the dependent mviews.
-- Reads the already-populated calendar_year_production_elt; the
-- loader is idempotent (delete_production + re-insert).
CALL load_production_calendar_year();
REFRESH MATERIALIZED VIEW _mview_commodity_qtp;
REFRESH MATERIALIZED VIEW _mview_location_commodity_qtp;
REFRESH MATERIALIZED VIEW _mview_location_qtp;
REFRESH MATERIALIZED VIEW query_tool_production;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
databaseChangeLog:
- changeSet:
id: production-calendar-year-24
id: production-calendar-year-25
author: Lindsay Goldstein
changes:
- sql:
Expand Down
2 changes: 2 additions & 0 deletions database/changelog/changelog-root.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ databaseChangeLog:
file: changelog-data/commodity-alias-seed-narrow.yaml
- include:
file: changelog-data/commodity-alias-cy-renames.yaml
- include:
file: changelog-data/commodity-alias-cy-utilization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
databaseChangeLog:
- changeSet:
id: production-commodity-options-view
author: Jeff Schwartz
runOnChange: true
changes:
- sqlFile:
path: src/views/production_commodity_options.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
CREATE OR REPLACE PROCEDURE load_production_calendar_year()
AS $$
DECLARE
-- The CSV carries many rows per (location, period, commodity) -- e.g. one
-- per offshore lease/block flattened to the offshore-region grain -- so the
-- volume is SUMmed per key. (Previously volume and product were in the
-- GROUP BY, producing one group per distinct volume; since the production
-- PK is (location_id, period_id, commodity_id), all but one collided on
-- INSERT ... ON CONFLICT DO NOTHING and were silently dropped, undercounting
-- offshore Gas/Oil by tens of billions.) unit/unit_abbr are derived from a
-- single MAX() of the product's parenthetical so they stay consistent.
cy_production CURSOR FOR
SELECT location_id,
period_id,
a.commodity_id,
TO_NUMBER(volume, 'L999G999G999G999D99') volume,
COALESCE(SUBSTR(SPLIT_PART(SPLIT_PART(e.product, ' (', 2), ')', 1), 1, 20), '') unit,
COALESCE(SUBSTR(SPLIT_PART(SPLIT_PART(e.product, ' (', 2), ')', 1), 1, 5), '') unit_abbr,
SUM(TO_NUMBER(volume, 'L999G999G999G999D99')) volume,
SUBSTR(COALESCE(MAX(SPLIT_PART(SPLIT_PART(e.product, ' (', 2), ')', 1)), ''), 1, 20) unit,
SUBSTR(COALESCE(MAX(SPLIT_PART(SPLIT_PART(e.product, ' (', 2), ')', 1)), ''), 1, 5) unit_abbr,
COUNT(*) duplicate_no
FROM calendar_year_production_elt e,
location l,
Expand All @@ -25,9 +33,7 @@ DECLARE
AND p.period_date = TO_DATE(concat('01', '/01/', e.calendar_year), 'MM/DD/YYYY')
GROUP BY location_id,
period_id,
a.commodity_id,
volume,
e.product;
a.commodity_id;

v_from_date period.period_date%TYPE;
BEGIN
Expand Down
11 changes: 11 additions & 0 deletions database/changelog/src/views/production_commodity_options.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP VIEW IF EXISTS production_commodity_options;

CREATE VIEW production_commodity_options AS
SELECT DISTINCT commodity.product,
commodity.commodity_order,
period.period
FROM commodity
JOIN production USING (commodity_id)
JOIN period USING (period_id)
WHERE period.period IN ('Calendar Year', 'Fiscal Year')
ORDER BY commodity.commodity_order;
Loading
Loading