Skip to content

Commit 7ddd381

Browse files
committed
Migrations update
1 parent 4d59caf commit 7ddd381

29 files changed

Lines changed: 794 additions & 1401 deletions

migrations/001.core.up.sql

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
CREATE TABLE IF NOT EXISTS geometries (
77
-- internal unique id
88
geometry_id serial PRIMARY KEY,
9-
-- cross-reference to data source id
10-
source_id varchar(30),
9+
-- replaced source id with ELSTAT and OSM ids
10+
elstat_id varchar(30),
11+
osm_id varchar(30),
1112
-- geometry as EPSG:3857 avoiding reprojection for tiles
1213
geometry_geom geometry(GEOMETRY, 3857)
1314
);
@@ -21,13 +22,19 @@ CREATE TABLE IF NOT EXISTS buildings (
2122
-- internal unique id
2223
building_id serial PRIMARY KEY,
2324
-- OS MasterMap topo id
24-
ref_toid varchar,
25+
--ref_toid varchar,
26+
--replaced by Hellenic Statistics building ID
27+
ref_elstat_id varchar,
2528
-- OSM reference id
26-
ref_osm_id bigint,
29+
ref_osm_id varchar,
2730
-- reference to geometry, aiming to decouple from geometry provider
2831
geometry_id integer REFERENCES geometries
2932
);
30-
ALTER TABLE buildings ADD CONSTRAINT buildings_ref_toid_len CHECK (length(ref_toid) < 90);
33+
34+
ALTER TABLE
35+
buildings
36+
ADD
37+
CONSTRAINT buildings_ref_elstat_id CHECK (length(ref_elstat_id) < 90);
3138

3239
--
3340
-- Properties table
@@ -38,15 +45,15 @@ CREATE TABLE IF NOT EXISTS building_properties (
3845
-- internal primary key
3946
building_property_id serial PRIMARY KEY,
4047
-- UPRN
41-
uprn bigint,
48+
--uprn bigint,
4249
-- Parent should reference UPRN, but assume dataset may be (initially) incomplete
43-
parent_uprn bigint,
50+
--parent_uprn bigint,
4451
-- Building ID may be null for failed matches
45-
building_id integer REFERENCES buildings,
52+
building_id integer REFERENCES buildings
4653
-- TOID match provided by AddressBase
47-
toid varchar,
54+
--toid varchar,
4855
-- Geometry (for verification if loaded, not for public access)
49-
uprn_geom geometry(POINT, 3857)
56+
--uprn_geom geometry(POINT, 3857)
5057
);
5158

5259
--
@@ -58,7 +65,11 @@ CREATE TABLE IF NOT EXISTS user_categories (
5865
-- category name/short description
5966
category varchar
6067
);
61-
INSERT INTO user_categories ( category_id, category ) VALUES ( 1, 'Not provided');
68+
69+
INSERT INTO
70+
user_categories (category_id, category)
71+
VALUES
72+
(1, 'Not provided');
6273

6374
--
6475
-- User access levels
@@ -69,7 +80,11 @@ CREATE TABLE IF NOT EXISTS user_access_levels (
6980
-- name/short description
7081
access_level varchar
7182
);
72-
INSERT INTO user_access_levels ( access_level_id, access_level ) VALUES ( 1, 'untrusted');
83+
84+
INSERT INTO
85+
user_access_levels (access_level_id, access_level)
86+
VALUES
87+
(1, 'untrusted');
7388

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

100-
CREATE INDEX IF NOT EXISTS user_username_idx ON users ( username );
101-
CREATE INDEX IF NOT EXISTS user_email_idx ON users ( email );
112+
ALTER TABLE
113+
users
114+
ADD
115+
CONSTRAINT users_username_len CHECK (length(username) < 30);
116+
117+
ALTER TABLE
118+
users
119+
ADD
120+
CONSTRAINT users_email_len CHECK (length(email) < 50);
121+
122+
ALTER TABLE
123+
users
124+
ADD
125+
CONSTRAINT users_pass_len CHECK (length(pass) <= 60);
126+
127+
CREATE INDEX IF NOT EXISTS user_username_idx ON users (username);
128+
129+
CREATE INDEX IF NOT EXISTS user_email_idx ON users (email);
102130

103131
--
104132
-- User session table
@@ -109,7 +137,7 @@ CREATE TABLE IF NOT EXISTS user_sessions (
109137
expire timestamp(6) NOT NULL
110138
);
111139

