-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPG.sql
541 lines (417 loc) · 16.6 KB
/
PG.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
CREATE SCHEMA private;
ALTER SCHEMA private OWNER TO postgres;
CREATE LANGUAGE plpgsql;
SET check_function_bodies = false;
CREATE FUNCTION private.set_session_variable(var_name text, var_value text) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
AS $_$
begin
perform pg_catalog.set_config('private.' || var_name, var_value, false);
end;
$_$;
ALTER FUNCTION private.set_session_variable(var_name text, var_value text) OWNER TO postgres;
CREATE FUNCTION audit_fullmodified() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
AS $_$
DECLARE
user_id integer := private.get_session_variable('audit_user',
'0')::integer;
rv record;
BEGIN
if TG_OP = 'INSERT' or TG_OP = 'UPDATE' THEN
NEW.modified_timestamp = CURRENT_TIMESTAMP;
NEW.modified_by_user = user_id;
rv = NEW;
else
rv = OLD;
END IF;
perform pg_notify(TG_TABLE_NAME || '_change',
json_build_object('operation',TG_OP,'pk',rv.id,'id',user_id)::text);
RETURN rv;
END$_$;
COMMENT ON FUNCTION audit_fullmodified() IS 'Provides created values for audit columns.';
CREATE FUNCTION private.get_session_variable(var_name text, default_value text) RETURNS text
LANGUAGE plpgsql SECURITY DEFINER
AS $_$
begin
return pg_catalog.current_setting('private.' || var_name);
exception
when undefined_object then
return default_value;
end;
$_$;
ALTER FUNCTION private.get_session_variable(var_name text, default_value text) OWNER TO postgres;
CREATE FUNCTION audit_full() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
AS $_$
DECLARE
user_id integer := private.get_session_variable('audit_user',
'0')::integer;
rv record;
BEGIN
IF TG_OP = 'INSERT' THEN
NEW.created_timestamp = CURRENT_TIMESTAMP;
NEW.created_by_user = user_id;
NEW.modified_timestamp = CURRENT_TIMESTAMP;
NEW.modified_by_user = user_id;
rv = NEW;
ELSIF TG_OP = 'UPDATE' THEN
NEW.created_timestamp = OLD.created_timestamp;
NEW.created_by_user = OLD.created_by_user;
NEW.modified_timestamp = CURRENT_TIMESTAMP;
NEW.modified_by_user = user_id;
rv = NEW;
ELSE
rv = OLD;
END IF;
perform pg_notify(TG_TABLE_NAME || '_change',
json_build_object('operation',TG_OP,'pk',rv.id,'id',user_id)::text);
RETURN rv;
END$_$;
COMMENT ON FUNCTION audit_full() IS 'Provides created/modified values for audit columns.';
CREATE FUNCTION private.get_session_variable(var_name text) RETURNS text
LANGUAGE plpgsql SECURITY DEFINER
AS $_$
begin
return pg_catalog.current_setting('private.' || var_name);
end;
$_$;
ALTER FUNCTION private.get_session_variable(var_name text) OWNER TO postgres;
CREATE FUNCTION private.set_session(ab_sid bigint, OUT login_id bigint, OUT session_id bigint, OUT tzname text, OUT ab_session_id bigint) RETURNS record
LANGUAGE plpgsql SECURITY DEFINER
AS $_$
declare
srec record;
begin
login_id := null;
session_id := null;
tzname := null;
ab_session_id := ab_sid;
select * into strict srec from session where ab_session_id = ab_sid;
login_id = srec.login_id;
perform pg_catalog.set_config('private.audit_user', srec.login_id::text, false);
session_id = srec.id;
perform pg_catalog.set_config('private.audit_session', srec.id::text, false);
if srec.tzname is not null then
tzname := srec.tzname;
perform pg_catalog.set_config('timezone', srec.tzname, false);
end if;
exception
when NO_DATA_FOUND then
raise notice 'ignoring NO_DATA_FOUND setting %', ab_sid;
when TOO_MANY_ROWS then
raise notice 'ignoring TOO_MANY_ROWS setting %', ab_sid;
when others then
raise notice 'ignoring OTHER error setting %', ab_sid;
-- crickets
return;
end;
$_$;
ALTER FUNCTION private.set_session(ab_sid bigint, OUT login_id bigint, OUT session_id bigint, OUT tzname text, OUT ab_session_id bigint) OWNER TO postgres;
CREATE AGGREGATE private.array_accum(anyelement) (
SFUNC = array_append,
STYPE = anyarray,
INITCOND = '{}');
ALTER AGGREGATE private.array_accum(anyelement) OWNER TO postgres;
CREATE SEQUENCE loginrole_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE loginrole_id_seq OWNER TO postgres;
CREATE SEQUENCE topic_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE topic_id_seq OWNER TO postgres;
CREATE SEQUENCE role_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE role_id_seq OWNER TO postgres;
CREATE SEQUENCE login_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE login_id_seq OWNER TO postgres;
CREATE SEQUENCE topicrole_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE topicrole_id_seq OWNER TO postgres;
CREATE SEQUENCE activity_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE activity_id_seq OWNER TO postgres;
CREATE SEQUENCE session_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE session_id_seq OWNER TO postgres;
CREATE SEQUENCE topicrole_role_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
ALTER SEQUENCE topicrole_role_id_seq OWNER TO postgres;
CREATE TABLE session (
id integer NOT NULL,
login_id integer,
ab_session_id bigint,
tzname text,
created_by_user integer NOT NULL,
created_timestamp timestamp with time zone NOT NULL,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE session OWNER TO postgres;
CREATE TABLE activity_type (
id text NOT NULL,
name text,
description text);
ALTER TABLE activity_type OWNER TO postgres;
CREATE TABLE loginrole (
id integer NOT NULL,
login_id integer NOT NULL,
role_id integer NOT NULL,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE loginrole OWNER TO postgres;
CREATE TABLE role (
bind_to integer,
name text NOT NULL,
id integer NOT NULL,
description text,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE role OWNER TO postgres;
CREATE TABLE activity (
id integer NOT NULL,
session_id integer,
topic_name text,
type_id text,
allow boolean,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE activity OWNER TO postgres;
CREATE TABLE topicrole (
id integer NOT NULL,
topic_id integer NOT NULL,
role_id integer NOT NULL,
type_id text NOT NULL,
allow boolean,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE topicrole OWNER TO postgres;
CREATE TABLE sqlauth (
component text NOT NULL,
version text,
profile jsonb);
ALTER TABLE sqlauth OWNER TO postgres;
CREATE TABLE login (
id integer NOT NULL,
login text,
fullname text,
password text NOT NULL,
salt text,
tzname text,
old_login text,
inactive boolean,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE login OWNER TO postgres;
CREATE TABLE topic (
id integer NOT NULL,
name text NOT NULL,
description text,
modified_by_user integer NOT NULL,
modified_timestamp timestamp with time zone NOT NULL);
ALTER TABLE topic OWNER TO postgres;
ALTER SEQUENCE loginrole_id_seq OWNED BY loginrole.id;
ALTER SEQUENCE topic_id_seq OWNED BY topic.id;
ALTER SEQUENCE role_id_seq OWNED BY role.id;
ALTER SEQUENCE login_id_seq OWNED BY login.id;
ALTER SEQUENCE topicrole_id_seq OWNED BY topicrole.id;
ALTER SEQUENCE activity_id_seq OWNED BY activity.id;
ALTER SEQUENCE session_id_seq OWNED BY session.id;
ALTER SEQUENCE topicrole_role_id_seq OWNED BY topicrole.role_id;
ALTER TABLE session ALTER COLUMN id SET DEFAULT nextval('session_id_seq'::regclass);
ALTER TABLE loginrole ALTER COLUMN id SET DEFAULT nextval('loginrole_id_seq'::regclass);
ALTER TABLE role ALTER COLUMN id SET DEFAULT nextval('role_id_seq'::regclass);
ALTER TABLE activity ALTER COLUMN id SET DEFAULT nextval('activity_id_seq'::regclass);
ALTER TABLE topicrole ALTER COLUMN id SET DEFAULT nextval('topicrole_id_seq'::regclass);
ALTER TABLE topicrole ALTER COLUMN role_id SET DEFAULT nextval('topicrole_role_id_seq'::regclass);
ALTER TABLE login ALTER COLUMN id SET DEFAULT nextval('login_id_seq'::regclass);
ALTER TABLE topic ALTER COLUMN id SET DEFAULT nextval('topic_id_seq'::regclass);
ALTER TABLE topic ADD CONSTRAINT topic_name_key UNIQUE (name);
ALTER TABLE activity_type ADD CONSTRAINT activity_type_pkey PRIMARY KEY (id);
ALTER TABLE login ADD CONSTRAINT login_login_key UNIQUE (login);
ALTER TABLE role ADD CONSTRAINT role_name_key UNIQUE (name);
ALTER TABLE session ADD CONSTRAINT session_pkey PRIMARY KEY (id);
ALTER TABLE role ADD CONSTRAINT role_pkey PRIMARY KEY (id);
ALTER TABLE topic ADD CONSTRAINT topic_pkey PRIMARY KEY (id);
ALTER TABLE session ADD CONSTRAINT session_ab_session_id_key UNIQUE (ab_session_id);
ALTER TABLE loginrole ADD CONSTRAINT loginrole_pkey PRIMARY KEY (id);
ALTER TABLE topicrole ADD CONSTRAINT topicrole_pkey PRIMARY KEY (id);
ALTER TABLE topicrole ADD CONSTRAINT topicrole_topic_id_role_id_type_id UNIQUE (topic_id, role_id, type_id, allow);
ALTER TABLE loginrole ADD CONSTRAINT loginrole_login_id_role_id UNIQUE (login_id, role_id);
ALTER TABLE login ADD CONSTRAINT login_pkey PRIMARY KEY (id);
ALTER TABLE activity ADD CONSTRAINT activity_pkey PRIMARY KEY (id);
ALTER TABLE session ADD CONSTRAINT session_ab_session_id UNIQUE (ab_session_id);
ALTER TABLE sqlauth ADD CONSTRAINT sqlauth_pkey PRIMARY KEY (component);
ALTER TABLE loginrole ADD CONSTRAINT loginrole_login_id_fkey FOREIGN KEY (login_id) REFERENCES login (id);
ALTER TABLE role ADD CONSTRAINT role_topic_binding FOREIGN KEY (bind_to) REFERENCES topic (id) ON DELETE SET NULL;
ALTER TABLE topicrole ADD CONSTRAINT topicrole_role_id_fkey FOREIGN KEY (role_id) REFERENCES role (id);
ALTER TABLE session ADD CONSTRAINT session_login_id_fkey FOREIGN KEY (login_id) REFERENCES login (id);
ALTER TABLE activity ADD CONSTRAINT activity_session_id_fkey FOREIGN KEY (session_id) REFERENCES session (id);
ALTER TABLE topicrole ADD CONSTRAINT topicrole_topic_id_fkey FOREIGN KEY (topic_id) REFERENCES topic (id);
ALTER TABLE topicrole ADD CONSTRAINT topicrole_type_id_fkey FOREIGN KEY (type_id) REFERENCES activity_type (id);
ALTER TABLE activity ADD CONSTRAINT activity_type_id_fkey FOREIGN KEY (type_id) REFERENCES activity_type (id);
ALTER TABLE loginrole ADD CONSTRAINT loginrole_role_id_fkey FOREIGN KEY (role_id) REFERENCES role (id);
CREATE TRIGGER topic_20_audit_fullmodified
BEFORE INSERT OR UPDATE OR DELETE ON topic
FOR EACH ROW
EXECUTE PROCEDURE audit_fullmodified();
CREATE TRIGGER login_20_audit_fullmodified
BEFORE INSERT OR UPDATE OR DELETE ON login
FOR EACH ROW
EXECUTE PROCEDURE audit_fullmodified();
CREATE TRIGGER role_20_audit_fullmodified
BEFORE INSERT OR UPDATE OR DELETE ON role
FOR EACH ROW
EXECUTE PROCEDURE audit_fullmodified();
CREATE TRIGGER activity_20_audit_fullmodified
BEFORE INSERT OR UPDATE OR DELETE ON activity
FOR EACH ROW
EXECUTE PROCEDURE audit_fullmodified();
CREATE TRIGGER topicrole_20_audit_fullmodified
BEFORE INSERT OR UPDATE OR DELETE ON topicrole
FOR EACH ROW
EXECUTE PROCEDURE audit_fullmodified();
CREATE TRIGGER loginrole_20_audit_fullmodified
BEFORE INSERT OR UPDATE OR DELETE ON loginrole
FOR EACH ROW
EXECUTE PROCEDURE audit_fullmodified();
CREATE TRIGGER session_20_audit_full
BEFORE INSERT OR UPDATE OR DELETE ON session
FOR EACH ROW
EXECUTE PROCEDURE audit_full();
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_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: activity_type; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY activity_type (id, name, description) FROM stdin;
call rpc call A call to a registered rpc
register register Register an rpc so it can be called
publish publish Publish a message
subscribe subscribe Subscribe to a message
start begin a session Begin a new autobahn session
end end a session End an existing autobahn session
admin Admin Domain Rule over this space
\.
COPY login (id, login, fullname, password, tzname, modified_by_user, modified_timestamp, salt, old_login, inactive) FROM stdin;
0 sys System Internal Root 17q8IJB2AO5dCvCcjJg/DHcP51dAHrULJUD9zoNZIbg= America/Chicago 0 2014-12-03 20:21:15.442137-06 oihewuhweg98797325 \N \N
1 adm Public System Administrator 17q8IJB2AO5dCvCcjJg/DHcP51dAHrULJUD9zoNZIbg= America/Chicago 0 2014-12-03 20:21:15.442137-06 oihewuhweg98797325 \N \N
\.
SELECT pg_catalog.setval('login_id_seq', 2, true);
COPY topic (id, name, description, modified_by_user, modified_timestamp) FROM stdin;
1 sys All things system, including authentication and authorization 0 2014-11-25 12:59:21.890086-06
2 sys.topic Topic stuff 0 2014-12-03 19:58:36.862908-06
3 sys.role Add a topic 0 2014-12-03 20:07:45.035027-06
4 sys.userrole User role association 0 2014-12-03 19:58:36.862908-06
5 sys.topicrole Topic role association 0 2014-12-03 19:58:36.862908-06
200 adm Administrator Space 0 2014-11-26 08:06:09.931566-06
300 com Community Space 0 2014-11-26 08:06:09.931566-06
400 pub Public Space 0 2014-11-26 08:06:09.931566-06
500 role Role Binding Hierarchy 0 2014-11-26 08:06:09.931566-06
501 role.sys Role sys Binding 0 2014-11-26 08:06:09.931566-06
502 role.adm Role adm Binding 0 2014-11-26 08:06:09.931566-06
503 role.com Role com Binding 0 2014-11-26 08:06:09.931566-06
504 role.public Role Public Binding 0 2014-11-26 08:06:09.931566-06
\.
SELECT pg_catalog.setval('topic_id_seq', 600, true);
COPY role (id, name, description, bind_to, modified_by_user, modified_timestamp) FROM stdin;
1 sys All things system 501 0 2014-11-25 12:54:50.715127-06
2 adm Administration 502 0 2014-11-25 12:54:50.715127-06
3 com Community 503 0 2014-11-25 12:54:50.715127-06
4 public Public 504 0 2014-11-25 12:54:50.715127-06
\.
SELECT pg_catalog.setval('role_id_seq', 5, true);
COPY loginrole (id, login_id, role_id, modified_by_user, modified_timestamp) FROM stdin;
1 0 1 0 2014-11-25 16:40:27.358583-06
2 0 2 0 2014-11-25 16:41:37.57475-06
3 0 3 0 2014-11-25 16:41:37.57475-06
4 0 4 0 2014-11-25 16:41:37.57475-06
5 1 2 0 2014-11-25 17:00:27.238068-06
6 1 3 0 2014-11-25 17:05:24.62864-06
7 1 4 0 2014-11-25 17:23:28.316645-06
\.
SELECT pg_catalog.setval('loginrole_id_seq', 8, true);
COPY sqlauth (component, version, profile) FROM stdin;
\.
COPY topicrole (id, topic_id, role_id, type_id, allow, modified_by_user, modified_timestamp) FROM stdin;
1 1 1 call t 0 2014-11-25 13:11:42.31319-06
2 1 1 register t 0 2014-11-25 13:11:46.196604-06
3 1 1 publish t 0 2014-11-25 13:11:58.806129-06
4 1 1 subscribe t 0 2014-11-25 13:12:14.718075-06
5 1 1 admin t 0 2014-11-25 13:12:29.087589-06
6 200 2 call t 0 2014-11-25 13:11:42.31319-06
7 200 2 register t 0 2014-11-25 13:11:46.196604-06
8 200 2 publish t 0 2014-11-25 13:11:58.806129-06
9 200 2 subscribe t 0 2014-11-25 13:12:14.718075-06
10 200 2 admin t 0 2014-11-25 13:12:29.087589-06
11 300 3 call t 0 2014-11-25 13:11:42.31319-06
12 300 3 register t 0 2014-11-25 13:11:46.196604-06
13 300 3 publish t 0 2014-11-25 13:11:58.806129-06
14 300 3 subscribe t 0 2014-11-25 13:12:14.718075-06
15 300 3 admin t 0 2014-11-25 13:12:29.087589-06
16 400 4 call t 0 2014-11-25 13:11:42.31319-06
17 400 4 register t 0 2014-11-25 13:11:46.196604-06
18 400 4 publish t 0 2014-11-25 13:11:58.806129-06
19 400 4 subscribe t 0 2014-11-25 13:12:14.718075-06
20 400 4 admin t 0 2014-11-25 13:12:29.087589-06
21 2 4 call t 0 2014-11-25 13:12:29.087589-06
22 3 4 call t 0 2014-11-25 13:12:29.087589-06
23 4 4 call t 0 2014-11-25 13:12:29.087589-06
24 5 4 call t 0 2014-11-25 13:12:29.087589-06
100 500 1 admin t 0 2014-11-25 13:12:29.087589-06
101 501 1 admin t 0 2014-11-25 13:12:29.087589-06
102 502 2 admin t 0 2014-11-25 13:12:29.087589-06
103 503 3 admin t 0 2014-11-25 13:12:29.087589-06
104 504 4 admin t 0 2014-11-25 13:12:29.087589-06
\.
--
-- Name: topicrole_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('topicrole_id_seq', 500, true);
--
-- Name: topicrole_role_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('topicrole_role_id_seq', 1, false);
--
-- PostgreSQL database dump complete
--
insert into sqlauth (component,version) values ('database','0.2.9');