-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathtest_sql_functions.sql
5994 lines (5478 loc) · 201 KB
/
test_sql_functions.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
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
DROP EXTENSION IF EXISTS pgtap CASCADE;
CREATE EXTENSION IF NOT EXISTS pgtap;
-- msar.drop_columns -------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION __setup_drop_columns() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE atable (dodrop1 integer, dodrop2 integer, dontdrop text);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_columns_oid() RETURNS SETOF TEXT AS $$
DECLARE
rel_id oid;
BEGIN
PERFORM __setup_drop_columns();
rel_id := 'atable'::regclass::oid;
PERFORM msar.drop_columns(rel_id, 1, 2);
RETURN NEXT has_column(
'atable', 'dontdrop', 'Keeps correct columns'
);
RETURN NEXT hasnt_column(
'atable', 'dodrop1', 'Drops correct columns 1'
);
RETURN NEXT hasnt_column(
'atable', 'dodrop2', 'Drops correct columns 2'
);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_columns_ne_oid() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE "12345" (bleh text, bleh2 numeric);
PERFORM msar.drop_columns(12345, 1);
RETURN NEXT has_column(
'12345', 'bleh', 'Doesn''t drop columns of stupidly-named table'
);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_columns_names() RETURNS SETOF TEXT AS $$
BEGIN
PERFORM __setup_drop_columns();
PERFORM msar.drop_columns('public', 'atable', 'dodrop1', 'dodrop2');
RETURN NEXT has_column(
'atable', 'dontdrop', 'Dropper keeps correct columns'
);
RETURN NEXT hasnt_column(
'atable', 'dodrop1', 'Dropper drops correct columns 1'
);
RETURN NEXT hasnt_column(
'atable', 'dodrop2', 'Dropper drops correct columns 2'
);
END;
$$ LANGUAGE plpgsql;
-- msar.drop_table ---------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION __setup_drop_tables() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE dropme (id SERIAL PRIMARY KEY, col1 integer);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_table_oid() RETURNS SETOF TEXT AS $$
DECLARE
rel_id oid;
BEGIN
PERFORM __setup_drop_tables();
rel_id := 'dropme'::regclass::oid;
PERFORM msar.drop_table(tab_id => rel_id, cascade_ => false);
RETURN NEXT hasnt_table('dropme', 'Drops table');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_table_oid_restricted_fkey() RETURNS SETOF TEXT AS $$
DECLARE
rel_id oid;
BEGIN
PERFORM __setup_drop_tables();
rel_id := 'dropme'::regclass::oid;
CREATE TABLE
dependent (id SERIAL PRIMARY KEY, col1 integer REFERENCES dropme);
RETURN NEXT throws_ok(
format('SELECT msar.drop_table(tab_id => %s, cascade_ => false);', rel_id),
'2BP01',
'cannot drop table dropme because other objects depend on it',
'Table dropper throws for dependent objects'
);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_table_oid_cascade_fkey() RETURNS SETOF TEXT AS $$
DECLARE
rel_id oid;
BEGIN
PERFORM __setup_drop_tables();
rel_id := 'dropme'::regclass::oid;
CREATE TABLE
dependent (id SERIAL PRIMARY KEY, col1 integer REFERENCES dropme);
PERFORM msar.drop_table(tab_id => rel_id, cascade_ => true);
RETURN NEXT hasnt_table('dropme', 'Drops table with dependent using CASCADE');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_table_name() RETURNS SETOF TEXT AS $$
BEGIN
PERFORM __setup_drop_tables();
PERFORM msar.drop_table(
sch_name => 'public',
tab_name => 'dropme',
cascade_ => false,
if_exists => false
);
RETURN NEXT hasnt_table('dropme', 'Drops table');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_table_name_missing_if_exists() RETURNS SETOF TEXT AS $$
BEGIN
PERFORM __setup_drop_tables();
PERFORM msar.drop_table(
sch_name => 'public',
tab_name => 'dropmenew',
cascade_ => false,
if_exists => true
);
RETURN NEXT has_table('dropme', 'Drops table with IF EXISTS');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_drop_table_name_missing_no_if_exists() RETURNS SETOF TEXT AS $$
BEGIN
RETURN NEXT throws_ok(
'SELECT msar.drop_table(''public'', ''doesntexist'', false, false);',
'42P01',
'table "doesntexist" does not exist',
'Table dropper throws for missing table'
);
END;
$$ LANGUAGE plpgsql;
-- msar.build_type_text ----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION test_build_type_text() RETURNS SETOF TEXT AS $$/*
Note that many type building tests are in the column adding section, to make sure the strings the
function writes are as expected, and also valid type definitions.
*/
BEGIN
RETURN NEXT is(msar.build_type_text('{}'), 'text');
RETURN NEXT is(msar.build_type_text(null), 'text');
RETURN NEXT is(msar.build_type_text('{"name": "varchar"}'), 'character varying');
CREATE DOMAIN msar.testtype AS text CHECK (value LIKE '%test');
RETURN NEXT is(
msar.build_type_text('{"schema": "msar", "name": "testtype"}'), 'msar.testtype'
);
END;
$$ LANGUAGE plpgsql;
-- __msar.process_col_def_jsonb ----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION test_process_col_def_jsonb() RETURNS SETOF TEXT AS $f$
BEGIN
RETURN NEXT is(
__msar.process_col_def_jsonb(0, '[{}, {}]'::jsonb, false),
ARRAY[
('"Column 1"', 'text', null, null, false, null),
('"Column 2"', 'text', null, null, false, null)
]::__msar.col_def[],
'Empty columns should result in defaults'
);
RETURN NEXT is(
__msar.process_col_def_jsonb(0, '[{"name": "id"}]'::jsonb, false),
null,
'Column definition processing should ignore "id" column'
);
RETURN NEXT is(
__msar.process_col_def_jsonb(0, '[{}, {}]'::jsonb, false, true),
ARRAY[
('id', 'integer', true, null, true, 'Mathesar default ID column'),
('"Column 1"', 'text', null, null, false, null),
('"Column 2"', 'text', null, null, false, null)
]::__msar.col_def[],
'Column definition processing add "id" column'
);
RETURN NEXT is(
__msar.process_col_def_jsonb(0, '[{"description": "Some comment"}]'::jsonb, false),
ARRAY[
('"Column 1"', 'text', null, null, false, '''Some comment''')
]::__msar.col_def[],
'Comments should be sanitized'
);
END;
$f$ LANGUAGE plpgsql;
-- msar.add_columns --------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION __setup_add_columns() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE add_col_testable (id serial primary key, col1 integer, col2 varchar);
END;
$$ LANGUAGE plpgsql;
-- TODO: Figure out a way to parameterize these
CREATE OR REPLACE FUNCTION test_add_columns_fullspec_text() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := $j$[
{"name": "tcol", "type": {"name": "text"}, "not_null": true, "default": "my super default"}
]$j$;
BEGIN
PERFORM __setup_add_columns();
RETURN NEXT is(
msar.add_columns('add_col_testable'::regclass::oid, col_create_arr), '{4}'::smallint[]
);
RETURN NEXT col_not_null('add_col_testable', 'tcol');
RETURN NEXT col_type_is('add_col_testable', 'tcol', 'text');
RETURN NEXT col_default_is('add_col_testable', 'tcol', 'my super default');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_minspec_text() RETURNS SETOF TEXT AS $f$
/*
This tests the default settings. When not given, the defautl column should be nullable and have no
default value. The name should be "Column <n>", where <n> is the attnum of the added column.
*/
DECLARE
col_create_arr jsonb := '[{"type": {"name": "text"}}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_is_null('add_col_testable', 'Column 4');
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'text');
RETURN NEXT col_hasnt_default('add_col_testable', 'Column 4');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_comment() RETURNS SETOF TEXT AS $f$
DECLARE
col_name text := 'tcol';
description text := 'Some; comment with a semicolon';
tab_id integer;
col_id integer;
col_create_arr jsonb;
BEGIN
PERFORM __setup_add_columns();
tab_id := 'add_col_testable'::regclass::oid;
col_create_arr := format('[{"name": "%s", "description": "%s"}]', col_name, description);
PERFORM msar.add_columns(tab_id, col_create_arr);
col_id := msar.get_attnum(tab_id, col_name);
RETURN NEXT is(
msar.col_description(tab_id, col_id),
description
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_multi_default_name() RETURNS SETOF TEXT AS $f$
/*
This tests the default settings. When not given, the defautl column should be nullable and have no
default value. The name should be "Column <n>", where <n> is the attnum of the added column.
*/
DECLARE
col_create_arr jsonb := '[{"type": {"name": "text"}}, {"type": {"name": "numeric"}}]';
BEGIN
PERFORM __setup_add_columns();
RETURN NEXT is(
msar.add_columns('add_col_testable'::regclass::oid, col_create_arr), '{4, 5}'::smallint[]
);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'text');
RETURN NEXT col_type_is('add_col_testable', 'Column 5', 'numeric');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_numeric_def() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := '[{"type": {"name": "numeric"}, "default": 3.14159}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'numeric');
RETURN NEXT col_default_is('add_col_testable', 'Column 4', 3.14159);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_numeric_prec() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := '[{"type": {"name": "numeric", "options": {"precision": 3}}}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'numeric(3,0)');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_numeric_prec_scale() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := $j$[
{"type": {"name": "numeric", "options": {"precision": 3, "scale": 2}}}
]$j$;
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'numeric(3,2)');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_caps_numeric() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := '[{"type": {"name": "NUMERIC"}}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'numeric');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_varchar_length() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := '[{"type": {"name": "varchar", "options": {"length": 128}}}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'character varying(128)');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_interval_precision() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := '[{"type": {"name": "interval", "options": {"precision": 6}}}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'interval(6)');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_interval_fields() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := '[{"type": {"name": "interval", "options": {"fields": "year"}}}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'interval year');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_interval_fields_prec() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := $j$
[{"type": {"name": "interval", "options": {"fields": "second", "precision": 3}}}]
$j$;
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'interval second(3)');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_timestamp_prec() RETURNS SETOF TEXT AS $f$
DECLARE
col_create_arr jsonb := $j$
[{"type": {"name": "timestamp", "options": {"precision": 3}}}]
$j$;
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'timestamp(3) without time zone');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_timestamp_raw_default() RETURNS SETOF TEXT AS $f$
/*
This test will fail if the default is being sanitized, but will succeed if it's not.
*/
DECLARE
col_create_arr jsonb := '[{"type": {"name": "timestamp"}, "default": "now()::timestamp"}]';
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr, raw_default => true);
RETURN NEXT col_type_is('add_col_testable', 'Column 4', 'timestamp without time zone');
RETURN NEXT col_default_is(
'add_col_testable', 'Column 4', '(now())::timestamp without time zone'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_sanitize_default() RETURNS SETOF TEXT AS $f$
/*
This test will succeed if the default is being sanitized, but will fail if it's not.
It's important to check that we're careful with SQL submitted from python.
*/
DECLARE
col_create_arr jsonb := $j$
[{"type": {"name": "text"}, "default": "null; drop table add_col_testable"}]
$j$;
BEGIN
PERFORM __setup_add_columns();
PERFORM msar.add_columns('add_col_testable'::regclass::oid, col_create_arr, raw_default => false);
RETURN NEXT has_table('add_col_testable');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_columns_errors() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_add_columns();
RETURN NEXT throws_ok(
format(
'SELECT msar.add_columns(tab_id => %s, col_defs => ''%s'');',
'add_col_testable'::regclass::oid,
'[{"type": {"name": "taxt"}}]'::jsonb
),
'42704',
'type "taxt" does not exist'
);
RETURN NEXT CASE WHEN pg_version_num() < 150000
THEN throws_ok(
format(
'SELECT msar.add_columns(tab_id => %s, col_defs => ''%s'');',
'add_col_testable'::regclass::oid,
'[{"type": {"name": "numeric", "options": {"scale": 23, "precision": 3}}}]'::jsonb
),
'22023',
'NUMERIC scale 23 must be between 0 and precision 3'
)
ELSE skip('Numeric scale can be negative or greater than precision as of v15')
END;
END;
$f$ LANGUAGE plpgsql;
-- msar.copy_column --------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION __setup_copy_column() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE copy_coltest (
id SERIAL PRIMARY KEY,
col1 varchar,
col2 varchar NOT NULL,
col3 numeric(5, 3) DEFAULT 5,
col4 timestamp without time zone DEFAULT NOW(),
col5 timestamp without time zone NOT NULL DEFAULT NOW(),
col6 interval second(3),
"col space" varchar
);
ALTER TABLE copy_coltest ADD UNIQUE (col1, col2);
INSERT INTO copy_coltest VALUES
(DEFAULT, 'abc', 'def', 5.234, '1999-01-08 04:05:06', '1999-01-09 04:05:06', '4:05:06', 'ghi'),
(DEFAULT, 'jkl', 'mno', null, null, '1999-02-08 04:05:06', '3 4:05:07', 'pqr'),
(DEFAULT, null, 'stu', DEFAULT, DEFAULT, DEFAULT, null, 'vwx')
;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_copies_unique() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 2::smallint, 'col1 supercopy', true, true
);
RETURN NEXT col_type_is('copy_coltest', 'col1 supercopy', 'character varying');
RETURN NEXT col_is_null('copy_coltest', 'col1 supercopy');
RETURN NEXT col_is_unique('copy_coltest', ARRAY['col1', 'col2']);
RETURN NEXT col_is_unique('copy_coltest', ARRAY['col1 supercopy', 'col2']);
RETURN NEXT results_eq(
'SELECT "col1 supercopy" FROM copy_coltest ORDER BY id',
$v$VALUES ('abc'::varchar), ('jkl'::varchar), (null)$v$
);
RETURN NEXT lives_ok(
$u$UPDATE copy_coltest SET "col1 supercopy"='abc' WHERE "col1 supercopy"='jkl'$u$,
'Copied col should not have a single column unique constraint'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_copies_unique_and_nnull() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 3::smallint, null, true, true
);
RETURN NEXT col_type_is('copy_coltest', 'col2 1', 'character varying');
RETURN NEXT col_not_null('copy_coltest', 'col2 1');
RETURN NEXT col_is_unique('copy_coltest', ARRAY['col1', 'col2']);
RETURN NEXT col_is_unique('copy_coltest', ARRAY['col1', 'col2 1']);
RETURN NEXT results_eq(
'SELECT "col2 1" FROM copy_coltest',
$v$VALUES ('def'::varchar), ('mno'::varchar), ('stu'::varchar)$v$
);
RETURN NEXT lives_ok(
$u$UPDATE copy_coltest SET "col2 1"='def' WHERE "col2 1"='mno'$u$,
'Copied col should not have a single column unique constraint'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_false_copy_data_and_con() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 3::smallint, null, false, false
);
RETURN NEXT col_type_is('copy_coltest', 'col2 1', 'character varying');
RETURN NEXT col_is_null('copy_coltest', 'col2 1');
RETURN NEXT col_is_unique('copy_coltest', ARRAY['col1', 'col2']);
RETURN NEXT results_eq(
'SELECT "col2 1" FROM copy_coltest',
$v$VALUES (null::varchar), (null::varchar), (null::varchar)$v$
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_num_options_static_default() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 4::smallint, null, true, false
);
RETURN NEXT col_type_is('copy_coltest', 'col3 1', 'numeric(5,3)');
RETURN NEXT col_is_null('copy_coltest', 'col3 1');
RETURN NEXT col_default_is('copy_coltest', 'col3 1', '5');
RETURN NEXT results_eq(
'SELECT "col3 1" FROM copy_coltest',
$v$VALUES (5.234), (null), (5)$v$
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_nullable_dynamic_default() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 5::smallint, null, true, false
);
RETURN NEXT col_type_is('copy_coltest', 'col4 1', 'timestamp without time zone');
RETURN NEXT col_is_null('copy_coltest', 'col4 1');
RETURN NEXT col_default_is('copy_coltest', 'col4 1', 'now()');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_non_null_dynamic_default() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 6::smallint, null, true, true
);
RETURN NEXT col_type_is('copy_coltest', 'col5 1', 'timestamp without time zone');
RETURN NEXT col_not_null('copy_coltest', 'col5 1');
RETURN NEXT col_default_is('copy_coltest', 'col5 1', 'now()');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_interval_notation() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 7::smallint, null, false, false
);
RETURN NEXT col_type_is('copy_coltest', 'col6 1', 'interval second(3)');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_space_name() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 8::smallint, null, false, false
);
RETURN NEXT col_type_is('copy_coltest', 'col space 1', 'character varying');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_pkey() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 1::smallint, null, true, true
);
RETURN NEXT col_type_is('copy_coltest', 'id 1', 'integer');
RETURN NEXT col_not_null('copy_coltest', 'id 1');
RETURN NEXT col_default_is(
'copy_coltest', 'id 1', $d$nextval('copy_coltest_id_seq'::regclass)$d$
);
RETURN NEXT col_is_pk('copy_coltest', 'id');
RETURN NEXT col_isnt_pk('copy_coltest', 'id 1');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_column_increment_name() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM __setup_copy_column();
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 2::smallint, null, true, true
);
RETURN NEXT has_column('copy_coltest', 'col1 1');
PERFORM msar.copy_column(
'copy_coltest'::regclass::oid, 2::smallint, null, true, true
);
RETURN NEXT has_column('copy_coltest', 'col1 2');
END;
$f$ LANGUAGE plpgsql;
-- msar.add_constraints ----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION __setup_add_pkey() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE add_pkeytest (col1 serial, col2 serial, col3 text);
INSERT INTO add_pkeytest (col1, col2, col3) VALUES
(DEFAULT, DEFAULT, 'abc'),
(DEFAULT, DEFAULT, 'def'),
(DEFAULT, DEFAULT, 'abc'),
(DEFAULT, DEFAULT, 'def'),
(DEFAULT, DEFAULT, 'abc'),
(DEFAULT, DEFAULT, 'def'),
(DEFAULT, DEFAULT, 'abc');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_id_fullspec() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := $j$[
{"name": "mysuperkey", "type": "p", "columns": [1], "deferrable": true}
]$j$;
created_name text;
deferrable_ boolean;
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('add_pkeytest'::regclass::oid, con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', 'col1');
created_name := conname FROM pg_constraint
WHERE conrelid='add_pkeytest'::regclass::oid AND conkey='{1}';
RETURN NEXT is(created_name, 'mysuperkey');
deferrable_ := condeferrable FROM pg_constraint WHERE conname='mysuperkey';
RETURN NEXT is(deferrable_, true);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_id_defname() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": [1]}]';
created_name text;
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('add_pkeytest'::regclass::oid, con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', 'col1');
created_name := conname FROM pg_constraint
WHERE conrelid='add_pkeytest'::regclass::oid AND conkey='{1}';
RETURN NEXT is(created_name, 'add_pkeytest_pkey');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_id_multicol() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": [1, 2]}]';
created_name text;
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('add_pkeytest'::regclass::oid, con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', ARRAY['col1', 'col2']);
created_name := conname FROM pg_constraint
WHERE conrelid='add_pkeytest'::regclass::oid AND conkey='{1, 2}';
RETURN NEXT is(created_name, 'add_pkeytest_pkey');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_tab_name_singlecol() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": [1]}]';
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('public', 'add_pkeytest', con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', 'col1');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_col_name_singlecol() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": ["col1"]}]';
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('add_pkeytest'::regclass::oid, con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', 'col1');
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_col_name_multicol() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": ["col1", "col2"]}]';
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('add_pkeytest'::regclass::oid, con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', ARRAY['col1', 'col2']);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_pkey_col_mix_multicol() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": [1, "col2"]}]';
BEGIN
PERFORM __setup_add_pkey();
PERFORM msar.add_constraints('add_pkeytest'::regclass::oid, con_create_arr);
RETURN NEXT col_is_pk('add_pkeytest', ARRAY['col1', 'col2']);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION __setup_add_fkey() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE add_fk_users (id serial primary key, fname TEXT, lname TEXT, phoneno TEXT);
INSERT INTO add_fk_users (fname, lname, phoneno) VALUES
('alice', 'smith', '123 4567'),
('bob', 'jones', '234 5678'),
('eve', 'smith', '345 6789');
CREATE TABLE add_fk_comments (id serial primary key, user_id integer, comment text);
INSERT INTO add_fk_comments (user_id, comment) VALUES
(1, 'aslfkjasfdlkjasdfl'),
(2, 'aslfkjasfdlkjasfl'),
(3, 'aslfkjasfdlkjsfl'),
(1, 'aslfkjasfdlkasdfl'),
(2, 'aslfkjasfkjasdfl'),
(2, 'aslfkjasflkjasdfl'),
(3, 'aslfkjasfdjasdfl'),
(1, 'aslfkjasfkjasdfl'),
(1, 'fkjasfkjasdfl');
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_fkey_id_fullspec() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb;
BEGIN
PERFORM __setup_add_fkey();
con_create_arr := format(
$j$[
{
"name": "superfkey",
"type": "f",
"columns": [2],
"fkey_relation_id": %s,
"fkey_columns": [1],
"fkey_update_action": "a",
"fkey_delete_action": "a",
"fkey_match_type": "f"
}
]$j$, 'add_fk_users'::regclass::oid
);
PERFORM msar.add_constraints('add_fk_comments'::regclass::oid, con_create_arr);
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
RETURN NEXT results_eq(
$h$
SELECT conname, confupdtype, confdeltype, confmatchtype
FROM pg_constraint WHERE conname='superfkey'
$h$,
$w$VALUES ('superfkey'::name, 'a'::"char", 'a'::"char", 'f'::"char")$w$
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION fkey_options_eq("char", "char", "char") RETURNS TEXT AS $f$
DECLARE
con_create_arr jsonb;
BEGIN
PERFORM __setup_add_fkey();
con_create_arr := format(
$j$[
{
"name": "superfkey",
"type": "f",
"columns": [2],
"fkey_relation_id": %s,
"fkey_update_action": "%s",
"fkey_delete_action": "%s",
"fkey_match_type": "%s"
}
]$j$,
'add_fk_users'::regclass::oid, $1, $2, $3
);
PERFORM msar.add_constraints('add_fk_comments'::regclass::oid, con_create_arr);
RETURN results_eq(
$h$
SELECT conname, confupdtype, confdeltype, confmatchtype
FROM pg_constraint WHERE conname='superfkey'
$h$,
format(
$w$VALUES ('superfkey'::name, '%s'::"char", '%s'::"char", '%s'::"char")$w$,
$1, $2, $3
),
format('Should have confupdtype %s, confdeltype %s, and confmatchtype %s', $1, $2, $3)
);
END;
$f$ LANGUAGE plpgsql;
-- Options for fkey delete, update action and match type
-- a = no action, r = restrict, c = cascade, n = set null, d = set default
-- f = full, s = simple
-- Note that partial match is not implemented.
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_aas() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('a', 'a', 's');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_arf() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('a', 'r', 'f');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_rrf() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('r', 'r', 'f');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_rrf() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('r', 'r', 'f');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_ccf() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('c', 'c', 'f');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_nnf() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('n', 'n', 'f');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_fkey_opts_ddf() RETURNS SETOF TEXT AS $f$
BEGIN
PERFORM fkey_options_eq('d', 'd', 'f');
RETURN NEXT fk_ok(
'public', 'add_fk_comments', 'user_id', 'public', 'add_fk_users', 'id'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION __setup_add_unique() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE add_unique_con (id serial primary key, col1 integer, col2 integer, col3 integer);
INSERT INTO add_unique_con (col1, col2, col3) VALUES
(1, 1, 1),
(2, 2, 3),
(3, 3, 3);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_unique_single() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"name": "myuniqcons", "type": "u", "columns": [2]}]';
BEGIN
PERFORM __setup_add_unique();
PERFORM msar.add_constraints('add_unique_con'::regclass::oid, con_create_arr);
RETURN NEXT col_is_unique('add_unique_con', ARRAY['col1']);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraints_unique_multicol() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"name": "myuniqcons", "type": "u", "columns": [2, 3]}]';
BEGIN
PERFORM __setup_add_unique();
PERFORM msar.add_constraints('add_unique_con'::regclass::oid, con_create_arr);
RETURN NEXT col_is_unique('add_unique_con', ARRAY['col1', 'col2']);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_duplicate_name() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"name": "myuniqcons", "type": "u", "columns": [2]}]';
con_create_arr2 jsonb := '[{"name": "myuniqcons", "type": "u", "columns": [3]}]';
BEGIN
PERFORM __setup_add_unique();
PERFORM msar.add_constraints('add_unique_con'::regclass::oid, con_create_arr);
RETURN NEXT throws_ok(
format(
'SELECT msar.add_constraints(%s, ''%s'');', 'add_unique_con'::regclass::oid, con_create_arr
),
'42P07',
'relation "myuniqcons" already exists',
'Throws error for duplicate constraint name'
);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION __setup_copy_unique() RETURNS SETOF TEXT AS $$
BEGIN
CREATE TABLE copy_unique_con
(id serial primary key, col1 integer, col2 integer, col3 integer, col4 integer);
ALTER TABLE copy_unique_con ADD CONSTRAINT olduniqcon UNIQUE (col1, col2, col3);
INSERT INTO copy_unique_con (col1, col2, col3, col4) VALUES
(1, 2, 5, 9),
(2, 3, 6, 0),
(3, 4, 8, 1);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_copy_constraint() RETURNS SETOF TEXT AS $f$
DECLARE
orig_oid oid;
BEGIN
PERFORM __setup_copy_unique();
orig_oid := oid
FROM pg_constraint
WHERE conrelid='copy_unique_con'::regclass::oid AND conname='olduniqcon';
PERFORM msar.copy_constraint(orig_oid, 4::smallint, 5::smallint);
RETURN NEXT col_is_unique('copy_unique_con', ARRAY['col1', 'col2', 'col4']);
END;
$f$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION test_add_constraint_errors() RETURNS SETOF TEXT AS $f$
DECLARE
con_create_arr jsonb := '[{"type": "p", "columns": [7]}]'::jsonb;