comparison schema/gemma.sql @ 182:4df4e4bf480e

Beautify SQL E.g. indent with 4 spaces more consequently.
author Tom Gottfried <tom@intevation.de>
date Wed, 18 Jul 2018 16:48:27 +0200
parents 4e2451d561b1
children bc7829defa99
comparison
equal deleted inserted replaced
181:e509eccff303 182:4df4e4bf480e
17 $$; 17 $$;
18 18
19 -- Composite type: UN/LOCODE, fairway section, object reference, hectometre. 19 -- Composite type: UN/LOCODE, fairway section, object reference, hectometre.
20 -- See RIS-Index Encoding Guide 20 -- See RIS-Index Encoding Guide
21 CREATE TYPE isrs AS ( 21 CREATE TYPE isrs AS (
22 country_code char(2), -- ISO 3166 country code 22 country_code char(2), -- ISO 3166 country code
23 -- could be validated against countries table. 23 -- could be validated against countries table.
24 locode char(3), -- without the country code: 24 locode char(3), -- without the country code:
25 -- http://www.unece.org/cefact/locode/welcome.html 25 -- http://www.unece.org/cefact/locode/welcome.html
26 fairway_section char(5), 26 fairway_section char(5),
27 object_reference char(5), 27 object_reference char(5),
28 hectometre int -- should be constrained to five digits 28 hectometre int -- should be constrained to five digits
29 ); 29 );
30 30
31 CREATE TYPE isrsrange AS RANGE ( 31 CREATE TYPE isrsrange AS RANGE (
32 subtype = isrs 32 subtype = isrs
33 ); 33 );
34 34
35 -- 35 --
36 -- GEMMA data 36 -- GEMMA data
37 -- 37 --
38 CREATE SCHEMA gemma; 38 CREATE SCHEMA gemma;
45 45
46 -- 46 --
47 -- Auxiliary tables 47 -- Auxiliary tables
48 -- 48 --
49 CREATE TABLE gemma.system_config ( 49 CREATE TABLE gemma.system_config (
50 config_key varchar PRIMARY KEY, 50 config_key varchar PRIMARY KEY,
51 config_val varchar 51 config_val varchar
52 ); 52 );
53 53
54 CREATE TABLE gemma.countries ( 54 CREATE TABLE gemma.countries (
55 country_code char(2) PRIMARY KEY -- ISO 3166 country code 55 country_code char(2) PRIMARY KEY -- ISO 3166 country code
56 -- A smallint PK would require even less disk space i.e. on the FK side. 56 -- A smallint PK would require even less disk space i.e. on the FK side.
57 -- This might be an issue in case cache space becomes a limiting 57 -- This might be an issue in case cache space becomes a limiting
58 -- factor when there are many FKs pointing here. 58 -- factor when there are many FKs pointing here.
59 ); 59 );
60 60
61 CREATE TABLE gemma.responsibility_areas ( 61 CREATE TABLE gemma.responsibility_areas (
62 country char(2) PRIMARY KEY REFERENCES countries, 62 country char(2) PRIMARY KEY REFERENCES countries,
63 area geometry(MULTIPOLYGON, 4326) --XXX: Should be geography (elsewhere too) 63 area geometry(MULTIPOLYGON, 4326)
64 ); 64 --XXX: Should be geography (elsewhere too)
65 );
65 66
66 CREATE TABLE gemma.language_codes ( 67 CREATE TABLE gemma.language_codes (
67 language_code varchar PRIMARY KEY 68 language_code varchar PRIMARY KEY
68 ); 69 );
69 70
70 CREATE TABLE gemma.user_profiles ( 71 CREATE TABLE gemma.user_profiles (
71 username varchar PRIMARY KEY, -- TODO: check it's in pg_roles by trigger 72 username varchar PRIMARY KEY, -- TODO: check it's in pg_roles by trigger
72 country char(2) NOT NULL REFERENCES countries, 73 country char(2) NOT NULL REFERENCES countries,
73 language_code varchar REFERENCES language_codes, 74 language_code varchar REFERENCES language_codes,
74 map_extent box2d, 75 map_extent box2d,
75 email_adress varchar NOT NULL UNIQUE 76 email_adress varchar NOT NULL UNIQUE
76 ); 77 );
77 78
78 CREATE TABLE gemma.templates ( 79 CREATE TABLE gemma.templates (
79 template_name varchar PRIMARY KEY, 80 template_name varchar PRIMARY KEY,
80 template_data bytea NOT NULL, 81 template_data bytea NOT NULL,
81 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP 82 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
82 ); 83 );
83 CREATE TRIGGER templates_date_info BEFORE UPDATE ON templates 84 CREATE TRIGGER templates_date_info BEFORE UPDATE ON templates
84 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 85 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
85 86
86 CREATE TABLE gemma.user_templates ( 87 CREATE TABLE gemma.user_templates (
87 username varchar NOT NULL REFERENCES user_profiles ON DELETE CASCADE, 88 username varchar NOT NULL REFERENCES user_profiles ON DELETE CASCADE,
88 template_name varchar NOT NULL REFERENCES templates ON DELETE CASCADE, 89 template_name varchar NOT NULL REFERENCES templates ON DELETE CASCADE,
89 PRIMARY KEY (username, template_name) 90 PRIMARY KEY (username, template_name)
90 ); 91 );
91 92
92 -- 93 --
93 -- General river information 94 -- General river information
94 -- 95 --
95 96
104 -- -- composites of string values or should we use inter PKs? 105 -- -- composites of string values or should we use inter PKs?
105 -- -- => In case the index is used and cache space becomes a limiting 106 -- -- => In case the index is used and cache space becomes a limiting
106 -- -- factor, this might be an issue. 107 -- -- factor, this might be an issue.
107 -- rwdr double precision NOT NULL, 108 -- rwdr double precision NOT NULL,
108 -- EXCLUDE USING GIST (stretch WITH &&) 109 -- EXCLUDE USING GIST (stretch WITH &&)
109 -- ); 110 --);
110 111
111 CREATE TABLE gemma_waterway.catccls ( 112 CREATE TABLE gemma_waterway.catccls (
112 catccl smallint PRIMARY KEY 113 catccl smallint PRIMARY KEY
113 -- TODO: Do we need name and/or definition from IENC feature catalogue? 114 -- TODO: Do we need name and/or definition from IENC feature catalogue?
114 ); 115 );
115 116
116 CREATE TABLE gemma_waterway.dirimps ( 117 CREATE TABLE gemma_waterway.dirimps (
117 dirimp smallint PRIMARY KEY 118 dirimp smallint PRIMARY KEY
118 -- TODO: Do we need name and/or definition from IENC feature catalogue? 119 -- TODO: Do we need name and/or definition from IENC feature catalogue?
119 ); 120 );
120 121
121 CREATE TABLE gemma_waterway.waterway_area ( 122 CREATE TABLE gemma_waterway.waterway_area (
122 area geometry(POLYGON, 4326) PRIMARY KEY, 123 area geometry(POLYGON, 4326) PRIMARY KEY,
123 catccl smallint REFERENCES catccls, 124 catccl smallint REFERENCES catccls,
124 dirimp smallint REFERENCES dirimps 125 dirimp smallint REFERENCES dirimps
125 ); 126 );
126 127
127 CREATE TABLE gemma_fairway.depth_references ( 128 CREATE TABLE gemma_fairway.depth_references (
128 depth_reference varchar(4) PRIMARY KEY 129 depth_reference varchar(4) PRIMARY KEY
129 -- See col. AB and AI RIS-Index Encoding Guide 130 -- See col. AB and AI RIS-Index Encoding Guide
130 -- XXX: We need a way to distinguish between geodetic (eg. col. AP 131 -- XXX: We need a way to distinguish between geodetic (eg. col. AP
131 -- RIS-Index) and other references (e.g. col. AB and AI): 132 -- RIS-Index) and other references (e.g. col. AB and AI):
132 -- _ multi-column FK with a boolean column (geodetic/non-geodetic; 133 -- _ multi-column FK with a boolean column (geodetic/non-geodetic;
133 -- i.e. absolut/not absolut) and DEFAULTs and CHECKs at the FK side. 134 -- i.e. absolut/not absolut) and DEFAULTs and CHECKs at the FK side.
134 -- _ Do not mixup things with different meanings in one table at all 135 -- _ Do not mixup things with different meanings in one table at all
135 -- (which would mean a model differing a bit from RIS-Index ideas) 136 -- (which would mean a model differing a bit from RIS-Index ideas)
136 ); 137 );
137 138
138 CREATE TABLE gemma_fairway.reference_water_levels ( 139 CREATE TABLE gemma_fairway.reference_water_levels (
139 reference_water_level varchar(20) PRIMARY KEY 140 reference_water_level varchar(20) PRIMARY KEY
140 ); 141 );
141 142
142 CREATE TABLE gemma_fairway.gauges ( 143 CREATE TABLE gemma_fairway.gauges (
143 location isrs PRIMARY KEY, 144 location isrs PRIMARY KEY,
144 function_code varchar(10) NOT NULL, -- XXX: What is this really for? 145 function_code varchar(10) NOT NULL, -- XXX: What is this really for?
145 objname varchar NOT NULL, 146 objname varchar NOT NULL,
146 is_left boolean, -- XXX: Or reference position_codes? 147 is_left boolean, -- XXX: Or reference position_codes?
147 geom geometry(POINT, 4326) NOT NULL, 148 geom geometry(POINT, 4326) NOT NULL,
148 applicability isrsrange, 149 applicability isrsrange,
149 validity tstzrange, -- XXX: Should ranges be NOT NULL? In DRC, only copy 150 validity tstzrange, -- XXX: Should ranges be NOT NULL? In DRC, only copy
150 -- pasted text from a more general specification is given 151 -- pasted text from a more general specification is given
151 -- (a gauge is not a berth!) 152 -- (a gauge is not a berth!)
152 -- TODO: Ranges need a joint exclusion constaint to prevent overlaps? 153 -- TODO: Ranges need a joint exclusion constaint to prevent overlaps?
153 zero_point double precision NOT NULL, 154 zero_point double precision NOT NULL,
154 geodref varchar(4) REFERENCES depth_references, 155 geodref varchar(4) REFERENCES depth_references,
155 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 156 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
156 source_organization varchar NOT NULL 157 source_organization varchar NOT NULL
157 ); 158 );
158 CREATE TRIGGER gauges_date_info BEFORE UPDATE ON gauges 159 CREATE TRIGGER gauges_date_info BEFORE UPDATE ON gauges
159 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 160 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
160 161
161 CREATE TABLE gemma_fairway.gauges_reference_water_levels ( 162 CREATE TABLE gemma_fairway.gauges_reference_water_levels (
162 gauge_id isrs NOT NULL REFERENCES gauges, 163 gauge_id isrs NOT NULL REFERENCES gauges,
163 reference_water_level varchar(20) 164 reference_water_level varchar(20)
164 NOT NULL REFERENCES reference_water_levels, 165 NOT NULL REFERENCES reference_water_levels,
165 PRIMARY KEY (gauge_id, reference_water_level), 166 PRIMARY KEY (gauge_id, reference_water_level),
166 value int NOT NULL 167 value int NOT NULL
167 ); 168 );
168 169
169 CREATE TABLE gemma_fairway.gauge_measurements ( 170 CREATE TABLE gemma_fairway.gauge_measurements (
170 fk_gauge_id isrs NOT NULL REFERENCES gauges, 171 fk_gauge_id isrs NOT NULL REFERENCES gauges,
171 measure_date timestamp with time zone NOT NULL, 172 measure_date timestamp with time zone NOT NULL,
172 PRIMARY KEY (fk_gauge_id, measure_date), 173 PRIMARY KEY (fk_gauge_id, measure_date),
173 -- XXX: Is country_code really relevant for GEMMA or just NtS? 174 -- XXX: Is country_code really relevant for GEMMA or just NtS?
174 -- country_code char(2) NOT NULL REFERENCES countries, 175 -- country_code char(2) NOT NULL REFERENCES countries,
175 -- TODO: add relations to stuff provided as enumerations 176 -- TODO: add relations to stuff provided as enumerations
176 sender varchar NOT NULL, -- "from" attribute from DRC 177 sender varchar NOT NULL, -- "from" attribute from DRC
177 language_code varchar NOT NULL REFERENCES language_codes, 178 language_code varchar NOT NULL REFERENCES language_codes,
178 date_issue timestamp with time zone NOT NULL, 179 date_issue timestamp with time zone NOT NULL,
179 -- reference_code varchar(4) NOT NULL REFERENCES depth_references, 180 -- reference_code varchar(4) NOT NULL REFERENCES depth_references,
180 -- XXX: Always ZPG? 181 -- XXX: Always ZPG?
181 water_level double precision NOT NULL, 182 water_level double precision NOT NULL,
182 predicted boolean NOT NULL, 183 predicted boolean NOT NULL,
183 is_waterlevel boolean NOT NULL, 184 is_waterlevel boolean NOT NULL,
184 -- XXX: "measure_code" if really only W or Q 185 -- XXX: "measure_code" if really only W or Q
185 -- XXX: Do we need "unit" attribute or can we normalise on import? 186 -- XXX: Do we need "unit" attribute or can we normalise on import?
186 value_min double precision, -- XXX: NOT NULL if predicted? 187 value_min double precision, -- XXX: NOT NULL if predicted?
187 value_max double precision, -- XXX: NOT NULL if predicted? 188 value_max double precision, -- XXX: NOT NULL if predicted?
188 --- TODO: Add a double range type for checking? 189 --- TODO: Add a double range type for checking?
189 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 190 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
190 source_organization varchar NOT NULL -- "originator" 191 source_organization varchar NOT NULL -- "originator"
191 ); 192 );
192 CREATE TRIGGER gauge_measurements_date_info BEFORE UPDATE ON gauge_measurements 193 CREATE TRIGGER gauge_measurements_date_info BEFORE UPDATE ON gauge_measurements
193 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 194 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
194 195
195 CREATE TABLE gemma_waterway.waterway_axis ( 196 CREATE TABLE gemma_waterway.waterway_axis (
196 wtwaxs geometry(LINESTRING, 4326) PRIMARY KEY, 197 wtwaxs geometry(LINESTRING, 4326) PRIMARY KEY,
197 -- TODO: Do we need to check data set quality as described in DRC 2.1.6? 198 -- TODO: Do we need to check data set quality as described in DRC 2.1.6?
198 objnam varchar NOT NULL, 199 objnam varchar NOT NULL,
199 nobjnam varchar 200 nobjnam varchar
200 ); 201 );
201 202
202 CREATE TABLE gemma_waterway.distance_mark_functions ( 203 CREATE TABLE gemma_waterway.distance_mark_functions (
203 -- XXX: Redundant information to object code in isrs code of dist. mark 204 -- XXX: Redundant information to object code in isrs code of dist. mark
204 distance_mark_function varchar(8) PRIMARY KEY 205 distance_mark_function varchar(8) PRIMARY KEY
205 ); 206 );
206 207
207 CREATE TABLE gemma_waterway.position_codes ( 208 CREATE TABLE gemma_waterway.position_codes (
208 position_code char(2) PRIMARY KEY 209 position_code char(2) PRIMARY KEY
209 -- Use smallint because of fairway availability provided on daily basis? 210 -- Use smallint because of fairway availability provided on daily basis?
210 -- Data come from 2015_06_23_RIS_Index_template_v2p0.xlsx, 211 -- Data come from 2015_06_23_RIS_Index_template_v2p0.xlsx,
211 -- sheet "Position_code" or RIS-Index encoding guide? 212 -- sheet "Position_code" or RIS-Index encoding guide?
212 -- XXX: DRC 2.1.7 and 2.2.5 _seem_ to reference the same thing here. 213 -- XXX: DRC 2.1.7 and 2.2.5 _seem_ to reference the same thing here.
213 -- Clarify! 214 -- Clarify!
214 -- TODO: Do we need an attribute "meaning" or so? 215 -- TODO: Do we need an attribute "meaning" or so?
215 ); 216 );
216 217
217 -- This table allows linkage between the 1D ISRS location codes and 2D space 218 -- This table allows linkage between the 1D ISRS location codes and 2D space
218 -- e.g. for cutting bottleneck area out of waterway area based on virtual 219 -- e.g. for cutting bottleneck area out of waterway area based on virtual
219 -- distance marks along waterway axis (see SUC7). 220 -- distance marks along waterway axis (see SUC7).
220 CREATE TABLE gemma_waterway.distance_marks ( 221 CREATE TABLE gemma_waterway.distance_marks (
221 location_code isrs PRIMARY KEY, 222 location_code isrs PRIMARY KEY,
222 geom geometry(POINT, 4326) NOT NULL, 223 geom geometry(POINT, 4326) NOT NULL,
223 distance_mark_function varchar(8) 224 distance_mark_function varchar(8)
224 NOT NULL REFERENCES distance_mark_functions, 225 NOT NULL REFERENCES distance_mark_functions,
225 -- XXX: attribute "function" in DRC 2.1.7 as well as CATDIS seem 226 -- XXX: attribute "function" in DRC 2.1.7 as well as CATDIS seem
226 -- to encode the same thing as the object code in ISRS location code. 227 -- to encode the same thing as the object code in ISRS location code.
227 position_code char(2) NOT NULL REFERENCES position_codes 228 position_code char(2) NOT NULL REFERENCES position_codes
228 ); 229 );
229 230
230 CREATE TABLE gemma_waterway.sections_stretches ( 231 CREATE TABLE gemma_waterway.sections_stretches (
231 id varchar PRIMARY KEY, 232 id varchar PRIMARY KEY,
232 is_section boolean NOT NULL, -- maps 'function' from interface 233 is_section boolean NOT NULL, -- maps 'function' from interface
233 stretch isrsrange, 234 stretch isrsrange,
234 objnam varchar NOT NULL, 235 objnam varchar NOT NULL,
235 nobjnam varchar, 236 nobjnam varchar,
236 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 237 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
237 source_organization varchar NOT NULL 238 source_organization varchar NOT NULL
238 ); 239 );
239 CREATE TRIGGER sections_stretches_date_info BEFORE UPDATE ON sections_stretches 240 CREATE TRIGGER sections_stretches_date_info BEFORE UPDATE ON sections_stretches
240 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 241 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
241 242
242 CREATE TABLE gemma_waterway.waterway_profiles ( 243 CREATE TABLE gemma_waterway.waterway_profiles (
243 location isrs NOT NULL, 244 location isrs NOT NULL,
244 validity tstzrange, 245 validity tstzrange,
245 EXCLUDE USING GIST (validity WITH &&), 246 EXCLUDE USING GIST (validity WITH &&),
246 PRIMARY KEY (location, validity), 247 PRIMARY KEY (location, validity),
247 lnwl smallint, 248 lnwl smallint,
248 mwl smallint, 249 mwl smallint,
249 hnwl smallint, 250 hnwl smallint,
250 fe30 smallint, 251 fe30 smallint,
251 fe100 smallint, 252 fe100 smallint,
252 -- XXX: further normalise using reference_water_levels? 253 -- XXX: further normalise using reference_water_levels?
253 CHECK(COALESCE(lnwl, mwl, hnwl, fe30, fe100) IS NULL 254 CHECK(COALESCE(lnwl, mwl, hnwl, fe30, fe100) IS NULL
254 OR validity IS NOT NULL), 255 OR validity IS NOT NULL),
255 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 256 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
256 source_organization varchar NOT NULL 257 source_organization varchar NOT NULL
257 ); 258 );
258 CREATE TRIGGER waterway_profiles_date_info BEFORE UPDATE ON waterway_profiles 259 CREATE TRIGGER waterway_profiles_date_info BEFORE UPDATE ON waterway_profiles
259 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 260 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
260 261
261 CREATE TABLE gemma_waterway.levels_of_service ( 262 CREATE TABLE gemma_waterway.levels_of_service (
262 level_of_service smallint PRIMARY KEY 263 level_of_service smallint PRIMARY KEY
263 ); 264 );
264 265
265 CREATE TABLE gemma_waterway.fairway_dimensions ( 266 CREATE TABLE gemma_waterway.fairway_dimensions (
266 area geometry(POLYGON, 4326) PRIMARY KEY, 267 area geometry(POLYGON, 4326) PRIMARY KEY,
267 level_of_service smallint NOT NULL REFERENCES levels_of_service, 268 level_of_service smallint NOT NULL REFERENCES levels_of_service,
268 min_width smallint NOT NULL, 269 min_width smallint NOT NULL,
269 max_width smallint NOT NULL, 270 max_width smallint NOT NULL,
270 min_depth smallint NOT NULL, 271 min_depth smallint NOT NULL,
271 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 272 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
272 source_organization varchar NOT NULL 273 source_organization varchar NOT NULL
273 ); 274 );
274 CREATE TRIGGER fairway_dimensions_date_info BEFORE UPDATE ON fairway_dimensions 275 CREATE TRIGGER fairway_dimensions_date_info BEFORE UPDATE ON fairway_dimensions
275 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 276 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
276 277
277 -- 278 --
278 -- Bottlenecks 279 -- Bottlenecks
279 -- 280 --
280 CREATE TABLE gemma_fairway.riverbed_materials ( 281 CREATE TABLE gemma_fairway.riverbed_materials (
281 material varchar PRIMARY KEY 282 material varchar PRIMARY KEY
282 -- XXX: Should this table contain choices from DRC 2.2.3 or 283 -- XXX: Should this table contain choices from DRC 2.2.3 or
283 -- from IENC Encoding Guide M.4.3, attribute NATSUR? 284 -- from IENC Encoding Guide M.4.3, attribute NATSUR?
284 ); 285 );
285 286
286 CREATE TABLE gemma_fairway.survey_types ( 287 CREATE TABLE gemma_fairway.survey_types (
287 survey_type varchar PRIMARY KEY 288 survey_type varchar PRIMARY KEY
288 ); 289 );
289 290
290 CREATE TABLE gemma_fairway.coverage_types ( 291 CREATE TABLE gemma_fairway.coverage_types (
291 coverage_type varchar PRIMARY KEY 292 coverage_type varchar PRIMARY KEY
292 ); 293 );
293 294
294 CREATE TABLE gemma_fairway.limiting_factors ( 295 CREATE TABLE gemma_fairway.limiting_factors (
295 limiting_factor varchar PRIMARY KEY 296 limiting_factor varchar PRIMARY KEY
296 ); 297 );
297 298
298 -- XXX: Nullability differs between DRC (attributes marked "O") and WSDL 299 -- XXX: Nullability differs between DRC (attributes marked "O") and WSDL
299 -- (minOccurs=0; nillable seems to be set arbitrarily as even bottleneck_id and 300 -- (minOccurs=0; nillable seems to be set arbitrarily as even bottleneck_id and
300 -- fk_g_fid (both mandatory, i.e. marked "M" in DRC) have nillable="true" in WSDL) 301 -- fk_g_fid (both mandatory, i.e. marked "M" in DRC) have nillable="true" in WSDL)
301 CREATE TABLE gemma_fairway.bottlenecks ( 302 CREATE TABLE gemma_fairway.bottlenecks (
302 bottleneck_id varchar PRIMARY KEY, 303 bottleneck_id varchar PRIMARY KEY,
303 fk_g_fid isrs NOT NULL REFERENCES gauges, 304 fk_g_fid isrs NOT NULL REFERENCES gauges,
304 -- XXX: DRC references "ch. 3.1.1", which does not exist in document. 305 -- XXX: DRC references "ch. 3.1.1", which does not exist in document.
305 objnam varchar, 306 objnam varchar,
306 nobjnm varchar, 307 nobjnm varchar,
307 stretch isrsrange NOT NULL, 308 stretch isrsrange NOT NULL,
308 area geometry(POLYGON, 4326) NOT NULL, 309 area geometry(POLYGON, 4326) NOT NULL,
309 rb char(2) NOT NULL REFERENCES countries, -- from rb_lb in interface 310 rb char(2) NOT NULL REFERENCES countries, -- from rb_lb in interface
310 lb char(2) NOT NULL REFERENCES countries, -- from rb_lb in interface 311 lb char(2) NOT NULL REFERENCES countries, -- from rb_lb in interface
311 responsible_country char(2) NOT NULL REFERENCES countries, 312 responsible_country char(2) NOT NULL REFERENCES countries,
312 revisiting_time smallint NOT NULL, 313 revisiting_time smallint NOT NULL,
313 limiting varchar NOT NULL REFERENCES limiting_factors, 314 limiting varchar NOT NULL REFERENCES limiting_factors,
314 -- surtyp varchar NOT NULL REFERENCES survey_types, 315 -- surtyp varchar NOT NULL REFERENCES survey_types,
315 -- XXX: Also an attribut of sounding result? 316 -- XXX: Also an attribut of sounding result?
316 -- coverage varchar REFERENCES coverage_types, 317 -- coverage varchar REFERENCES coverage_types,
317 -- XXX: Also an attribut of sounding result? 318 -- XXX: Also an attribut of sounding result?
318 -- CHECK allowed combinations of surtyp and coverage or 319 -- CHECK allowed combinations of surtyp and coverage or
319 -- different model approach? 320 -- different model approach?
320 -- depth_reference char(3) NOT NULL REFERENCES depth_references, 321 -- depth_reference char(3) NOT NULL REFERENCES depth_references,
321 -- XXX: Also an attribut of sounding result? 322 -- XXX: Also an attribut of sounding result?
322 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 323 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
323 source_organization varchar NOT NULL, 324 source_organization varchar NOT NULL,
324 -- additional_data xml -- Currently not relevant for GEMMA 325 -- additional_data xml -- Currently not relevant for GEMMA
325 staging_done boolean NOT NULL DEFAULT false 326 staging_done boolean NOT NULL DEFAULT false
326 ); 327 );
327 CREATE TRIGGER bottleneck_date_info BEFORE UPDATE ON bottlenecks 328 CREATE TRIGGER bottleneck_date_info BEFORE UPDATE ON bottlenecks
328 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 329 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
329 330
330 CREATE TABLE gemma_fairway.bottlenecks_riverbed_materials ( 331 CREATE TABLE gemma_fairway.bottlenecks_riverbed_materials (
331 bottleneck_id varchar REFERENCES bottlenecks, 332 bottleneck_id varchar REFERENCES bottlenecks,
332 riverbed varchar REFERENCES riverbed_materials, 333 riverbed varchar REFERENCES riverbed_materials,
333 -- XXX: should be 'natsur' according to IENC Encoding Guide M.4.3 334 -- XXX: should be 'natsur' according to IENC Encoding Guide M.4.3
334 PRIMARY KEY (bottleneck_id, riverbed) 335 PRIMARY KEY (bottleneck_id, riverbed)
335 ); 336 );
336 337
337 CREATE TABLE gemma_fairway.sounding_results ( 338 CREATE TABLE gemma_fairway.sounding_results (
338 bottleneck_id varchar NOT NULL REFERENCES bottlenecks, 339 bottleneck_id varchar NOT NULL REFERENCES bottlenecks,
339 date_info date NOT NULL, 340 date_info date NOT NULL,
340 PRIMARY KEY (bottleneck_id, date_info), 341 PRIMARY KEY (bottleneck_id, date_info),
341 area geometry(POLYGON, 4326) NOT NULL, 342 area geometry(POLYGON, 4326) NOT NULL,
342 surtyp varchar NOT NULL REFERENCES survey_types, 343 surtyp varchar NOT NULL REFERENCES survey_types,
343 coverage varchar REFERENCES coverage_types, 344 coverage varchar REFERENCES coverage_types,
344 depth_reference char(3) NOT NULL REFERENCES depth_references, 345 depth_reference char(3) NOT NULL REFERENCES depth_references,
345 sounding_data raster NOT NULL, 346 sounding_data raster NOT NULL,
346 staging_done boolean NOT NULL DEFAULT false 347 staging_done boolean NOT NULL DEFAULT false
347 ); 348 );
348 349
349 -- 350 --
350 -- Fairway availability 351 -- Fairway availability
351 -- 352 --
352 CREATE TABLE gemma_fairway.measure_types ( 353 CREATE TABLE gemma_fairway.measure_types (
353 measure_type varchar PRIMARY KEY 354 measure_type varchar PRIMARY KEY
354 ); 355 );
355 356
356 CREATE TABLE gemma_fairway.fairway_availability ( 357 CREATE TABLE gemma_fairway.fairway_availability (
357 id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, 358 id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
358 position_code char(2) REFERENCES position_codes, 359 position_code char(2) REFERENCES position_codes,
359 bottleneck_id varchar NOT NULL REFERENCES bottlenecks, 360 bottleneck_id varchar NOT NULL REFERENCES bottlenecks,
360 surdat date NOT NULL, 361 surdat date NOT NULL,
361 UNIQUE (bottleneck_id, surdat), 362 UNIQUE (bottleneck_id, surdat),
362 -- additional_data xml -- Currently not relevant for GEMMA 363 -- additional_data xml -- Currently not relevant for GEMMA
363 critical boolean, 364 critical boolean,
364 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, 365 date_info timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
365 source_organization varchar NOT NULL 366 source_organization varchar NOT NULL
366 ); 367 );
367 CREATE TRIGGER fairway_availability_date_info 368 CREATE TRIGGER fairway_availability_date_info
368 BEFORE UPDATE ON fairway_availability 369 BEFORE UPDATE ON fairway_availability
369 FOR EACH ROW EXECUTE PROCEDURE update_date_info(); 370 FOR EACH ROW EXECUTE PROCEDURE update_date_info();
370 371
371 CREATE TABLE gemma_fairway.fa_reference_values ( 372 CREATE TABLE gemma_fairway.fa_reference_values (
372 fairway_availability_id int NOT NULL REFERENCES fairway_availability, 373 fairway_availability_id int NOT NULL REFERENCES fairway_availability,
373 level_of_service smallint NOT NULL REFERENCES levels_of_service, 374 level_of_service smallint NOT NULL REFERENCES levels_of_service,
374 PRIMARY KEY (fairway_availability_id, level_of_service), 375 PRIMARY KEY (fairway_availability_id, level_of_service),
375 fairway_depth smallint, 376 fairway_depth smallint,
376 fairway_width smallint, 377 fairway_width smallint,
377 fairway_radius int, 378 fairway_radius int,
378 CHECK(COALESCE(fairway_depth, fairway_width, fairway_radius) IS NOT NULL), 379 CHECK(COALESCE(fairway_depth, fairway_width, fairway_radius) IS NOT NULL),
379 shallowest_spot geometry(POINT, 4326) 380 shallowest_spot geometry(POINT, 4326)
380 ); 381 );
381 382
382 CREATE TABLE gemma_fairway.bottleneck_pdfs ( 383 CREATE TABLE gemma_fairway.bottleneck_pdfs (
383 fairway_availability_id int NOT NULL REFERENCES fairway_availability, 384 fairway_availability_id int NOT NULL REFERENCES fairway_availability,
384 profile_pdf_filename varchar NOT NULL, -- redundant to last part of URL 385 profile_pdf_filename varchar NOT NULL, -- redundant to last part of URL
385 profile_pdf_url varchar NOT NULL, -- prohibit insecure URLs somehow? 386 profile_pdf_url varchar NOT NULL, -- prohibit insecure URLs somehow?
386 PRIMARY KEY (fairway_availability_id, profile_pdf_url), 387 PRIMARY KEY (fairway_availability_id, profile_pdf_url),
387 pdf_generation_date timestamp with time zone NOT NULL, 388 pdf_generation_date timestamp with time zone NOT NULL,
388 source_organization varchar NOT NULL 389 source_organization varchar NOT NULL
389 ); 390 );
390 391
391 CREATE TABLE gemma_fairway.effective_fairway_availability ( 392 CREATE TABLE gemma_fairway.effective_fairway_availability (
392 fairway_availability_id int NOT NULL REFERENCES fairway_availability, 393 fairway_availability_id int NOT NULL REFERENCES fairway_availability,
393 measure_date timestamp with time zone NOT NULL, 394 measure_date timestamp with time zone NOT NULL,
394 level_of_service smallint NOT NULL REFERENCES levels_of_service, 395 level_of_service smallint NOT NULL REFERENCES levels_of_service,
395 PRIMARY KEY (fairway_availability_id, measure_date, level_of_service), 396 PRIMARY KEY (fairway_availability_id, measure_date, level_of_service),
396 available_depth_value smallint, 397 available_depth_value smallint,
397 available_width_value smallint, 398 available_width_value smallint,
398 water_level_value smallint, 399 water_level_value smallint,
399 CHECK(COALESCE(available_depth_value, available_width_value, 400 CHECK(COALESCE(available_depth_value, available_width_value,
400 water_level_value) IS NOT NULL), 401 water_level_value) IS NOT NULL),
401 measure_type varchar NOT NULL REFERENCES measure_types, 402 measure_type varchar NOT NULL REFERENCES measure_types,
402 source_organization varchar NOT NULL, 403 source_organization varchar NOT NULL,
403 forecast_generation_time timestamp with time zone, 404 forecast_generation_time timestamp with time zone,
404 CHECK(measure_type <> 'forecasted' 405 CHECK(measure_type <> 'forecasted'
405 OR forecast_generation_time IS NOT NULL), 406 OR forecast_generation_time IS NOT NULL),
406 value_lifetime timestamp with time zone, 407 value_lifetime timestamp with time zone,
407 CHECK(measure_type = 'minimum guaranteed' OR value_lifetime IS NOT NULL) 408 CHECK(measure_type = 'minimum guaranteed' OR value_lifetime IS NOT NULL)
408 ); 409 );
409 410
410 COMMIT; 411 COMMIT;