colouring-montreal/migrations/016.landuse.up.sql

59 lines
2.4 KiB
MySQL
Raw Normal View History

2019-12-02 10:57:22 -05:00
-- Create land use and fields
2023-01-04 07:17:56 -05:00
--Landuse is hierarchical. Highest level is Order (Residential) then Group (Residential-Dwelling) then Class (Residential-Dwelling-Detached house)
2019-12-02 10:57:22 -05:00
--Some ETL work required to get this together refer to analysis repo
--Prerequesite is to have first run bulk_data_sources migrations
--Then create table landuse_order for the app, this is used as foreign key for current and original landuse_order
CREATE TABLE IF NOT EXISTS reference_tables.buildings_landuse_order AS
SELECT a.landuse_id,
a.description
FROM reference_tables.landuse_classifications a
WHERE a.level = 'order'
AND a.is_used;
ALTER TABLE reference_tables.buildings_landuse_order
ADD UNIQUE (description);
CREATE TABLE IF NOT EXISTS reference_tables.buildings_landuse_group AS
2019-12-02 10:57:22 -05:00
SELECT a.landuse_id,
a.description,
a.parent_id AS parent_order_id
2019-12-02 10:57:22 -05:00
FROM reference_tables.landuse_classifications a
WHERE a.level = 'group'
AND a.is_used;
ALTER TABLE reference_tables.buildings_landuse_group
ADD UNIQUE (description);
2019-12-02 10:57:22 -05:00
--the below is for front end reference, not current used as a constraint
CREATE TABLE IF NOT EXISTS reference_tables.buildings_landuse_class AS
SELECT a.landuse_id,
a.description,
a.parent_id AS parent_group_id
FROM reference_tables.landuse_classifications a
WHERE a.level = 'class'
AND a.is_used;
2019-12-02 10:57:22 -05:00
2023-01-04 07:17:56 -05:00
--Landuse is hierarchical. Highest level is Order (Residential) then Group (Residential-Dwelling) then Class (Residential-Dwelling-Detached house)
--Interface will collected most detailed (class) but visualise highest level (order)
--Landuse is a table as #358
2019-12-02 10:57:22 -05:00
--Prerequisite run bulk_sources migration first
2023-01-04 07:17:56 -05:00
-- Land use is table with 3 levels of hierarchy (highest to lowest). order > group > class
-- Land use order, singular. Client and db constrained with foreign key
ALTER TABLE buildings
ADD COLUMN IF NOT EXISTS current_landuse_order text,
ADD FOREIGN KEY (current_landuse_order)
REFERENCES reference_tables.buildings_landuse_order (description);
-- Land use groups, array. Derived from classes.
ALTER TABLE buildings ADD COLUMN IF NOT EXISTS current_landuse_group text ARRAY[41];
-- Land use class or classes, array object, client constrained. ARRAY[] is used to constrain array size. The array is limited to 250 based on Westfield Stratford as a single toid with many uses, this may want to be reduced down to reduce maximum size.
2019-12-02 10:57:22 -05:00
ALTER TABLE buildings ADD COLUMN IF NOT EXISTS current_landuse_class text ARRAY[250];