This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
536 lines (360 loc) · 12.8 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
--
-- PostgreSQL database dump
--
-- Dumped from database version 11.1
-- Dumped by pg_dump version 11.1
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: event_action_name; Type: TYPE; Schema: public; Owner: growbot
--
CREATE TYPE public.event_action_name AS ENUM (
'PLANT_WATER',
'PLANT_CAPTURE_PHOTO',
'ROBOT_RANDOM_MOVEMENT'
);
ALTER TYPE public.event_action_name OWNER TO growbot;
--
-- Name: growbot_create_state(); Type: FUNCTION; Schema: public; Owner: growbot
--
CREATE FUNCTION public.growbot_create_state() RETURNS trigger
LANGUAGE plpgsql
AS $$begin
insert into public.robot_state(id) values (new.id);
return new;
end;$$;
ALTER FUNCTION public.growbot_create_state() OWNER TO growbot;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: event_actions; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.event_actions (
id integer NOT NULL,
name public.event_action_name NOT NULL,
data jsonb NOT NULL,
plant_id integer,
event_id integer NOT NULL,
robot_id uuid NOT NULL
);
ALTER TABLE public.event_actions OWNER TO growbot;
--
-- Name: event_actions_id_seq; Type: SEQUENCE; Schema: public; Owner: growbot
--
CREATE SEQUENCE public.event_actions_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.event_actions_id_seq OWNER TO growbot;
--
-- Name: event_actions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: growbot
--
ALTER SEQUENCE public.event_actions_id_seq OWNED BY public.event_actions.id;
--
-- Name: events; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.events (
id integer NOT NULL,
summary text NOT NULL,
recurrence text[] DEFAULT ARRAY[]::text[] NOT NULL,
user_id integer NOT NULL,
ephemeral boolean DEFAULT false NOT NULL
);
ALTER TABLE public.events OWNER TO growbot;
--
-- Name: events_id_seq; Type: SEQUENCE; Schema: public; Owner: growbot
--
CREATE SEQUENCE public.events_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.events_id_seq OWNER TO growbot;
--
-- Name: events_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: growbot
--
ALTER SEQUENCE public.events_id_seq OWNED BY public.events.id;
--
-- Name: log; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.log (
id integer NOT NULL,
user_id integer NOT NULL,
type text NOT NULL,
message text NOT NULL,
severity integer NOT NULL,
robot_id uuid,
plant_id integer,
created_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
ALTER TABLE public.log OWNER TO growbot;
--
-- Name: log_id_seq; Type: SEQUENCE; Schema: public; Owner: growbot
--
CREATE SEQUENCE public.log_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.log_id_seq OWNER TO growbot;
--
-- Name: log_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: growbot
--
ALTER SEQUENCE public.log_id_seq OWNED BY public.log.id;
--
-- Name: plant_photos; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.plant_photos (
id integer NOT NULL,
created_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
filename uuid NOT NULL,
plant_id integer NOT NULL
);
ALTER TABLE public.plant_photos OWNER TO growbot;
--
-- Name: plant_photos_id_seq; Type: SEQUENCE; Schema: public; Owner: growbot
--
CREATE SEQUENCE public.plant_photos_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.plant_photos_id_seq OWNER TO growbot;
--
-- Name: plant_photos_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: growbot
--
ALTER SEQUENCE public.plant_photos_id_seq OWNED BY public.plant_photos.id;
--
-- Name: plants; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.plants (
id integer NOT NULL,
user_id integer NOT NULL,
name text NOT NULL,
soil_moisture integer
);
ALTER TABLE public.plants OWNER TO growbot;
--
-- Name: plants_id_seq; Type: SEQUENCE; Schema: public; Owner: growbot
--
CREATE SEQUENCE public.plants_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.plants_id_seq OWNER TO growbot;
--
-- Name: plants_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: growbot
--
ALTER SEQUENCE public.plants_id_seq OWNED BY public.plants.id;
--
-- Name: robot_state; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.robot_state (
id uuid NOT NULL,
battery_level integer DEFAULT 0 NOT NULL,
water_level integer DEFAULT 0 NOT NULL,
standby boolean DEFAULT true NOT NULL,
seen_at timestamp without time zone
);
ALTER TABLE public.robot_state OWNER TO growbot;
--
-- Name: TABLE robot_state; Type: COMMENT; Schema: public; Owner: growbot
--
COMMENT ON TABLE public.robot_state IS 'All fields are mandatory, except that seen_at has the special case where if it''s `null`, the robot has not been "seen" yet. That is to say, the robot hasn''t been turned on yet.
This is so that the interactive interfaces can report "Not seen yet" instead of just the default (blank) values.';
--
-- Name: robots; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.robots (
id uuid NOT NULL,
admin_token text NOT NULL,
user_id integer,
title text DEFAULT ''::text NOT NULL,
created_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
ALTER TABLE public.robots OWNER TO growbot;
--
-- Name: users; Type: TABLE; Schema: public; Owner: growbot
--
CREATE TABLE public.users (
id integer NOT NULL,
forename text NOT NULL,
surname text NOT NULL,
password character(60) NOT NULL,
email character varying(254) NOT NULL,
is_activated boolean DEFAULT false NOT NULL,
created_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
ALTER TABLE public.users OWNER TO growbot;
--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: growbot
--
CREATE SEQUENCE public.users_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.users_id_seq OWNER TO growbot;
--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: growbot
--
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
--
-- Name: event_actions id; Type: DEFAULT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.event_actions ALTER COLUMN id SET DEFAULT nextval('public.event_actions_id_seq'::regclass);
--
-- Name: events id; Type: DEFAULT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.events ALTER COLUMN id SET DEFAULT nextval('public.events_id_seq'::regclass);
--
-- Name: log id; Type: DEFAULT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.log ALTER COLUMN id SET DEFAULT nextval('public.log_id_seq'::regclass);
--
-- Name: plant_photos id; Type: DEFAULT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plant_photos ALTER COLUMN id SET DEFAULT nextval('public.plant_photos_id_seq'::regclass);
--
-- Name: plants id; Type: DEFAULT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plants ALTER COLUMN id SET DEFAULT nextval('public.plants_id_seq'::regclass);
--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);
--
-- Name: event_actions event_actions_id_key; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.event_actions
ADD CONSTRAINT event_actions_id_key PRIMARY KEY (id);
--
-- Name: events events_id_key; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.events
ADD CONSTRAINT events_id_key PRIMARY KEY (id);
--
-- Name: log log_id_pkey; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.log
ADD CONSTRAINT log_id_pkey PRIMARY KEY (id);
--
-- Name: plant_photos plant_photos_id_key; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plant_photos
ADD CONSTRAINT plant_photos_id_key PRIMARY KEY (id);
--
-- Name: plants plants_id_pkey; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plants
ADD CONSTRAINT plants_id_pkey PRIMARY KEY (id);
--
-- Name: plants plants_name_user_id_key; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plants
ADD CONSTRAINT plants_name_user_id_key UNIQUE (user_id, name);
--
-- Name: robot_state robot_state_id_pkey; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.robot_state
ADD CONSTRAINT robot_state_id_pkey PRIMARY KEY (id);
--
-- Name: robots robots_id_pkey; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.robots
ADD CONSTRAINT robots_id_pkey PRIMARY KEY (id);
--
-- Name: users users_email_key; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_email_key UNIQUE (email);
--
-- Name: users users_id_pkey; Type: CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_id_pkey PRIMARY KEY (id);
--
-- Name: robots trig_create_state; Type: TRIGGER; Schema: public; Owner: growbot
--
CREATE TRIGGER trig_create_state AFTER INSERT ON public.robots FOR EACH ROW EXECUTE PROCEDURE public.growbot_create_state();
--
-- Name: event_actions event_actions_event_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.event_actions
ADD CONSTRAINT event_actions_event_id_fkey FOREIGN KEY (event_id) REFERENCES public.events(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: event_actions event_actions_plant_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.event_actions
ADD CONSTRAINT event_actions_plant_id_fkey FOREIGN KEY (plant_id) REFERENCES public.plants(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: event_actions event_actions_robot_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.event_actions
ADD CONSTRAINT event_actions_robot_id_fkey FOREIGN KEY (robot_id) REFERENCES public.robots(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: events events_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.events
ADD CONSTRAINT events_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: log log_plant_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.log
ADD CONSTRAINT log_plant_id_fkey FOREIGN KEY (plant_id) REFERENCES public.plants(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: log log_robot_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.log
ADD CONSTRAINT log_robot_id_fkey FOREIGN KEY (robot_id) REFERENCES public.robots(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: log log_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.log
ADD CONSTRAINT log_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: plant_photos plant_photos_plant_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plant_photos
ADD CONSTRAINT plant_photos_plant_id_fkey FOREIGN KEY (plant_id) REFERENCES public.plants(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: plants plants_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.plants
ADD CONSTRAINT plants_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: robot_state robot_state_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.robot_state
ADD CONSTRAINT robot_state_id_fkey FOREIGN KEY (id) REFERENCES public.robots(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- Name: robots robots_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: growbot
--
ALTER TABLE ONLY public.robots
ADD CONSTRAINT robots_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE;
--
-- PostgreSQL database dump complete
--