Skip to content

Commit

Permalink
Migrations update
Browse files Browse the repository at this point in the history
  • Loading branch information
gpanag committed Mar 9, 2023
1 parent 4d59caf commit 7ddd381
Show file tree
Hide file tree
Showing 29 changed files with 794 additions and 1,401 deletions.
77 changes: 55 additions & 22 deletions migrations/001.core.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
CREATE TABLE IF NOT EXISTS geometries (
-- internal unique id
geometry_id serial PRIMARY KEY,
-- cross-reference to data source id
source_id varchar(30),
-- replaced source id with ELSTAT and OSM ids
elstat_id varchar(30),
osm_id varchar(30),
-- geometry as EPSG:3857 avoiding reprojection for tiles
geometry_geom geometry(GEOMETRY, 3857)
);
Expand All @@ -21,13 +22,19 @@ CREATE TABLE IF NOT EXISTS buildings (
-- internal unique id
building_id serial PRIMARY KEY,
-- OS MasterMap topo id
ref_toid varchar,
--ref_toid varchar,
--replaced by Hellenic Statistics building ID
ref_elstat_id varchar,
-- OSM reference id
ref_osm_id bigint,
ref_osm_id varchar,
-- reference to geometry, aiming to decouple from geometry provider
geometry_id integer REFERENCES geometries
);
ALTER TABLE buildings ADD CONSTRAINT buildings_ref_toid_len CHECK (length(ref_toid) < 90);

ALTER TABLE
buildings
ADD
CONSTRAINT buildings_ref_elstat_id CHECK (length(ref_elstat_id) < 90);

--
-- Properties table
Expand All @@ -38,15 +45,15 @@ CREATE TABLE IF NOT EXISTS building_properties (
-- internal primary key
building_property_id serial PRIMARY KEY,
-- UPRN
uprn bigint,
--uprn bigint,
-- Parent should reference UPRN, but assume dataset may be (initially) incomplete
parent_uprn bigint,
--parent_uprn bigint,
-- Building ID may be null for failed matches
building_id integer REFERENCES buildings,
building_id integer REFERENCES buildings
-- TOID match provided by AddressBase
toid varchar,
--toid varchar,
-- Geometry (for verification if loaded, not for public access)
uprn_geom geometry(POINT, 3857)
--uprn_geom geometry(POINT, 3857)
);

--
Expand All @@ -58,7 +65,11 @@ CREATE TABLE IF NOT EXISTS user_categories (
-- category name/short description
category varchar
);
INSERT INTO user_categories ( category_id, category ) VALUES ( 1, 'Not provided');

INSERT INTO
user_categories (category_id, category)
VALUES
(1, 'Not provided');

--
-- User access levels
Expand All @@ -69,7 +80,11 @@ CREATE TABLE IF NOT EXISTS user_access_levels (
-- name/short description
access_level varchar
);
INSERT INTO user_access_levels ( access_level_id, access_level ) VALUES ( 1, 'untrusted');

INSERT INTO
user_access_levels (access_level_id, access_level)
VALUES
(1, 'untrusted');

--
-- Users table
Expand All @@ -93,12 +108,25 @@ CREATE TABLE IF NOT EXISTS users (
-- user API key - to give limited query/edit access
api_key uuid UNIQUE default NULL
);
ALTER TABLE users ADD CONSTRAINT users_username_len CHECK (length(username) < 30);
ALTER TABLE users ADD CONSTRAINT users_email_len CHECK (length(email) < 50);
ALTER TABLE users ADD CONSTRAINT users_pass_len CHECK (length(pass) <= 60);

CREATE INDEX IF NOT EXISTS user_username_idx ON users ( username );
CREATE INDEX IF NOT EXISTS user_email_idx ON users ( email );
ALTER TABLE
users
ADD
CONSTRAINT users_username_len CHECK (length(username) < 30);

ALTER TABLE
users
ADD
CONSTRAINT users_email_len CHECK (length(email) < 50);

ALTER TABLE
users
ADD
CONSTRAINT users_pass_len CHECK (length(pass) <= 60);

CREATE INDEX IF NOT EXISTS user_username_idx ON users (username);

CREATE INDEX IF NOT EXISTS user_email_idx ON users (email);

--
-- User session table
Expand All @@ -109,7 +137,7 @@ CREATE TABLE IF NOT EXISTS user_sessions (
expire timestamp(6) NOT NULL
);

CREATE INDEX IF NOT EXISTS user_sessions_expire_idx on user_sessions ( expire );
CREATE INDEX IF NOT EXISTS user_sessions_expire_idx on user_sessions (expire);

--
-- Logs table
Expand All @@ -131,8 +159,13 @@ CREATE TABLE IF NOT EXISTS logs (
building_id integer REFERENCES buildings
);

