/
psql-footer.vm
1399 lines (1112 loc) · 46.4 KB
/
psql-footer.vm
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Copyright 2006-2017 University of Dundee. All rights reserved.
-- Use is subject to license terms supplied in LICENSE.txt
--
--
-- This file was generated by dsl/resources/ome/dsl/psql-footer.vm
--
--
-- Triggers
--
#foreach($type in $types)
#foreach($prop in $type.classProperties)
#if( $prop.foreignKey && $prop.ordered && ! $prop.one2Many )
#set( $tbl_name = $prop.actualType.table )
#set( $col_name = $prop.name )
CREATE OR REPLACE FUNCTION ${tbl_name}_${col_name}_index_move() RETURNS "trigger" AS '
DECLARE
duplicate INT8;
BEGIN
-- Avoids a query if the new and old values of x are the same.
IF new.${col_name} = old.${col_name} AND new.${col_name}_index = old.${col_name}_index THEN
RETURN new;
END IF;
-- At most, there should be one duplicate
SELECT id INTO duplicate
FROM ${tbl_name}
WHERE ${col_name} = new.${col_name} AND ${col_name}_index = new.${col_name}_index
OFFSET 0
LIMIT 1;
IF duplicate IS NOT NULL THEN
RAISE NOTICE ''Remapping ${tbl_name} %% via (-1 - oldvalue )'', duplicate;
UPDATE ${tbl_name} SET ${col_name}_index = -1 - ${col_name}_index WHERE id = duplicate;
END IF;
RETURN new;
END;' LANGUAGE plpgsql;
CREATE TRIGGER ${tbl_name}_${col_name}_index_trigger
BEFORE UPDATE ON ${tbl_name}
FOR EACH ROW EXECUTE PROCEDURE ${tbl_name}_${col_name}_index_move ();
#end
#end
#end
--
-- Indexes
--
#foreach($type in $types)
#if(!$type.superclass && !$type.global)
#set( $tbl_name = $type.tableName() )
CREATE INDEX i_${tbl_name}_owner ON $tbl_name(owner_id);
CREATE INDEX i_${tbl_name}_group ON $tbl_name(group_id);
#end
#foreach($prop in $type.classProperties)
#if( $prop.foreignKey && ! $prop.name.equals("details") && ! $prop.one2Many )
#set( $tbl_name = $prop.actualType.tableName() )
#set( $col_name = $prop.actualType.columnName($prop, '"') )
CREATE INDEX $type.indexName($prop) ON $tbl_name($col_name);
#end
#end
#end
CREATE INDEX annotation_name ON annotation(name);
CREATE INDEX namespace_displayname ON namespace(displayname);
CREATE INDEX roi_name ON roi(name);
--
-- Finally, a function for showing our permissions
-- select id, ome_perms(permissions) FROM sometable...
--
CREATE OR REPLACE FUNCTION ome_perms(p INT8) RETURNS VARCHAR AS '
DECLARE
ur CHAR DEFAULT ''-'';
uw CHAR DEFAULT ''-'';
gr CHAR DEFAULT ''-'';
gw CHAR DEFAULT ''-'';
wr CHAR DEFAULT ''-'';
ww CHAR DEFAULT ''-'';
BEGIN
-- annotate flags may be overwritten by write flags
-- shift 8 (-RWA--------)
SELECT INTO ur CASE WHEN (p & 1024) = 1024 THEN ''r'' ELSE ''-'' END;
SELECT INTO uw CASE WHEN (p & 512) = 512 THEN ''w''
WHEN (p & 256) = 256 THEN ''a'' ELSE ''-'' END;
-- shift 4 (-----RWA----)
SELECT INTO gr CASE WHEN (p & 64) = 64 THEN ''r'' ELSE ''-'' END;
SELECT INTO gw CASE WHEN (p & 32) = 32 THEN ''w''
WHEN (p & 16) = 16 THEN ''a'' ELSE ''-'' END;
-- shift 0 (---------RWA)
SELECT INTO wr CASE WHEN (p & 4) = 4 THEN ''r'' ELSE ''-'' END;
SELECT INTO ww CASE WHEN (p & 2) = 2 THEN ''w''
WHEN (p & 1) = 1 THEN ''a'' ELSE ''-'' END;
RETURN ur || uw || gr || gw || wr || ww;
END;' LANGUAGE plpgsql;
set constraints all deferred;
--
-- #1176 : create our own nextval() functionality for more consistent
-- sequence operation in hibernate. This functionality was updated for
-- OMERO 4.2 (#2508) in order to prevent logging during triggers.
--
CREATE OR REPLACE FUNCTION ome_nextval(seq VARCHAR) RETURNS INT8 AS '
BEGIN
RETURN ome_nextval(seq, 1);
END;' LANGUAGE plpgsql;
-- These renamings allow us to reuse the Hibernate-generated tables
-- for sequence generation. Eventually, a method might be found to
-- make Hibernate generate them for us.
CREATE SEQUENCE _lock_seq;
ALTER TABLE seq_table RENAME TO _lock_ids;
ALTER TABLE _lock_ids RENAME COLUMN sequence_name TO name;
ALTER TABLE _lock_ids DROP CONSTRAINT seq_table_pkey;
ALTER TABLE _lock_ids DROP COLUMN next_val;
ALTER TABLE _lock_ids ADD COLUMN id int PRIMARY KEY DEFAULT nextval('_lock_seq');
CREATE UNIQUE INDEX _lock_ids_name ON _lock_ids (name);
CREATE OR REPLACE FUNCTION ome_nextval(seq VARCHAR, increment int4) RETURNS INT8 AS '
DECLARE
Lid int4;
nv int8;
BEGIN
SELECT id INTO Lid FROM _lock_ids WHERE name = seq;
IF Lid IS NULL THEN
SELECT INTO Lid nextval(''_lock_seq'');
INSERT INTO _lock_ids (id, name) VALUES (Lid, seq);
END IF;
PERFORM pg_advisory_lock(1, Lid);
BEGIN
PERFORM nextval(seq) FROM generate_series(1, increment);
SELECT currval(seq) INTO nv;
EXCEPTION
WHEN OTHERS THEN
PERFORM pg_advisory_unlock(1, Lid);
RAISE;
END;
PERFORM pg_advisory_unlock(1, Lid);
RETURN nv;
END;' LANGUAGE plpgsql;
#foreach($type in $types)
#if(!$type.superclass)
#set($table = ${type.table})
CREATE SEQUENCE seq_${table}; INSERT INTO _lock_ids (name, id) SELECT 'seq_${table}', nextval('_lock_seq');
#end
#end
--
-- END #1176/#2508
--
--
-- #1390 : Triggering the addition of an "REINDEX" event on annotation and other events.
--
CREATE OR REPLACE FUNCTION _prepare_session(_event_id int8, _user_id int8, _group_id int8) RETURNS void
AS '
BEGIN
IF NOT EXISTS(SELECT table_name FROM information_schema.tables where table_name = ''_current_session'') THEN
CREATE TEMP TABLE _current_session ("event_id" int8, "user_id" int8, "group_id" int8) ON COMMIT DELETE ROWS;
INSERT INTO _current_session VALUES (_event_id, _user_id, _group_id);
ELSE
DELETE FROM _current_session;
INSERT INTO _current_session VALUES (_event_id, _user_id, _group_id);
END IF;
END;'
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION _current_event() RETURNS int8
AS '
DECLARE
eid int8;
BEGIN
IF NOT EXISTS(SELECT table_name FROM information_schema.tables where table_name = ''_current_session'') THEN
RETURN 0;
END IF;
SELECT INTO eid event_id FROM _current_session;
RETURN eid;
END;'
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION _current_or_new_event() RETURNS int8
AS '
DECLARE
eid int8;
BEGIN
SELECT INTO eid _current_event();
IF eid = 0 OR eid IS NULL THEN
SELECT INTO eid ome_nextval(''seq_event'');
INSERT INTO event (id, permissions, status, time, experimenter, experimentergroup, session, type)
SELECT eid, -52, ''TRIGGERED'', clock_timestamp(), 0, 0, 0, 0;
END IF;
RETURN eid;
END;'
LANGUAGE plpgsql;
CREATE TABLE _reindexing_required (
event_id BIGINT NOT NULL,
entity_type TEXT NOT NULL,
entity_id BIGINT NOT NULL,
CONSTRAINT FK_reindexing_required_event_id
FOREIGN KEY (event_id)
REFERENCES event);
CREATE INDEX _reindexing_required_event_index
ON _reindexing_required (event_id);
CREATE INDEX _reindexing_required_row_index
ON _reindexing_required (event_id, entity_type, entity_id);
CREATE FUNCTION annotation_update_event_trigger() RETURNS TRIGGER AS $$
DECLARE
pid BIGINT;
eid BIGINT;
BEGIN
SELECT INTO eid _current_or_new_event();
#foreach($type in $types)
#if( ${type.annotated} && !${type.superclass})
FOR pid IN SELECT DISTINCT parent FROM ${type.tableName()}annotationlink WHERE child = new.id
LOOP
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, '${type.id}', pid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS ua
WHERE ua.event_id = eid AND ua.entity_type = '${type.id}' AND ua.entity_id = pid);
END LOOP;
#end
#end
RETURN new;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER annotation_trigger
AFTER UPDATE ON annotation
FOR EACH ROW
EXECUTE PROCEDURE annotation_update_event_trigger();
-- Note: not adding an annotation insert trigger since that would be handled by any links on insert
CREATE OR REPLACE FUNCTION annotation_link_update_trigger() RETURNS "trigger"
AS '
DECLARE
eid int8;
BEGIN
SELECT INTO eid _current_or_new_event();
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, TG_ARGV[0], old.parent
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS ua
WHERE ua.event_id = eid AND ua.entity_type = TG_ARGV[0] AND ua.entity_id = old.parent);
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, TG_ARGV[0], new.parent
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS ua
WHERE ua.event_id = eid AND ua.entity_type = TG_ARGV[0] AND ua.entity_id = new.parent);
RETURN new;
END;'
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION annotation_link_insert_trigger() RETURNS "trigger"
AS '
DECLARE
eid int8;
BEGIN
SELECT INTO eid _current_or_new_event();
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, TG_ARGV[0], new.parent
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS ua
WHERE ua.event_id = eid AND ua.entity_type = TG_ARGV[0] AND ua.entity_id = new.parent);
RETURN new;
END;'
LANGUAGE plpgsql;
#foreach($type in $types)
#if( ${type.annotated} && !${type.superclass})
CREATE TRIGGER ${type.tableName()}_annotation_link_update_trigger
AFTER UPDATE ON ${type.tableName()}annotationlink
FOR EACH ROW
EXECUTE PROCEDURE annotation_link_update_trigger('${type.id}');
CREATE TRIGGER ${type.tableName()}_annotation_link_insert_trigger
AFTER INSERT ON ${type.tableName()}annotationlink
FOR EACH ROW
EXECUTE PROCEDURE annotation_link_insert_trigger('${type.id}');
#end
#end
-- Delete triggers to go with update triggers (See #9337)
CREATE OR REPLACE FUNCTION annotation_link_delete_trigger() RETURNS "trigger"
AS '
DECLARE
eid int8;
BEGIN
SELECT INTO eid _current_or_new_event();
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, TG_ARGV[0], old.parent
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS ua
WHERE ua.event_id = eid AND ua.entity_type = TG_ARGV[0] AND ua.entity_id = old.parent);
RETURN old;
END;'
LANGUAGE plpgsql;
#foreach($type in $types)
#if( ${type.annotated} && !${type.superclass})
CREATE TRIGGER ${type.tableName()}_annotation_link_delete_trigger
BEFORE DELETE ON ${type.tableName()}annotationlink
FOR EACH ROW
EXECUTE PROCEDURE annotation_link_delete_trigger('${type.id}');
#end
#end
-- move update notes from updated_annotations to eventlog REINDEX entries
CREATE OR REPLACE FUNCTION updated_entities_note_reindex() RETURNS void AS $$
DECLARE
curs CURSOR FOR SELECT * FROM _reindexing_required ORDER BY event_id LIMIT 100000 FOR UPDATE;
row _reindexing_required%%rowtype;
BEGIN
FOR row IN curs
LOOP
DELETE FROM _reindexing_required WHERE CURRENT OF curs;
INSERT INTO eventlog (id, action, permissions, entityid, entitytype, event)
SELECT ome_nextval('seq_eventlog'), 'REINDEX', -52, row.entity_id, row.entity_type, row.event_id
WHERE NOT EXISTS (SELECT 1 FROM eventlog AS el
WHERE el.entityid = row.entity_id AND el.entitytype = row.entity_type AND el.event = row.event_id);
END LOOP;
END;
$$ LANGUAGE plpgsql;
--
-- END #1390
--
--
-- add reindexing triggers for ROI folders; see FullTextBridge.set_folders
--
CREATE FUNCTION folder_update_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
FOR iid IN SELECT DISTINCT r.image FROM roi AS r, folderroilink AS l
WHERE l.parent = NEW.id AND l.child = r.id AND r.image IS NOT NULL LOOP
IF eid IS NULL THEN
eid := _current_or_new_event();
END IF;
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END LOOP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION folder_roi_link_insert_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
FOR iid IN SELECT DISTINCT r.image FROM roi AS r
WHERE r.id = NEW.child AND r.image IS NOT NULL LOOP
IF eid IS NULL THEN
eid := _current_or_new_event();
END IF;
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END LOOP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION folder_roi_link_update_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
FOR iid IN SELECT DISTINCT r.image FROM roi AS r
WHERE r.id IN (OLD.child, NEW.child) AND r.image IS NOT NULL LOOP
IF eid IS NULL THEN
eid := _current_or_new_event();
END IF;
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END LOOP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION folder_roi_link_delete_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
FOR iid IN SELECT DISTINCT r.image FROM roi AS r
WHERE r.id = OLD.child AND r.image IS NOT NULL LOOP
IF eid IS NULL THEN
eid := _current_or_new_event();
END IF;
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END LOOP;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION roi_insert_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
iid := NEW.image;
IF iid IS NOT NULL THEN
eid := _current_or_new_event();
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION roi_update_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
IF OLD.image = NEW.image THEN
RETURN NEW;
END IF;
iid := OLD.image;
IF iid IS NOT NULL THEN
eid := _current_or_new_event();
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END IF;
iid := NEW.image;
IF iid IS NOT NULL THEN
IF eid IS NULL THEN
eid := _current_or_new_event();
END IF;
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION roi_delete_trigger() RETURNS TRIGGER AS $$
DECLARE
eid BIGINT;
iid BIGINT;
BEGIN
iid := OLD.image;
IF iid IS NOT NULL THEN
eid := _current_or_new_event();
INSERT INTO _reindexing_required (event_id, entity_type, entity_id)
SELECT eid, 'ome.model.core.Image', iid
WHERE NOT EXISTS (SELECT 1 FROM _reindexing_required AS rr
WHERE rr.event_id = eid AND rr.entity_type = 'ome.model.core.Image' AND rr.entity_id = iid);
END IF;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER folder_update_trigger
AFTER UPDATE ON folder
FOR EACH ROW
EXECUTE PROCEDURE folder_update_trigger();
CREATE TRIGGER folder_roi_link_insert_trigger
AFTER INSERT ON folderroilink
FOR EACH ROW
EXECUTE PROCEDURE folder_roi_link_insert_trigger();
CREATE TRIGGER folder_roi_link_update_trigger
AFTER UPDATE ON folderroilink
FOR EACH ROW
EXECUTE PROCEDURE folder_roi_link_update_trigger();
CREATE TRIGGER folder_roi_link_delete_trigger
AFTER DELETE ON folderroilink
FOR EACH ROW
EXECUTE PROCEDURE folder_roi_link_delete_trigger();
CREATE TRIGGER roi_insert_trigger
AFTER INSERT ON roi
FOR EACH ROW
EXECUTE PROCEDURE roi_insert_trigger();
CREATE TRIGGER roi_update_trigger
AFTER UPDATE ON roi
FOR EACH ROW
EXECUTE PROCEDURE roi_update_trigger();
CREATE TRIGGER roi_delete_trigger
AFTER DELETE ON roi
FOR EACH ROW
EXECUTE PROCEDURE roi_delete_trigger();
--
-- #12317 -- delete map property values along with their holders
--
#foreach($type in $types)
#foreach($prop in $type.classProperties)
#if( $prop.class.name == "ome.dsl.MapField" )
CREATE FUNCTION ${type.tableName()}_${prop.name}_map_entry_delete_trigger_function() RETURNS "trigger" AS '
BEGIN
DELETE FROM ${type.tableName()}_${prop.name}
#if( $type.superclass && !$type.discriminator )
WHERE ${type.tableName()}_id = OLD.${type.typeToColumn($type.superclass)}_id;
#else
WHERE ${type.tableName()}_id = OLD.id;
#end
RETURN OLD;
END;'
LANGUAGE plpgsql;
CREATE TRIGGER ${type.tableName()}_${prop.name}_map_entry_delete_trigger
BEFORE DELETE ON ${type.tableName()}
FOR EACH ROW
EXECUTE PROCEDURE ${type.tableName()}_${prop.name}_map_entry_delete_trigger_function();
#end
#end
#end
--
-- done #12317
--
-- First, we install a trigger to ensure users may go
-- from versionA/patchA to versionB/patchB once only.
--
CREATE FUNCTION dbpatch_versions_trigger_function() RETURNS TRIGGER AS $$
BEGIN
IF (NEW.currentversion <> NEW.previousversion OR NEW.currentpatch <> NEW.previouspatch) AND
(SELECT COUNT(*) FROM dbpatch WHERE id <> NEW.id AND
(currentversion <> previousversion OR currentpatch <> previouspatch) AND
((currentversion = NEW.currentversion AND currentpatch = NEW.currentpatch) OR
(previousversion = NEW.previousversion AND previouspatch = NEW.previouspatch))) > 0 THEN
RAISE 'upgrades cannot be repeated';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER dbpatch_versions_trigger
BEFORE INSERT OR UPDATE ON dbpatch
FOR EACH ROW
EXECUTE PROCEDURE dbpatch_versions_trigger_function();
--
-- Since this is a table that we will be using in DB-specific ways, we're also going
-- to make working with it a bit simpler.
--
alter table dbpatch alter id set default ome_nextval('seq_dbpatch');
alter table dbpatch alter permissions set default -52;
alter table dbpatch alter message set default 'Updating';
--
-- Then, we insert into the patch table the patch (initialization) which we are currently
-- running so that if anything goes wrong, we'll have some record.
--
insert into dbpatch (currentVersion, currentPatch, previousVersion, previousPatch, message)
values ('@DBVERSION@', @DBPATCH@, '@DBVERSION@', 0, 'Initializing');
--
-- Temporarily make event columns nullable; restored below.
--
alter table event alter column "type" drop not null;
alter table event alter column experimentergroup drop not null;
--
-- Here we will create the root account and the necessary groups
--
alter table experimenter alter ldap set default false;
alter table experimentergroup alter ldap set default false;
insert into experimenter (id,permissions,version,omename,firstname,lastname)
values (0,0,0,'root','root','root');
insert into experimenter (id,permissions,version,omename,firstname,lastname)
values (ome_nextval('seq_experimenter'),0,0,'guest','Guest','Account');
insert into node
(id,permissions,uuid,conn,up,down)
select 0,-52,'000000000000000000000000000000000000','unkclock_timestampn',clock_timestamp(),clock_timestamp();
insert into session
(id,permissions,timetoidle,timetolive,started,closed,defaulteventtype,uuid,owner,node)
select 0,-52,0,0,clock_timestamp(),clock_timestamp(),'BOOTSTRAP',0000, 0,0;
insert into session
(id,permissions,timetoidle,timetolive,started,closed,defaulteventtype,uuid,owner,node)
select ome_nextval('seq_session'),-52, 0,0,clock_timestamp(),clock_timestamp(),'PREVIOUSITEMS','1111',0,0;
insert into event (id,permissions,time,status,experimenter,session) values (0,0,clock_timestamp(),'BOOTSTRAP',0,0);
--
-- Default group permissions (ticket:1783)
-- * "system" is private, so that administrators do not share by accident
-- * "user" is public by default since its purpose is to share objects (ticket:1794)
-- * "guest" is private by default so that it doesn't show up on any lists,
-- though nothing should be created there.
--
insert into experimentergroup (id,permissions,version,name)
values (0,-120,0,'system');
insert into experimentergroup (id,permissions,version,name)
values (ome_nextval('seq_experimentergroup'),-52,0,'user');
insert into experimentergroup (id,permissions,version,name)
values (ome_nextval('seq_experimentergroup'),-120,0,'guest');
insert into eventtype (id,permissions,value) values
(0,-52,'Bootstrap');
insert into groupexperimentermap
(id,permissions,version, parent, child, child_index,owner)
values
(0,-52,0,0,0,0,true);
insert into groupexperimentermap
(id,permissions,version, parent, child, child_index,owner)
select ome_nextval('seq_groupexperimentermap'),-52,0,1,0,1,false;
insert into groupexperimentermap
(id,permissions,version, parent, child, child_index,owner)
select ome_nextval('seq_groupexperimentermap'),-52,0,2,1,0,false;
update event set type = 0;
update event set experimentergroup = 0;
alter table event alter column type set not null;
alter table event alter column experimentergroup set not null;
##insert into event (id,version,name,e) select next, 0 as version, 'Bootstrap'
## as name, next from (select ome_nextval('seq_event') as next) as nv;
-- temporarily disable the not null constraints
alter table pixelstype alter column bitsize drop not null;
#foreach($enum in $types)
#foreach($prop in $enum.properties)
#set($table = ${enum.typeToColumn($enum.id)} )
#if($prop.class.name == "ome.dsl.EntryField")
insert into $table (id,permissions,value)
select ome_nextval('seq_$table'),-52,'$prop.name';
#end
#end
#end
-- Adding bit depth to pixelstype (#2724)
update pixelstype set bitsize = 1 where value = 'bit';
update pixelstype set bitsize = 8 where value = 'int8';
update pixelstype set bitsize = 8 where value = 'uint8';
update pixelstype set bitsize = 16 where value = 'int16';
update pixelstype set bitsize = 16 where value = 'uint16';
update pixelstype set bitsize = 32 where value = 'int32';
update pixelstype set bitsize = 32 where value = 'uint32';
update pixelstype set bitsize = 32 where value = 'float';
update pixelstype set bitsize = 64 where value = 'double';
update pixelstype set bitsize = 64 where value = 'complex';
update pixelstype set bitsize = 128 where value = 'double-complex';
-- reactivate not null constraints
alter table pixelstype alter column bitsize set not null;
--
-- Cryptographic functions for specifying UUID
--
create or replace function uuid() returns character(36)
as '
select substring(x.my_rand from 1 for 8)||''-''||
substring(x.my_rand from 9 for 4)||''-4''||
substring(x.my_rand from 13 for 3)||''-''||x.clock_1||
substring(x.my_rand from 16 for 3)||''-''||
substring(x.my_rand from 19 for 12)
from
(select md5(clock_timestamp()::text||random()) as my_rand, to_hex(8+(3*random())::int) as clock_1) as x;'
language sql;
--
-- Configuration table including a UUID uniquely identifying this database.
--
create table configuration ( name varchar(255) primary key, value text );
insert into configuration values ('omero.db.uuid',uuid());
--
-- ticket:2201 - creating repository data structures
--
alter table pixels add column path text;
alter table pixels add column name text;
alter table pixels add column repo varchar(36);
create index pixels_repo_index on pixels (repo);
-- No unique index on (path, repo, name) since it depends on params
alter table originalfile alter column mimetype set default 'application/octet-stream';
alter table originalfile alter column repo type varchar(36);
create index originalfile_mime_index on originalfile (mimetype);
create index originalfile_path_index on originalfile (path);
create index originalfile_repo_index on originalfile (repo);
create index originalfile_hash_index on originalfile (hash);
-- protect against 2017-SV5
CREATE UNIQUE INDEX originalfile_repo_path_index ON originalfile
(repo, regexp_split_to_array('/' || path || name || '/', '/+'))
WHERE repo IS NOT NULL;
--
-- end ticket:2201
--
-- ticket:11591 Shrink the userIP column down to maximum (IPv4-mapped IPv6)
alter table session alter column userIP TYPE varchar(45);
-- Indices. See #1640, #2573, etc.
create unique index namespace_name on namespace (name);
create unique index well_col_row on well (plate, "column", "row");
create index eventlog_entitytype on eventlog(entitytype);
create index eventlog_entityid on eventlog(entityid);
create index eventlog_action on eventlog(action);
create index annotation_discriminator on annotation(discriminator);
create index annotation_ns on annotation(ns);
#foreach($type in $types)
#foreach($prop in $type.classProperties)
#if( $prop.class.name == "ome.dsl.MapField" )
CREATE INDEX ${type.tableName()}_${prop.name}_name ON ${type.tableName()}_${prop.name}(name);
CREATE INDEX ${type.tableName()}_${prop.name}_value ON ${type.tableName()}_${prop.name}(value);
#end
#end
#end
create table password ( experimenter_id bigint primary key REFERENCES experimenter (id), hash VARCHAR(255),
changed TIMESTAMP WITHOUT TIME ZONE);
insert into password values (0,'@ROOTPASS@');
insert into password values (1,'');
-- root can now login with omero.rootpass property value
-- and guest can login with any value
-- use a table to note the role IDs explicitly in the database
CREATE TABLE _roles (
root_user_id BIGINT NOT NULL,
guest_user_id BIGINT NOT NULL,
system_group_id BIGINT NOT NULL,
user_group_id BIGINT NOT NULL,
guest_group_id BIGINT NOT NULL
);
INSERT INTO _roles (root_user_id, guest_user_id, system_group_id, user_group_id, guest_group_id)
VALUES (0, 1, 0, 1, 2);
--
-- FS Resources
--
-- Prevent the deletion of mimetype = "Directory" objects
create or replace function _fs_dir_delete() returns trigger AS $_fs_dir_delete$
begin
--
-- If any children are found, prevent deletion
--
if OLD.mimetype = 'Directory' and exists(
select id from originalfile
where repo = OLD.repo and path = OLD.path || OLD.name || '/'
limit 1) then
-- CANCEL DELETE
RAISE EXCEPTION '%%', 'Directory('||OLD.id||')='||OLD.path||OLD.name||'/ is not empty!';
end if;
return OLD; -- proceed
end;
$_fs_dir_delete$ LANGUAGE plpgsql;
create trigger _fs_dir_delete
before delete on originalfile
for each row execute procedure _fs_dir_delete();
-- Prevent Directory and Repository entries in the originalfile table from having their mimetype changed.
CREATE OR REPLACE FUNCTION _fs_protected_mimetype() RETURNS "trigger" AS $$
BEGIN
IF OLD.mimetype IN ('Directory', 'Repository') AND (NEW.mimetype IS NULL OR NEW.mimetype != OLD.mimetype) THEN
RAISE EXCEPTION 'cannot change media type %% of file id=%%', OLD.mimetype, OLD.id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER _fs_protected_mimetype
BEFORE UPDATE ON originalfile
FOR EACH ROW EXECUTE PROCEDURE _fs_protected_mimetype();
-- Prevent SQL DELETE from removing the root experimenter from the system or user group.
CREATE FUNCTION prevent_root_deactivate_delete() RETURNS "trigger" AS $$
DECLARE
roles _roles%%ROWTYPE;
BEGIN
SELECT * INTO STRICT roles FROM _roles;
IF OLD.child = roles.root_user_id THEN
IF OLD.parent = roles.system_group_id THEN
RAISE EXCEPTION 'cannot remove system group membership for root';
ELSIF OLD.parent = roles.user_group_id THEN
RAISE EXCEPTION 'cannot remove user group membership for root';
END IF;
END IF;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER prevent_root_deactivate_delete
BEFORE DELETE ON groupexperimentermap
FOR EACH ROW EXECUTE PROCEDURE prevent_root_deactivate_delete();
-- Prevent SQL UPDATE from removing the root experimenter from the system or user group.
CREATE FUNCTION prevent_root_deactivate_update() RETURNS "trigger" AS $$
DECLARE
roles _roles%%ROWTYPE;
BEGIN
SELECT * INTO STRICT roles FROM _roles;
IF OLD.child != NEW.child OR OLD.parent != NEW.parent THEN
IF OLD.child = roles.root_user_id THEN
IF OLD.parent = roles.system_group_id THEN
RAISE EXCEPTION 'cannot remove system group membership for root';
ELSIF OLD.parent = roles.user_group_id THEN
RAISE EXCEPTION 'cannot remove user group membership for root';
END IF;
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER prevent_root_deactivate_update
BEFORE UPDATE ON groupexperimentermap
FOR EACH ROW EXECUTE PROCEDURE prevent_root_deactivate_update();
-- Prevent the root and guest experimenters from being renamed.
CREATE FUNCTION prevent_experimenter_rename() RETURNS "trigger" AS $$
DECLARE
roles _roles%%ROWTYPE;
BEGIN
SELECT * INTO STRICT roles FROM _roles;
IF OLD.omename != NEW.omename THEN
IF OLD.id = roles.root_user_id THEN
RAISE EXCEPTION 'cannot rename root experimenter';
ELSIF OLD.id = roles.guest_user_id THEN
RAISE EXCEPTION 'cannot rename guest experimenter';
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER prevent_experimenter_rename
BEFORE UPDATE ON experimenter
FOR EACH ROW EXECUTE PROCEDURE prevent_experimenter_rename();
-- Prevent the system, user and guest groups from being renamed.
CREATE FUNCTION prevent_experimenter_group_rename() RETURNS "trigger" AS $$
DECLARE
roles _roles%%ROWTYPE;
BEGIN
SELECT * INTO STRICT roles FROM _roles;
IF OLD.name != NEW.name THEN
IF OLD.id = roles.system_group_id THEN
RAISE EXCEPTION 'cannot rename system experimenter group';
ELSIF OLD.id = roles.user_group_id THEN
RAISE EXCEPTION 'cannot rename user experimenter group';
ELSIF OLD.id = roles.guest_group_id THEN
RAISE EXCEPTION 'cannot rename guest experimenter group';
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER prevent_experimenter_group_rename
BEFORE UPDATE ON experimentergroup
FOR EACH ROW EXECUTE PROCEDURE prevent_experimenter_group_rename();
-- Prevent light administrator privileges from restricting the root experimenter.
CREATE FUNCTION prevent_root_privilege_restriction() RETURNS "trigger" AS $$
DECLARE
roles _roles%%ROWTYPE;
BEGIN
SELECT * INTO STRICT roles FROM _roles;
IF NEW.experimenter_id = roles.root_user_id AND NEW.name LIKE 'AdminPrivilege:%%' AND NEW.value NOT ILIKE 'true' THEN
RAISE EXCEPTION 'cannot restrict admin privileges of root experimenter';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;