From 8578157ae45eef3d66ae6f96f55ae299916a444b Mon Sep 17 00:00:00 2001 From: tangoyankee Date: Tue, 12 Nov 2024 17:52:27 -0500 Subject: [PATCH] Move capital project fund stage to constraint Remove the capital project fund stage enum - The stage field of the capital project fund table now uses a check constraint closes #63 --- drizzle/migration/schema.ts | 6 - pg/model-transform/capital-planning.sql | 159 ++++++++++++------------ 2 files changed, 79 insertions(+), 86 deletions(-) diff --git a/drizzle/migration/schema.ts b/drizzle/migration/schema.ts index c0f89da..4a02a15 100644 --- a/drizzle/migration/schema.ts +++ b/drizzle/migration/schema.ts @@ -15,12 +15,6 @@ export const capital_project_category = pgEnum("capital_project_category", [ "Lump Sum", "ITT, Vehicles and Equipment", ]); -export const capital_project_fund_stage = pgEnum("capital_project_fund_stage", [ - "adopt", - "allocate", - "commit", - "spent", -]); export const category = pgEnum("category", [ "Residential", "Commercial", diff --git a/pg/model-transform/capital-planning.sql b/pg/model-transform/capital-planning.sql index 24b1f38..b0282a5 100644 --- a/pg/model-transform/capital-planning.sql +++ b/pg/model-transform/capital-planning.sql @@ -1,4 +1,4 @@ -TRUNCATE +TRUNCATE managing_code, agency, capital_project, @@ -27,7 +27,7 @@ FROM source_capital_project; -- Move sponsoring agencies from commitment table into agency table INSERT INTO agency -SELECT DISTINCT +SELECT DISTINCT s_agency_acro as initials, s_agency_name as name FROM source_capital_commitment @@ -35,7 +35,7 @@ WHERE s_agency_acro NOT IN (SELECT initials FROM agency); -- Move project source into project target INSERT INTO capital_project -SELECT +SELECT m_agency AS managing_code, proj_id AS id, m_agency_acro AS managing_agency, @@ -46,7 +46,7 @@ SELECT -- This was unintentional but the simplest way to rectify -- the data source with the API database is to coerce the -- source value to drop the oxford comma - CASE + CASE WHEN type_category = 'Fixed Asset' OR type_category = 'Fixed Asset' OR type_category IS NULL @@ -65,275 +65,275 @@ FROM source_capital_project; -- adopt INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-non-exempt'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_ccnonexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-exempt'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_ccexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-cost'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_citycost AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-state'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_nccstate AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-federal'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_nccfederal AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-other'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_nccother AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-cost'::capital_fund_category AS category, - 'adopt'::capital_project_fund_stage AS stage, + 'adopt' AS stage, adopt_noncitycost AS value FROM source_capital_project; -- allocate INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-non-exempt'::capital_fund_category AS category, - 'allocate'::capital_project_fund_stage AS stage, + 'allocate' AS stage, allocate_ccnonexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-exempt'::capital_fund_category AS category, - 'allocate'::capital_project_fund_stage AS stage, + 'allocate' AS stage, allocate_ccexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-cost'::capital_fund_category AS category, - 'allocate'::capital_project_fund_stage AS stage, + 'allocate' AS stage, allocate_citycost AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-state'::capital_fund_category AS category, - 'allocate'::capital_project_fund_stage AS stage, + 'allocate' AS stage, allocate_nccstate AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-federal'::capital_fund_category AS category, - 'allocate'::capital_project_fund_stage AS stage, + 'allocate' AS stage, allocate_nccfederal AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-other'::capital_fund_category AS category, - 'allocate'::capital_project_fund_stage AS stage, + 'allocate' AS stage, allocate_nccother AS value FROM source_capital_project; -- commit INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-non-exempt'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_ccnonexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-exempt'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_ccexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-cost'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_citycost AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-state'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_nccstate AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-federal'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_nccfederal AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-other'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_nccother AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-cost'::capital_fund_category AS category, - 'commit'::capital_project_fund_stage AS stage, + 'commit' AS stage, commit_noncitycost AS value FROM source_capital_project; -- spent INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-non-exempt'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_ccnonexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-exempt'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_ccexempt AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'city-cost'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_citycost AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-state'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_nccstate AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-federal'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_nccfederal AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-other'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_nccother AS value FROM source_capital_project; INSERT INTO capital_project_fund -SELECT +SELECT gen_random_uuid() AS id, m_agency AS managing_code, proj_id AS capital_project_id, 'non-city-cost'::capital_fund_category AS category, - 'spent'::capital_project_fund_stage AS stage, + 'spent' AS stage, spent_noncitycost AS value FROM source_capital_project; @@ -346,7 +346,7 @@ FROM source_capital_commitment; -- Move data from commitment source to agency_budget INSERT INTO agency_budget -SELECT DISTINCT +SELECT DISTINCT SPLIT_PART(budget_line, '-', 1) AS code, project_type AS type, s_agency_acro AS sponsor @@ -354,7 +354,7 @@ FROM source_capital_commitment; -- Move data from commitment source to budget_line INSERT INTO budget_line -SELECT DISTINCT +SELECT DISTINCT SPLIT_PART(budget_line, '-', 1) AS code, SPLIT_PART(budget_line, '-', 2) AS id FROM source_capital_commitment; @@ -366,89 +366,89 @@ FROM source_capital_commitment; -- Move data from commitment source id to commitment INSERT INTO capital_commitment -SELECT +SELECT id, commitment_code AS type, TO_DATE(plan_comm_date, 'MM/YY') AS planned_date, m_agency AS managing_code, project_id AS capital_project_id, - SPLIT_PART(budget_line, '-', 1) AS budget_line_code, + SPLIT_PART(budget_line, '-', 1) AS budget_line_code, SPLIT_PART(budget_line, '-', 2) AS budget_line_id FROM source_capital_commitment_id; -- Move data from commitment source id to commitment_fund INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'city-non-exempt'::capital_fund_category AS category, plannedcommit_ccnonexempt AS value FROM source_capital_commitment_id; INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'city-exempt'::capital_fund_category AS category, plannedcommit_ccexempt AS value FROM source_capital_commitment_id; INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'non-city-state'::capital_fund_category AS category, plannedcommit_nccstate AS value FROM source_capital_commitment_id; INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'non-city-federal'::capital_fund_category AS category, plannedcommit_nccfederal AS value FROM source_capital_commitment_id; INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'non-city-other'::capital_fund_category AS category, plannedcommit_nccother AS value FROM source_capital_commitment_id; INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'non-city-cost'::capital_fund_category AS category, plannedcommit_noncitycost AS value FROM source_capital_commitment_id; INSERT INTO capital_commitment_fund -SELECT - gen_random_uuid() AS id, +SELECT + gen_random_uuid() AS id, id AS capital_commitment_id, 'total'::capital_fund_category AS category, plannedcommit_total AS value FROM source_capital_commitment_id; WITH capital_project_spatial AS ( - SELECT + SELECT COALESCE(source_capital_project_m_poly.magency, source_capital_project_m_pnt.magency) AS managing_code, COALESCE(source_capital_project_m_poly.projectid, source_capital_project_m_pnt.projectid) id, source_capital_project_m_poly.wkt AS m_poly, source_capital_project_m_pnt.wkt AS m_pnt FROM source_capital_project_m_poly FULL OUTER JOIN source_capital_project_m_pnt - ON source_capital_project_m_poly.magency = source_capital_project_m_pnt.magency AND + ON source_capital_project_m_poly.magency = source_capital_project_m_pnt.magency AND source_capital_project_m_poly.projectid = source_capital_project_m_pnt.projectid ) UPDATE capital_project -- Need to transform to 2263 because it is imported as 4326 SET li_ft_m_pnt = ST_Transform(capital_project_spatial.m_pnt, 2263), li_ft_m_poly = ST_Transform(capital_project_spatial.m_poly, 2263), - mercator_label = CASE + mercator_label = CASE WHEN capital_project_spatial.m_pnt IS NOT NULL THEN ST_Transform(ST_PointOnSurface(capital_project_spatial.m_pnt), 3857) WHEN capital_project_spatial.m_poly IS NOT NULL THEN ST_Transform((ST_MaximumInscribedCircle(capital_project_spatial.m_poly)).center, 3857) END, @@ -469,4 +469,3 @@ COPY agency_budget TO '/var/lib/postgresql/data/agency_budget.csv'; COPY budget_line TO '/var/lib/postgresql/data/budget_line.csv'; COPY capital_commitment TO '/var/lib/postgresql/data/capital_commitment.csv'; COPY capital_commitment_fund TO '/var/lib/postgresql/data/capital_commitment_fund.csv'; - \ No newline at end of file