From fa6db411f9fc7e640971e3ffe8c650a630154273 Mon Sep 17 00:00:00 2001 From: tangoyankee Date: Fri, 5 Jul 2024 17:25:24 -0400 Subject: [PATCH] update create tables to lumpy the enforcers --- bash/export.sh | 2 +- sql/create_tables.sql | 295 +++++++++++++++++++++++------------------- 2 files changed, 164 insertions(+), 133 deletions(-) diff --git a/bash/export.sh b/bash/export.sh index b3bd9bd..c925a76 100755 --- a/bash/export.sh +++ b/bash/export.sh @@ -31,7 +31,7 @@ mkdir -p ${DATA_DIRECTORY} && ( -t capital_project \ -t capital_project_checkbook \ -t capital_project_fund \ - -t captial_commitment \ + -t capital_commitment \ -t city_council_district \ -t community_district \ -t managing_code \ diff --git a/sql/create_tables.sql b/sql/create_tables.sql index c23e41f..28559ac 100644 --- a/sql/create_tables.sql +++ b/sql/create_tables.sql @@ -1,19 +1,63 @@ --- Zoning districts and tax lots -DROP TABLE IF EXISTS borough CASCADE; +-- Current sql file was generated after introspecting the database +-- If you want to run this migration please uncomment this code before executing migrations +DO $$ BEGIN + CREATE TYPE "public"."capital_fund_category" AS ENUM('city-non-exempt', 'city-exempt', 'city-cost', 'non-city-state', 'non-city-federal', 'non-city-other', 'non-city-cost', 'total'); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + CREATE TYPE "public"."capital_project_category" AS ENUM('Fixed Asset', 'Lump Sum', 'ITT, Vehicles and Equipment'); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + CREATE TYPE "public"."capital_project_fund_stage" AS ENUM('adopt', 'allocate', 'commit', 'spent'); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + CREATE TYPE "public"."category" AS ENUM('Residential', 'Commercial', 'Manufacturing'); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "geography_columns" ( + "f_table_catalog" "name", + "f_table_schema" "name", + "f_table_name" "name", + "f_geography_column" "name", + "coord_dimension" integer, + "srid" integer, + "type" text +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "geometry_columns" ( + "f_table_catalog" varchar(256), + "f_table_schema" "name", + "f_table_name" "name", + "f_geometry_column" "name", + "coord_dimension" integer, + "srid" integer, + "type" varchar(30) +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "spatial_ref_sys" ( + "srid" integer PRIMARY KEY NOT NULL, + "auth_name" varchar(256), + "auth_srid" integer, + "srtext" varchar(2048), + "proj4text" varchar(2048) +); +--> statement-breakpoint CREATE TABLE IF NOT EXISTS "borough" ( "id" char(1) PRIMARY KEY NOT NULL, "title" text NOT NULL, "abbr" text NOT NULL ); - -DROP TABLE IF EXISTS land_use CASCADE; -CREATE TABLE IF NOT EXISTS "land_use" ( - "id" char(2) PRIMARY KEY NOT NULL, - "description" text NOT NULL, - "color" char(9) NOT NULL -); - -DROP TABLE IF EXISTS tax_lot CASCADE; +--> statement-breakpoint CREATE TABLE IF NOT EXISTS "tax_lot" ( "bbl" char(10) PRIMARY KEY NOT NULL, "borough_id" char(1) NOT NULL, @@ -21,38 +65,28 @@ CREATE TABLE IF NOT EXISTS "tax_lot" ( "lot" text NOT NULL, "address" text, "land_use_id" char(2), - "wgs84" geography(multiPolygon, 4326) NOT NULL, - "li_ft" geometry(multiPolygon,2263) NOT NULL + "wgs84" "geography" NOT NULL, + "li_ft" geometry(MultiPolygon,2263) NOT NULL ); - -DO $$ BEGIN - ALTER TABLE "tax_lot" ADD CONSTRAINT "tax_lot_borough_id_borough_id_fk" FOREIGN KEY ("borough_id") REFERENCES "borough"("id") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; - -DO $$ BEGIN - ALTER TABLE "tax_lot" ADD CONSTRAINT "tax_lot_land_use_id_land_use_id_fk" FOREIGN KEY ("land_use_id") REFERENCES "land_use"("id") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; - - -DO $$ BEGIN - CREATE TYPE "category" AS ENUM('Residential', 'Commercial', 'Manufacturing'); -EXCEPTION - WHEN duplicate_object THEN null; -END $$; - -DROP TABLE IF EXISTS zoning_district CASCADE; +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "land_use" ( + "id" char(2) PRIMARY KEY NOT NULL, + "description" text NOT NULL, + "color" char(9) NOT NULL +); +--> statement-breakpoint CREATE TABLE IF NOT EXISTS "zoning_district" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "label" text NOT NULL, - "wgs84" geography(multiPolygon, 4326) NOT NULL, - "li_ft" geometry(multiPolygon,2263) NOT NULL + "wgs84" "geography" NOT NULL, + "li_ft" geometry(MultiPolygon,2263) NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "zoning_district_zoning_district_class" ( + "zoning_district_id" uuid NOT NULL, + "zoning_district_class_id" text NOT NULL ); - -DROP TABLE IF EXISTS zoning_district_class CASCADE; +--> statement-breakpoint CREATE TABLE IF NOT EXISTS "zoning_district_class" ( "id" text PRIMARY KEY NOT NULL, "category" "category", @@ -60,59 +94,23 @@ CREATE TABLE IF NOT EXISTS "zoning_district_class" ( "url" text, "color" char(9) NOT NULL ); - -DROP TABLE IF EXISTS zoning_district_zoning_district_class CASCADE; -CREATE TABLE IF NOT EXISTS "zoning_district_zoning_district_class" ( - "zoning_district_id" uuid NOT NULL, - "zoning_district_class_id" text NOT NULL -); - -DO $$ BEGIN - ALTER TABLE "zoning_district_zoning_district_class" ADD CONSTRAINT "zoning_district_zoning_district_class_zoning_district_id_zoning_district_id_fk" FOREIGN KEY ("zoning_district_id") REFERENCES "zoning_district"("id") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; - -DO $$ BEGIN - ALTER TABLE "zoning_district_zoning_district_class" ADD CONSTRAINT "zoning_district_zoning_district_class_zoning_district_class_id_zoning_district_class_id_fk" FOREIGN KEY ("zoning_district_class_id") REFERENCES "zoning_district_class"("id") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; - --- Capital Planning -DO $$ BEGIN - CREATE TYPE "capital_fund_category" AS ENUM('city-non-exempt', 'city-exempt', 'city-cost', 'non-city-state', 'non-city-federal', 'non-city-other', 'non-city-cost', 'total'); -EXCEPTION - WHEN duplicate_object THEN null; -END $$; --> statement-breakpoint -DO $$ BEGIN - CREATE TYPE "capital_project_fund_stage" AS ENUM('adopt', 'allocate', 'commit', 'spent'); -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - CREATE TYPE "capital_project_category" AS ENUM('Fixed Asset', 'Lump Sum', 'ITT, Vehicles and Equipment'); -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "agency_budget" ( - "code" text PRIMARY KEY NOT NULL, - "type" text, - "sponsor" text +CREATE TABLE IF NOT EXISTS "city_council_district" ( + "id" text PRIMARY KEY NOT NULL, + "li_ft" geometry(MultiPolygon,2263), + "mercator_fill" geometry(MultiPolygon,3857), + "mercator_label" geometry(Point,3857) ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "agency" ( "initials" text PRIMARY KEY NOT NULL, - "name" text + "name" text NOT NULL ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "budget_line" ( - "code" text, - "id" text, - CONSTRAINT budget_line_code_id_pk PRIMARY KEY("code","id") +CREATE TABLE IF NOT EXISTS "agency_budget" ( + "code" text PRIMARY KEY NOT NULL, + "type" text, + "sponsor" text ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "capital_commitment_fund" ( @@ -127,16 +125,6 @@ CREATE TABLE IF NOT EXISTS "capital_commitment_type" ( "description" text ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "captial_commitment" ( - "id" uuid PRIMARY KEY NOT NULL, - "type" char(4), - "planned_date" date, - "managing_code" char(3), - "capital_project_id" text, - "budget_line_code" text, - "budget_line_id" text -); ---> statement-breakpoint CREATE TABLE IF NOT EXISTS "capital_project_checkbook" ( "id" uuid PRIMARY KEY NOT NULL, "managing_code" char(3), @@ -153,105 +141,148 @@ CREATE TABLE IF NOT EXISTS "capital_project_fund" ( "value" numeric ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "capital_project" ( +CREATE TABLE IF NOT EXISTS "managing_code" ( + "id" char(3) PRIMARY KEY NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "capital_commitment" ( + "id" uuid PRIMARY KEY NOT NULL, + "type" char(4), + "planned_date" date, "managing_code" char(3), - "id" text, - "managing_agency" text, - "description" text, - "min_date" date, - "max_date" date, - "category" "capital_project_category", - "li_ft_m_pnt" geometry(multiPoint,2263), - "li_ft_m_poly" geometry(multiPolygon,2263), - "mercator_label" geometry(point,3857), - "mercator_fill_m_pnt" geometry(multiPoint,3857), - "mercator_fill_m_poly" geometry(multiPolygon,3857), - CONSTRAINT capital_project_managing_code_id_pk PRIMARY KEY("managing_code","id") + "capital_project_id" text, + "budget_line_code" text, + "budget_line_id" text ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "city_council_district" ( - "id" text PRIMARY KEY NOT NULL, - "li_ft" geometry(multiPolygon,2263), - "mercator_fill" geometry(multiPolygon,3857), - "mercator_label" geometry(point,3857) +CREATE TABLE IF NOT EXISTS "budget_line" ( + "code" text NOT NULL, + "id" text NOT NULL, + CONSTRAINT "budget_line_code_id_pk" PRIMARY KEY("code","id") ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "community_district" ( - "borough_id" char(1), - "id" char(2), - "li_ft" geometry(multiPolygon,2263), - "mercator_fill" geometry(multiPolygon,3857), - "mercator_label" geometry(point,3857), - CONSTRAINT community_district_borough_id_id_pk PRIMARY KEY("borough_id","id") + "borough_id" char(1) NOT NULL, + "id" char(2) NOT NULL, + "mercator_fill" geometry(MultiPolygon,3857), + "mercator_label" geometry(Point,3857), + "li_ft" geometry(MultiPolygon,2263), + CONSTRAINT "community_district_borough_id_id_pk" PRIMARY KEY("borough_id","id") ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "managing_code" ( - "id" char(3) PRIMARY KEY NOT NULL +CREATE TABLE IF NOT EXISTS "capital_project" ( + "managing_code" char(3) NOT NULL, + "id" text NOT NULL, + "managing_agency" text NOT NULL, + "description" text NOT NULL, + "min_date" date NOT NULL, + "max_date" date NOT NULL, + "category" "capital_project_category", + "li_ft_m_pnt" geometry(MultiPoint,2263), + "li_ft_m_poly" geometry(MultiPolygon,2263), + "mercator_label" geometry(Point,3857), + "mercator_fill_m_pnt" geometry(MultiPoint,3857), + "mercator_fill_m_poly" geometry(MultiPolygon,3857), + CONSTRAINT "capital_project_managing_code_id_pk" PRIMARY KEY("managing_code","id") ); --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "agency_budget" ADD CONSTRAINT "agency_budget_sponsor_agency_initials_fk" FOREIGN KEY ("sponsor") REFERENCES "agency"("initials") ON DELETE no action ON UPDATE no action; + ALTER TABLE "tax_lot" ADD CONSTRAINT "tax_lot_borough_id_borough_id_fk" FOREIGN KEY ("borough_id") REFERENCES "public"."borough"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "tax_lot" ADD CONSTRAINT "tax_lot_land_use_id_land_use_id_fk" FOREIGN KEY ("land_use_id") REFERENCES "public"."land_use"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "zoning_district_zoning_district_class" ADD CONSTRAINT "zoning_district_zoning_district_class_zoning_district_id_zoning" FOREIGN KEY ("zoning_district_id") REFERENCES "public"."zoning_district"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "zoning_district_zoning_district_class" ADD CONSTRAINT "zoning_district_zoning_district_class_zoning_district_class_id_" FOREIGN KEY ("zoning_district_class_id") REFERENCES "public"."zoning_district_class"("id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "budget_line" ADD CONSTRAINT "budget_line_code_agency_budget_code_fk" FOREIGN KEY ("code") REFERENCES "agency_budget"("code") ON DELETE no action ON UPDATE no action; + ALTER TABLE "agency_budget" ADD CONSTRAINT "agency_budget_sponsor_agency_initials_fk" FOREIGN KEY ("sponsor") REFERENCES "public"."agency"("initials") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "capital_commitment_fund" ADD CONSTRAINT "capital_commitment_fund_capital_commitment_id_captial_commitment_id_fk" FOREIGN KEY ("capital_commitment_id") REFERENCES "captial_commitment"("id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_commitment_fund" ADD CONSTRAINT "capital_commitment_fund_capital_commitment_id_capital_commitmen" FOREIGN KEY ("capital_commitment_id") REFERENCES "public"."capital_commitment"("id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "captial_commitment" ADD CONSTRAINT "captial_commitment_type_capital_commitment_type_code_fk" FOREIGN KEY ("type") REFERENCES "capital_commitment_type"("code") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_project_checkbook" ADD CONSTRAINT "custom_fk" FOREIGN KEY ("managing_code","capital_project_id") REFERENCES "public"."capital_project"("managing_code","id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "captial_commitment" ADD CONSTRAINT "captial_commitment_managing_code_capital_project_id_capital_project_managing_code_id_fk" FOREIGN KEY ("managing_code","capital_project_id") REFERENCES "capital_project"("managing_code","id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_project_fund" ADD CONSTRAINT "custom_fk" FOREIGN KEY ("managing_code","capital_project_id") REFERENCES "public"."capital_project"("managing_code","id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "captial_commitment" ADD CONSTRAINT "captial_commitment_budget_line_code_budget_line_id_budget_line_code_id_fk" FOREIGN KEY ("budget_line_code","budget_line_id") REFERENCES "budget_line"("code","id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_commitment" ADD CONSTRAINT "capital_commitment_type_capital_commitment_type_code_fk" FOREIGN KEY ("type") REFERENCES "public"."capital_commitment_type"("code") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "capital_project_checkbook" ADD CONSTRAINT "custom_fk" FOREIGN KEY ("managing_code","capital_project_id") REFERENCES "capital_project"("managing_code","id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_commitment" ADD CONSTRAINT "capital_commitment_managing_code_capital_project_id_capital_pro" FOREIGN KEY ("managing_code","capital_project_id") REFERENCES "public"."capital_project"("managing_code","id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "capital_project_fund" ADD CONSTRAINT "custom_fk" FOREIGN KEY ("managing_code","capital_project_id") REFERENCES "capital_project"("managing_code","id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_commitment" ADD CONSTRAINT "capital_commitment_budget_line_code_budget_line_id_budget_line_" FOREIGN KEY ("budget_line_code","budget_line_id") REFERENCES "public"."budget_line"("code","id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "capital_project" ADD CONSTRAINT "capital_project_managing_code_managing_code_id_fk" FOREIGN KEY ("managing_code") REFERENCES "managing_code"("id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "budget_line" ADD CONSTRAINT "budget_line_code_agency_budget_code_fk" FOREIGN KEY ("code") REFERENCES "public"."agency_budget"("code") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "capital_project" ADD CONSTRAINT "capital_project_managing_agency_agency_initials_fk" FOREIGN KEY ("managing_agency") REFERENCES "agency"("initials") ON DELETE no action ON UPDATE no action; + ALTER TABLE "community_district" ADD CONSTRAINT "community_district_borough_id_borough_id_fk" FOREIGN KEY ("borough_id") REFERENCES "public"."borough"("id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "community_district" ADD CONSTRAINT "community_district_borough_id_borough_id_fk" FOREIGN KEY ("borough_id") REFERENCES "borough"("id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "capital_project" ADD CONSTRAINT "capital_project_managing_code_managing_code_id_fk" FOREIGN KEY ("managing_code") REFERENCES "public"."managing_code"("id") ON DELETE no action ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; - +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "capital_project" ADD CONSTRAINT "capital_project_managing_agency_agency_initials_fk" FOREIGN KEY ("managing_agency") REFERENCES "public"."agency"("initials") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "city_council_district_li_ft_index" ON "city_council_district" USING gist ("li_ft" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "city_council_district_mercator_fill_index" ON "city_council_district" USING gist ("mercator_fill" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "city_council_district_mercator_label_index" ON "city_council_district" USING gist ("mercator_label" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "community_district_li_ft_index" ON "community_district" USING gist ("li_ft" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "community_district_mercator_fill_index" ON "community_district" USING gist ("mercator_fill" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "community_district_mercator_label_index" ON "community_district" USING gist ("mercator_label" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "capital_project_li_ft_m_pnt_index" ON "capital_project" USING gist ("li_ft_m_pnt" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "capital_project_li_ft_m_poly_index" ON "capital_project" USING gist ("li_ft_m_poly" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "capital_project_mercator_fill_m_pnt_index" ON "capital_project" USING gist ("mercator_fill_m_pnt" gist_geometry_ops_2d);--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "capital_project_mercator_fill_m_poly_index" ON "capital_project" USING gist ("mercator_fill_m_poly" gist_geometry_ops_2d); \ No newline at end of file