-
Notifications
You must be signed in to change notification settings - Fork 6
/
schema.sql
576 lines (401 loc) · 15.3 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
--
-- Name: postgis; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
--
-- Name: EXTENSION postgis; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions';
SET search_path = public, pg_catalog;
--
-- Name: update_daily_count_bucket(date, integer, character varying, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION update_daily_count_bucket(day date, w integer, sc character varying, change integer) RETURNS integer
LANGUAGE plpgsql
AS $$
BEGIN
<<insert_update>>
LOOP
UPDATE daily_counts
SET total = total + change
WHERE daily_counts.ward = w
AND daily_counts.requested_date = day
AND daily_counts.service_code = sc;
EXIT insert_update WHEN found;
BEGIN
INSERT INTO daily_counts ( requested_date, service_code, total, ward)
VALUES ( day, sc, change, w);
EXCEPTION WHEN not_null_violation THEN
-- ignore
END;
EXIT insert_update;
END LOOP insert_update;
RETURN NULL;
END;
$$;
--
-- Name: update_daily_count_bucket(character varying, date, integer, character varying, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION update_daily_count_bucket(tbl character varying, day date, w integer, sc character varying, change integer) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
updated integer;
BEGIN
<<insert_update>>
LOOP
EXECUTE 'UPDATE ' || tbl || ' SET total = total + $1 WHERE ward = $2 AND requested_date = $3 AND service_code = $4;' USING change, w, day, sc;
GET DIAGNOSTICS updated = ROW_COUNT;
EXIT insert_update WHEN updated != 0;
BEGIN
IF (change > 0) THEN
EXECUTE 'INSERT INTO ' || tbl || ' (requested_date, service_code, total, ward) VALUES ($1, $2, $3, $4);' USING day, sc, change, w;
END IF;
EXCEPTION WHEN not_null_violation THEN
-- ignore
END;
EXIT insert_update;
END LOOP insert_update;
RETURN NULL;
END;
$_$;
--
-- Name: update_daily_counts(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION update_daily_counts() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- mostly cribbed from http://www.postgresql.org/docs/9.2/static/plpgsql-trigger.html
DECLARE
change integer;
day_to_update DATE;
foo integer; -- throwaway
BEGIN
IF (TG_OP = 'DELETE' AND OLD.duplicate IS NULL) THEN
-- DECREMENT
-- FIXME: this codepath is never traversed -- we never delete!
foo = update_daily_count_bucket('daily_counts', DATE(OLD.requested_datetime), OLD.ward, OLD.service_code, -1 );
IF (OLD.closed_datetime IS NOT NULL) THEN
foo = update_daily_count_bucket('daily_closed_counts', DATE(OLD.closed_datetime), OLD.ward, OLD.service_code, -1 );
END IF;
ELSIF (TG_OP = 'UPDATE') THEN
-- if the OLD record was not a duplicate, decrement the day is was marked as opened
-- if the NEW record is not a duplicate, increment the day is is marked as opened
-- this means that if a record changes state to or from duplicate, we maintain
-- sane counts for that request. In most cases, it'll come to us as a duplicate
-- and stay a duplicate, rendering this all moot.
IF (OLD.duplicate IS NULL) THEN
day_to_update = DATE(OLD.requested_datetime);
foo = update_daily_count_bucket( 'daily_counts', day_to_update, OLD.ward, OLD.service_code, -1 );
IF (OLD.closed_datetime IS NOT NULL) THEN
foo = update_daily_count_bucket('daily_closed_counts', DATE(OLD.closed_datetime), OLD.ward, OLD.service_code, -1 );
END IF;
END IF;
IF (NEW.duplicate IS NULL) THEN
day_to_update = DATE(NEW.requested_datetime);
foo = update_daily_count_bucket( 'daily_counts', day_to_update, NEW.ward, NEW.service_code, 1 );
IF (NEW.closed_datetime IS NOT NULL) THEN
foo = update_daily_count_bucket('daily_closed_counts', DATE(NEW.closed_datetime), NEW.ward, NEW.service_code, 1 );
END IF;
END IF;
ELSIF (TG_OP = 'INSERT' AND NEW.duplicate IS NULL) THEN
foo = update_daily_count_bucket('daily_counts', DATE(NEW.requested_datetime), NEW.ward, NEW.service_code, 1 );
IF (NEW.closed_datetime IS NOT NULL) THEN
foo = update_daily_count_bucket('daily_closed_counts', DATE(NEW.closed_datetime), NEW.ward, NEW.service_code, 1 );
END IF;
END IF;
RETURN NULL;
END;
$$;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: daily_closed_counts; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE daily_closed_counts (
requested_date date NOT NULL,
service_code character varying(255) NOT NULL,
total integer DEFAULT 0 NOT NULL,
ward integer NOT NULL
);
--
-- Name: daily_counts; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE daily_counts (
requested_date date NOT NULL,
service_code character varying(255) NOT NULL,
total integer DEFAULT 0 NOT NULL,
ward integer NOT NULL
);
--
-- Name: schema_info; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE schema_info (
version character varying(12)
);
--
-- Name: service_requests; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE service_requests (
id integer NOT NULL,
service_request_id character varying(12),
status character varying(12),
service_name character varying(255),
service_code character varying(255),
agency_responsible character varying(255),
address character varying(255),
requested_datetime timestamp with time zone,
updated_datetime timestamp with time zone,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
lat double precision,
long double precision,
media_url character varying(255),
police_district integer,
ward integer,
channel character varying(255),
notes text,
duplicate character varying(40),
parent_service_request_id character varying(40),
closed_datetime timestamp with time zone,
ward_2015 integer,
transition_area_id integer
);
--
-- Name: service_requests_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE service_requests_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: service_requests_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE service_requests_id_seq OWNED BY service_requests.id;
--
-- Name: transition_areas; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE transition_areas (
id integer NOT NULL,
boundary geometry(MultiPolygon),
ward_2013 integer,
ward_2015 integer
);
--
-- Name: transition_areas_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE transition_areas_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: transition_areas_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE transition_areas_id_seq OWNED BY transition_areas.id;
--
-- Name: ward_boundaries_2013; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE ward_boundaries_2013 (
ward integer,
boundary geometry(MultiPolygon)
);
--
-- Name: ward_boundaries_2015; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE ward_boundaries_2015 (
ward integer,
boundary geometry(MultiPolygon)
);
--
-- Name: weather_daily_stats; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE weather_daily_stats (
weather_date date NOT NULL,
high_temp_f double precision,
low_temp_f double precision,
precip_in double precision
);
--
-- Name: COLUMN weather_daily_stats.weather_date; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_daily_stats.weather_date IS 'date for this weather data';
--
-- Name: COLUMN weather_daily_stats.high_temp_f; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_daily_stats.high_temp_f IS 'day''s high temperature, in farenheit';
--
-- Name: COLUMN weather_daily_stats.low_temp_f; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_daily_stats.low_temp_f IS 'day''s low temperature, in farenheit';
--
-- Name: COLUMN weather_daily_stats.precip_in; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_daily_stats.precip_in IS 'day''s precipitation, in inches';
--
-- Name: weather_storm_event_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE weather_storm_event_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: weather_storm_event; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE weather_storm_event (
id integer DEFAULT nextval('weather_storm_event_id_seq'::regclass) NOT NULL,
event_date date NOT NULL,
event_id integer NOT NULL,
event_type character varying(255) NOT NULL
);
--
-- Name: TABLE weather_storm_event; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE weather_storm_event IS 'select data drawn from the NOAA NCDC Storm Events database';
--
-- Name: COLUMN weather_storm_event.id; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_storm_event.id IS 'internal, database-only ID for this event';
--
-- Name: COLUMN weather_storm_event.event_date; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_storm_event.event_date IS 'start date of the event; based on the source data''s "begin_date" field';
--
-- Name: COLUMN weather_storm_event.event_id; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_storm_event.event_id IS 'event ID; based on the source data''s "event_id" field';
--
-- Name: COLUMN weather_storm_event.event_type; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN weather_storm_event.event_type IS 'type of event; based on the source data''s "event_type" field';
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY service_requests ALTER COLUMN id SET DEFAULT nextval('service_requests_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY transition_areas ALTER COLUMN id SET DEFAULT nextval('transition_areas_id_seq'::regclass);
--
-- Name: service_requests_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY service_requests
ADD CONSTRAINT service_requests_pkey PRIMARY KEY (id);
--
-- Name: sr_number_uniq; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY service_requests
ADD CONSTRAINT sr_number_uniq UNIQUE (service_request_id);
--
-- Name: dc_request_date; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX dc_request_date ON daily_counts USING btree (requested_date);
--
-- Name: dc_service_code; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX dc_service_code ON daily_counts USING btree (service_code);
--
-- Name: dc_ward; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX dc_ward ON daily_counts USING btree (ward);
--
-- Name: sr_closed_code; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX sr_closed_code ON service_requests USING btree (closed_datetime, service_code);
--
-- Name: sr_requested_code; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX sr_requested_code ON service_requests USING btree (requested_datetime, service_code);
--
-- Name: sr_requested_datetime; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX sr_requested_datetime ON service_requests USING btree (requested_datetime);
--
-- Name: sr_updated_datetime; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX sr_updated_datetime ON service_requests USING btree (updated_datetime);
--
-- Name: transition_areas_boundary_gist; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX transition_areas_boundary_gist ON transition_areas USING gist (boundary);
--
-- Name: ward_boundaries_2013_boundary_gist; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX ward_boundaries_2013_boundary_gist ON ward_boundaries_2013 USING gist (boundary);
--
-- Name: ward_boundaries_2015_boundary_gist; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX ward_boundaries_2015_boundary_gist ON ward_boundaries_2015 USING gist (boundary);
--
-- Name: geometry_columns_delete; Type: RULE; Schema: public; Owner: -
--
CREATE RULE geometry_columns_delete AS ON DELETE TO geometry_columns DO INSTEAD NOTHING;
--
-- Name: geometry_columns_insert; Type: RULE; Schema: public; Owner: -
--
CREATE RULE geometry_columns_insert AS ON INSERT TO geometry_columns DO INSTEAD NOTHING;
--
-- Name: geometry_columns_update; Type: RULE; Schema: public; Owner: -
--
CREATE RULE geometry_columns_update AS ON UPDATE TO geometry_columns DO INSTEAD NOTHING;
--
-- Name: update_daily_counts; Type: TRIGGER; Schema: public; Owner: -
--
CREATE TRIGGER update_daily_counts AFTER INSERT OR DELETE OR UPDATE ON service_requests FOR EACH ROW EXECUTE PROCEDURE update_daily_counts();
--
-- PostgreSQL database dump complete
--
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
--
-- Data for Name: schema_info; Type: TABLE DATA; Schema: public; Owner: cgansen
--
INSERT INTO schema_info VALUES ('201306061151');
INSERT INTO schema_info VALUES ('201306071651');
INSERT INTO schema_info VALUES ('201306071725');
INSERT INTO schema_info VALUES ('201306091221');
INSERT INTO schema_info VALUES ('201306161511');
INSERT INTO schema_info VALUES ('201306241712');
INSERT INTO schema_info VALUES ('201306251155');
INSERT INTO schema_info VALUES ('201306271128');
INSERT INTO schema_info VALUES ('201306271346');
INSERT INTO schema_info VALUES ('201307081428');
INSERT INTO schema_info VALUES ('201308211341');
INSERT INTO schema_info VALUES ('201307091601');
INSERT INTO schema_info VALUES ('201308271926');
INSERT INTO schema_info VALUES ('201308201558');
INSERT INTO schema_info VALUES ('201308211328');
INSERT INTO schema_info VALUES ('201308211659');
INSERT INTO schema_info VALUES ('201308281656');
INSERT INTO schema_info VALUES ('201309041806');
--
-- PostgreSQL database dump complete
--