112-
CREATE INDEX IF NOT EXISTS user_sessions_expire_idx on user_sessions ( expire );
140+
CREATE INDEX IF NOT EXISTS user_sessions_expire_idx on user_sessions (expire);
113141

114142
--
115143
-- Logs table
@@ -131,8 +159,13 @@ CREATE TABLE IF NOT EXISTS logs (
131159
building_id integer REFERENCES buildings
132160
);
133161

134-
CREATE INDEX IF NOT EXISTS log_timestamp_idx ON logs ( log_timestamp );
135-
CREATE INDEX IF NOT EXISTS log_user_idx ON logs ( user_id );
136-
CREATE INDEX IF NOT EXISTS log_building_idx ON logs ( building_id );
162+
CREATE INDEX IF NOT EXISTS log_timestamp_idx ON logs (log_timestamp);
163+
164+
CREATE INDEX IF NOT EXISTS log_user_idx ON logs (user_id);
165+
166+
CREATE INDEX IF NOT EXISTS log_building_idx ON logs (building_id);
137167

138-
ALTER TABLE buildings ADD COLUMN revision_id bigint REFERENCES logs ( log_id );
168+
ALTER TABLE
169+
buildings
170+
ADD
171+
COLUMN revision_id bigint REFERENCES logs (log_id);

migrations/002.index-geometries.down.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
DROP INDEX IF EXISTS geometries_idx;
55

66
-- Source ID index over geometries
7-
DROP INDEX IF EXISTS geometries_source_idx;
7+
DROP INDEX IF EXISTS geometries_elstat_idx;
8+
DROP INDEX IF EXISTS geometries_osm_idx;
89

910
-- Index over building geometry_id (expect to look up building by geometry_id for map tiles)
1011
DROP INDEX IF EXISTS building_geometry_idx;

migrations/002.index-geometries.up.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
CREATE INDEX IF NOT EXISTS geometries_idx ON geometries USING GIST ( geometry_geom );
55

66
-- Source ID index over geometries
7-
CREATE INDEX IF NOT EXISTS geometries_source_idx ON geometries ( source_id );
7+
8+
CREATE INDEX IF NOT EXISTS geometries_elstat_idx ON geometries ( elstat_id );
9+
CREATE INDEX IF NOT EXISTS geometries_osm_idx ON geometries ( osm_id );
810

911
-- Index over building geometry_id (expect to look up building by geometry_id for map tiles)
1012
CREATE INDEX IF NOT EXISTS building_geometry_idx ON buildings ( geometry_id );
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
-- Create building indexes after bulk loading
22

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

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

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

1212
-- TOID index over buildings
13-
CREATE INDEX IF NOT EXISTS building_toid_idx ON buildings ( ref_toid );
13+
--CREATE INDEX IF NOT EXISTS building_toid_idx ON buildings ( ref_toid );

migrations/004.location-date-size-like.down.sql

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,16 @@ ALTER TABLE buildings DROP COLUMN IF EXISTS location_longitude;
1212

1313
-- Drop date fields
1414

15-
ALTER TABLE buildings DROP COLUMN IF EXISTS date_year;
16-
ALTER TABLE buildings DROP COLUMN IF EXISTS date_lower;
17-
ALTER TABLE buildings DROP COLUMN IF EXISTS date_upper;
15+
ALTER TABLE buildings DROP COLUMN IF EXISTS year_built;
16+
ALTER TABLE buildings DROP COLUMN IF EXISTS reconstruction_year;
1817
ALTER TABLE buildings DROP COLUMN IF EXISTS date_source;
19-
20-
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_year;
21-
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_upper;
22-
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_lower;
23-
ALTER TABLE buildings DROP COLUMN IF EXISTS facade_source;
18+
ALTER TABLE buildings DROP COLUMN IF EXISTS date_source_detail;
2419

2520
-- Drop size fields
26-
ALTER TABLE buildings DROP COLUMN IF EXISTS size_storeys_attic;
2721
ALTER TABLE buildings DROP COLUMN IF EXISTS size_storeys_core;
2822
ALTER TABLE buildings DROP COLUMN IF EXISTS size_storeys_basement;
23+
ALTER TABLE buildings DROP COLUMN IF EXISTS pilotis;
24+
ALTER TABLE buildings DROP COLUMN IF EXISTS high_ground_floor;
2925

3026
ALTER TABLE buildings DROP COLUMN IF EXISTS size_height_apex;
3127
ALTER TABLE buildings DROP COLUMN IF EXISTS size_floor_area_ground;

0 commit comments

Comments
 (0)