-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathschema.sql
More file actions
607 lines (412 loc) · 14.5 KB
/
Copy pathschema.sql
File metadata and controls
607 lines (412 loc) · 14.5 KB
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
--
-- PostgreSQL database dump
--
-- Dumped from database version 10.5 (Ubuntu 10.5-0ubuntu0.18.04)
-- Dumped by pg_dump version 10.5 (Ubuntu 10.5-0ubuntu0.18.04)
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: 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: addalias(character varying, character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.addalias(nick_ character varying, alias_ character varying) RETURNS boolean
LANGUAGE plpgsql
AS $$
DECLARE
cyclic boolean;
BEGIN
SELECT (SELECT nick FROM aliaschain(alias_) WHERE lower(nick) = lower(nick_)) IS NULL INTO cyclic;
IF cyclic THEN
PERFORM delalias(nick_);
INSERT INTO aliases VALUES (nick_, alias_);
END IF;
RETURN cyclic;
END;
$$;
ALTER FUNCTION public.addalias(nick_ character varying, alias_ character varying) OWNER TO infobot;
--
-- Name: addinfo(character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.addinfo(nick_ character varying, user_ character varying, host_ character varying, info_ character varying) RETURNS integer
LANGUAGE sql
AS $$
SELECT delalias(nick_);
INSERT INTO infos VALUES (DEFAULT, nick_, user_, host_, info_) RETURNING id;
$$;
ALTER FUNCTION public.addinfo(nick_ character varying, user_ character varying, host_ character varying, info_ character varying) OWNER TO infobot;
--
-- Name: alias(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.alias(nick_ character varying) RETURNS character varying
LANGUAGE sql STABLE
AS $$
SELECT nick FROM aliaschain(nick_) ORDER BY i DESC LIMIT 1;
$$;
ALTER FUNCTION public.alias(nick_ character varying) OWNER TO infobot;
--
-- Name: aliaschain(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.aliaschain(nick_ character varying) RETURNS TABLE(i integer, nick character varying)
LANGUAGE sql STABLE ROWS 2
AS $$
WITH RECURSIVE chain(i, nick) AS (
VALUES (0, nick_)
UNION
SELECT chain.i + 1, aliases.alias FROM aliases, chain WHERE lower(aliases.nick) = lower(chain.nick)
)
SELECT * FROM chain;
$$;
ALTER FUNCTION public.aliaschain(nick_ character varying) OWNER TO infobot;
--
-- Name: copyinfo(character varying, character varying, character varying, integer); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.copyinfo(nick_ character varying, user_ character varying, host_ character varying, id_ integer) RETURNS integer
LANGUAGE sql
AS $$
SELECT delalias(nick_);
INSERT INTO infos VALUES (DEFAULT, nick_, user_, host_, (SELECT info FROM infos WHERE id = id_)) RETURNING id;
$$;
ALTER FUNCTION public.copyinfo(nick_ character varying, user_ character varying, host_ character varying, id_ integer) OWNER TO infobot;
--
-- Name: delalias(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.delalias(nick_ character varying) RETURNS void
LANGUAGE sql
AS $$
DELETE FROM aliases WHERE lower(nick) = lower(nick_);
$$;
ALTER FUNCTION public.delalias(nick_ character varying) OWNER TO infobot;
--
-- Name: delinfo(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.delinfo(nick_ character varying) RETURNS void
LANGUAGE sql
AS $$
SELECT addalias(nick_, NULL);
SELECT null::void; -- Crate a bogus matching return value for SQL to stop whining
$$;
ALTER FUNCTION public.delinfo(nick_ character varying) OWNER TO infobot;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: infos; Type: TABLE; Schema: public; Owner: infobot
--
CREATE TABLE public.infos (
id integer NOT NULL,
nick text NOT NULL,
"user" text,
host text,
info text,
ts timestamp without time zone DEFAULT timezone('utc'::text, now())
);
ALTER TABLE public.infos OWNER TO infobot;
--
-- Name: info(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.info(nick_ character varying) RETURNS SETOF public.infos
LANGUAGE sql STABLE ROWS 1
AS $$
SELECT * FROM infos WHERE id = (SELECT infoid(nick_));
$$;
ALTER FUNCTION public.info(nick_ character varying) OWNER TO infobot;
--
-- Name: infohistory(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.infohistory(nick_ character varying) RETURNS SETOF public.infos
LANGUAGE sql ROWS 1
AS $$
SELECT infos.* FROM infos, aliaschain(nick_) as ac WHERE infos.nick = ac.nick ORDER BY infos.id;
$$;
ALTER FUNCTION public.infohistory(nick_ character varying) OWNER TO infobot;
--
-- Name: infoid(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.infoid(nick_ character varying) RETURNS integer
LANGUAGE sql STABLE
AS $$
SELECT id FROM infos WHERE lower(nick) = lower(alias(nick_)) ORDER BY ts DESC LIMIT 1;
$$;
ALTER FUNCTION public.infoid(nick_ character varying) OWNER TO infobot;
--
-- Name: infoid2(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.infoid2(nick_ character varying) RETURNS integer
LANGUAGE sql
AS $$
SELECT id FROM infos WHERE lower(nick) = lower(nick_) ORDER BY ts DESC LIMIT 1;
$$;
ALTER FUNCTION public.infoid2(nick_ character varying) OWNER TO infobot;
--
-- Name: latesttimestamp(character varying); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.latesttimestamp(nick character varying) RETURNS timestamp without time zone
LANGUAGE sql COST 10
AS $_$
SELECT ts FROM infos WHERE infos.nick = $1 ORDER BY ts DESC LIMIT 1; $_$;
ALTER FUNCTION public.latesttimestamp(nick character varying) OWNER TO infobot;
--
-- Name: multiinfo(character varying[]); Type: FUNCTION; Schema: public; Owner: infobot
--
CREATE FUNCTION public.multiinfo(nicks_ character varying[]) RETURNS TABLE(alias character varying, id integer, nick character varying, "user" character varying, host character varying, info text, ts timestamp without time zone)
LANGUAGE sql ROWS 100
AS $$
SELECT nicks, infos.* FROM unnest(nicks_) AS nicks
LEFT JOIN infos ON infos.id = (SELECT infoid(nicks));
$$;
ALTER FUNCTION public.multiinfo(nicks_ character varying[]) OWNER TO infobot;
--
-- Name: aliases; Type: TABLE; Schema: public; Owner: infobot
--
CREATE TABLE public.aliases (
nick character varying(64) NOT NULL,
alias character varying(64),
ts timestamp without time zone DEFAULT timezone('utc'::text, now())
);
ALTER TABLE public.aliases OWNER TO infobot;
--
-- Name: aliasinfos; Type: VIEW; Schema: public; Owner: infobot
--
CREATE VIEW public.aliasinfos AS
SELECT a.alias AS rootnick,
a.ag AS nicks,
infos.info
FROM ( SELECT m.alias,
array_agg(m.nick) AS ag
FROM ( SELECT aliases.nick,
public.alias(aliases.nick) AS alias
FROM public.aliases
UNION
SELECT DISTINCT infos_1.nick,
infos_1.nick
FROM public.infos infos_1
WHERE (NOT (EXISTS ( SELECT NULL::text AS unknown
FROM public.aliases
WHERE (lower((aliases.nick)::text) = lower(infos_1.nick)))))) m
GROUP BY m.alias) a,
public.infos
WHERE (infos.id = ( SELECT public.infoid2(a.alias) AS infoid2));
ALTER TABLE public.aliasinfos OWNER TO infobot;
--
-- Name: bots; Type: TABLE; Schema: public; Owner: infobot
--
CREATE TABLE public.bots (
id integer NOT NULL,
cmdchar character varying(4) NOT NULL,
bot character varying(64) NOT NULL,
owner character varying(64)
);
ALTER TABLE public.bots OWNER TO infobot;
--
-- Name: bots_id_seq; Type: SEQUENCE; Schema: public; Owner: infobot
--
CREATE SEQUENCE public.bots_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.bots_id_seq OWNER TO infobot;
--
-- Name: bots_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: infobot
--
ALTER SEQUENCE public.bots_id_seq OWNED BY public.bots.id;
--
-- Name: infos_id_seq; Type: SEQUENCE; Schema: public; Owner: infobot
--
CREATE SEQUENCE public.infos_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.infos_id_seq OWNER TO infobot;
--
-- Name: infos_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: infobot
--
ALTER SEQUENCE public.infos_id_seq OWNED BY public.infos.id;
--
-- Name: latestinfos; Type: VIEW; Schema: public; Owner: infobot
--
CREATE VIEW public.latestinfos AS
SELECT infos.id,
infos.nick,
infos."user",
infos.host,
infos.info,
infos.ts
FROM public.infos
WHERE (infos.ts = ( SELECT public.latesttimestamp((infos.nick)::character varying) AS latesttimestamp));
ALTER TABLE public.latestinfos OWNER TO infobot;
--
-- Name: reminders; Type: TABLE; Schema: public; Owner: infobot
--
CREATE TABLE public.reminders (
id integer NOT NULL,
from_nick character varying(64) NOT NULL,
to_nick character varying(64) NOT NULL,
message text NOT NULL,
channel text NOT NULL,
begints timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
endts timestamp without time zone NOT NULL,
fulfilled boolean DEFAULT false NOT NULL
);
ALTER TABLE public.reminders OWNER TO infobot;
--
-- Name: reminders_id_seq; Type: SEQUENCE; Schema: public; Owner: infobot
--
CREATE SEQUENCE public.reminders_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.reminders_id_seq OWNER TO infobot;
--
-- Name: reminders_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: infobot
--
ALTER SEQUENCE public.reminders_id_seq OWNED BY public.reminders.id;
--
-- Name: tells; Type: TABLE; Schema: public; Owner: infobot
--
CREATE TABLE public.tells (
tellid integer NOT NULL,
from_nick character varying(64) NOT NULL,
to_nick character varying(64) NOT NULL,
message text NOT NULL,
begints timestamp without time zone DEFAULT timezone('utc'::text, now()),
fulfilled boolean DEFAULT false
);
ALTER TABLE public.tells OWNER TO infobot;
--
-- Name: tells_tellid_seq; Type: SEQUENCE; Schema: public; Owner: infobot
--
CREATE SEQUENCE public.tells_tellid_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.tells_tellid_seq OWNER TO infobot;
--
-- Name: tells_tellid_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: infobot
--
ALTER SEQUENCE public.tells_tellid_seq OWNED BY public.tells.tellid;
--
-- Name: bots id; Type: DEFAULT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.bots ALTER COLUMN id SET DEFAULT nextval('public.bots_id_seq'::regclass);
--
-- Name: infos id; Type: DEFAULT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.infos ALTER COLUMN id SET DEFAULT nextval('public.infos_id_seq'::regclass);
--
-- Name: reminders id; Type: DEFAULT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.reminders ALTER COLUMN id SET DEFAULT nextval('public.reminders_id_seq'::regclass);
--
-- Name: tells tellid; Type: DEFAULT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.tells ALTER COLUMN tellid SET DEFAULT nextval('public.tells_tellid_seq'::regclass);
--
-- Name: aliases alias_pkey; Type: CONSTRAINT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.aliases
ADD CONSTRAINT alias_pkey PRIMARY KEY (nick);
--
-- Name: bots bots_pkey; Type: CONSTRAINT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.bots
ADD CONSTRAINT bots_pkey PRIMARY KEY (id);
--
-- Name: bots cmdchar_bot_uniq; Type: CONSTRAINT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.bots
ADD CONSTRAINT cmdchar_bot_uniq UNIQUE (cmdchar, bot);
--
-- Name: infos infos_pkey; Type: CONSTRAINT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.infos
ADD CONSTRAINT infos_pkey PRIMARY KEY (id);
--
-- Name: reminders reminders_pkey; Type: CONSTRAINT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.reminders
ADD CONSTRAINT reminders_pkey PRIMARY KEY (id);
--
-- Name: tells tells_pkey; Type: CONSTRAINT; Schema: public; Owner: infobot
--
ALTER TABLE ONLY public.tells
ADD CONSTRAINT tells_pkey PRIMARY KEY (tellid);
--
-- Name: info_nick_index; Type: INDEX; Schema: public; Owner: infobot
--
CREATE INDEX info_nick_index ON public.infos USING btree (nick);
--
-- Name: infos_lower_nick_ts_desc_index; Type: INDEX; Schema: public; Owner: infobot
--
CREATE INDEX infos_lower_nick_ts_desc_index ON public.infos USING btree (lower(nick), ts DESC);
--
-- Name: SCHEMA public; Type: ACL; Schema: -; Owner: postgres
--
GRANT USAGE ON SCHEMA public TO readonly;
--
-- Name: TABLE infos; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.infos TO readonly;
--
-- Name: TABLE aliases; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.aliases TO readonly;
--
-- Name: TABLE aliasinfos; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.aliasinfos TO readonly;
--
-- Name: TABLE bots; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.bots TO readonly;
--
-- Name: SEQUENCE bots_id_seq; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON SEQUENCE public.bots_id_seq TO readonly;
--
-- Name: SEQUENCE infos_id_seq; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON SEQUENCE public.infos_id_seq TO readonly;
--
-- Name: TABLE latestinfos; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.latestinfos TO readonly;
--
-- Name: TABLE reminders; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.reminders TO readonly;
--
-- Name: SEQUENCE reminders_id_seq; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON SEQUENCE public.reminders_id_seq TO readonly;
--
-- Name: TABLE tells; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON TABLE public.tells TO readonly;
--
-- Name: SEQUENCE tells_tellid_seq; Type: ACL; Schema: public; Owner: infobot
--
GRANT SELECT ON SEQUENCE public.tells_tellid_seq TO readonly;
--
-- PostgreSQL database dump complete
--