CREATE INDEX IF NOT EXISTS log_timestamp_idx ON logs ( log_timestamp );
CREATE INDEX IF NOT EXISTS log_user_idx ON logs ( user_id );
CREATE INDEX IF NOT EXISTS log_building_idx ON logs ( building_id );
CREATE INDEX IF NOT EXISTS log_timestamp_idx ON logs (log_timestamp);

CREATE INDEX IF NOT EXISTS log_user_idx ON logs (user_id);

CREATE INDEX IF NOT EXISTS log_building_idx ON logs (building_id);

ALTER TABLE buildings ADD COLUMN revision_id bigint REFERENCES logs ( log_id );
ALTER TABLE
buildings
ADD
COLUMN revision_id bigint REFERENCES logs (log_id);
3 changes: 2 additions & 1 deletion migrations/002.index-geometries.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
DROP INDEX IF EXISTS geometries_idx;

-- Source ID index over geometries
DROP INDEX IF EXISTS geometries_source_idx;
DROP INDEX IF EXISTS geometries_elstat_idx;
DROP INDEX IF EXISTS geometries_osm_idx;

-- Index over building geometry_id (expect to look up building by geometry_id for map tiles)
DROP INDEX IF EXISTS building_geometry_idx;
4 changes: 3 additions & 1 deletion migrations/002.index-geometries.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
CREATE INDEX IF NOT EXISTS geometries_idx ON geometries USING GIST ( geometry_geom );

-- Source ID index over geometries
CREATE INDEX IF NOT EXISTS geometries_source_idx ON geometries ( source_id );

CREATE INDEX IF NOT EXISTS geometries_elstat_idx ON geometries ( elstat_id );
CREATE INDEX IF NOT EXISTS geometries_osm_idx ON geometries ( osm_id );

-- Index over building geometry_id (expect to look up building by geometry_id for map tiles)
CREATE INDEX IF NOT EXISTS building_geometry_idx ON buildings ( geometry_id );
8 changes: 4 additions & 4 deletions migrations/003.index-buildings.up.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- Create building indexes after bulk loading

-- Building index over UPRNs (given a building, find UPRNs)
CREATE INDEX IF NOT EXISTS uprn_building_idx ON building_properties ( building_id );
--CREATE INDEX IF NOT EXISTS uprn_building_idx ON building_properties ( building_id );

-- UPRN index (given a UPRN, find buildings or parents)
CREATE INDEX IF NOT EXISTS uprn_uprn_idx ON building_properties ( uprn );
--CREATE INDEX IF NOT EXISTS uprn_uprn_idx ON building_properties ( uprn );

-- Parent index over UPRNs (given a UPRN, find children)
CREATE INDEX IF NOT EXISTS uprn_parent_idx ON building_properties ( parent_uprn );
--CREATE INDEX IF NOT EXISTS uprn_parent_idx ON building_properties ( parent_uprn );

-- TOID index over buildings
CREATE INDEX IF NOT EXISTS building_toid_idx ON buildings ( ref_toid );
--CREATE INDEX IF NOT EXISTS building_toid_idx ON buildings ( ref_toid );
14 changes: 5 additions & 9 deletions migrations/004.location-date-size-like.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ ALTER TABLE buildings DROP COLUMN IF EXISTS location_longitude;

-- Drop date fields

ALTER TABLE buildings DROP COLUMN IF EXISTS date_year;
ALTER TABLE buildings DROP COLUMN IF EXISTS date_lower;
ALTER TABLE buildings DROP COLUMN IF EXISTS date_upper;
ALTER TABLE buildings DROP COLUMN IF EXISTS year_built;
ALTER TABLE buildings DROP COLUMN IF EXISTS reconstruction_year;
ALTER TABLE buildings DROP COLUMN IF EXISTS date_source;

ALTER TABLE buildings DROP COLUMN IF EXISTS facade_year;
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_upper;
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_lower;
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_source;
ALTER TABLE buildings DROP COLUMN IF EXISTS date_source_detail;

-- Drop size fields
ALTER TABLE buildings DROP COLUMN IF EXISTS size_storeys_attic;
ALTER TABLE buildings DROP COLUMN IF EXISTS size_storeys_core;
ALTER TABLE buildings DROP COLUMN IF EXISTS size_storeys_basement;
ALTER TABLE buildings DROP COLUMN IF EXISTS pilotis;
ALTER TABLE buildings DROP COLUMN IF EXISTS high_ground_floor;

ALTER TABLE buildings DROP COLUMN IF EXISTS size_height_apex;
ALTER TABLE buildings DROP COLUMN IF EXISTS size_floor_area_ground;
Expand Down
Loading

0 comments on commit 7ddd381

Please sign in to comment.