forked from CDLSoftware/pg-mv-fast-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pipeline_checks.sh
executable file
·793 lines (616 loc) · 21.2 KB
/
run_pipeline_checks.sh
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
#! /bin/bash
#File: run_pipeline_checks.sh
#Desc:
#
# Amendment History:
# Date: Who: Desc:
# 19/11/19 T.Mullen Initial;
# 25/11/19 T.Mullen amended to breakdown the run types;
# 26/11/19 T.Mullen updating the testing stages;
# 13/03/20 T.Mullen updating the permissions for module home;
#
. ./module_set_variables.sh
export INSTALLTYPE=$1
if [ $# -lt 1 ]; then
echo "Usage: $0 type [all|build|test|destroy;"
exit 1
fi
export LOG_FILE=/tmp/pipelinerun_${INSTALLTYPE}_`date +%Y%m%d-%H%M%S`.log
echo "INFO: Set variables" >> $LOG_FILE
echo "INFO: MODULEOWNER parameter set to $MODULEOWNER" >> $LOG_FILE
echo "INFO: PGUSERNAME parameter set to $PGUSERNAME" >> $LOG_FILE
echo "INFO: HOSTNAME parameter set to $HOSTNAME" >> $LOG_FILE
echo "INFO: PORT parameter set to $PORT" >> $LOG_FILE
echo "INFO: DBNAME parameter set to $DBNAME" >> $LOG_FILE
echo "INFO: MODULE_HOME parameter set to $MODULE_HOME" >> $LOG_FILE
PGPASS=$PGPASSWORD
echo "Starting pipeline script with option $INSTALLTYPE" | tee -a $LOG_FILE
echo "Starting time - $(date)" | tee -a $LOG_FILE
chmod 771 $MODULE_HOME/*.sh
function buildmodule
{
echo "INFO: Run $MODULEOWNER schema build script" >> $LOG_FILE
echo "INFO: Connect to postgres database $DBNAME via PSQL session" >> $LOG_FILE
psql --host=$HOSTNAME --port=$PORT --username=$PGUSERNAME --dbname=$DBNAME -v MODULE_HOME=$MODULE_HOME -v MODULEOWNERPASS=$MODULEOWNERPASS -v MODULEOWNER=$MODULEOWNER -v PGUSERNAME=$PGUSERNAME -v DBNAME=$DBNAME -v HOSTNAME=$HOSTNAME -v PORT=$PORT << EOF1 >> $LOG_FILE 2>&1
\i :MODULE_HOME/BuildScripts/createModuleOwnerSchema.sql;
\q
EOF1
PGPASSWORD=$MODULEOWNERPASS
echo "INFO: Run $MODULEOWNER schema object build scripts" >> $LOG_FILE
echo "INFO: Connect to postgres database $DBNAME via PSQL session" >> $LOG_FILE
psql --host=$HOSTNAME --port=$PORT --username=$MODULEOWNER --dbname=$DBNAME -v MODULE_HOME=$MODULE_HOME -v MODULEOWNER=$MODULEOWNER << EOF2 >> $LOG_FILE 2>&1
SET search_path = :MODULEOWNER,catalog,public;
\i :MODULE_HOME/BuildScripts/mvTypes.sql;
\i :MODULE_HOME/BuildScripts/mvConstants.sql;
\i :MODULE_HOME/BuildScripts/mvSimpleFunctions.sql;
\i :MODULE_HOME/BuildScripts/mvComplexFunctions.sql;
\i :MODULE_HOME/BuildScripts/mvApplicationFunctions.sql;
\i :MODULE_HOME/BuildScripts/mvTriggerFunction.sql;
\i :MODULE_HOME/BuildScripts/mvBESFunctions.sql;
\q
EOF2
$MODULE_HOME/module_error_chks.sh
}
function createsourceschema
{
echo "Starting building schemas " >> $LOG_FILE
echo "INFO: Creating Source Schema $SOURCEUSERNAME " >> $LOG_FILE
psql --host=$HOSTNAME --port=$PORT --username=$PGUSERNAME --dbname=$DBNAME << EOF1 >> $LOG_FILE 2>&1
DO
\$do\$
BEGIN
IF NOT EXISTS (
SELECT -- SELECT list can stay empty for this
FROM pg_catalog.pg_roles
WHERE rolname = '$SOURCEUSERNAME') THEN
CREATE USER $SOURCEUSERNAME WITH
LOGIN
NOSUPERUSER
NOCREATEDB
NOCREATEROLE
INHERIT
NOREPLICATION
CONNECTION LIMIT -1
PASSWORD '$SOURCEPASSWORD';
END IF;
END
\$do\$;
GRANT ALL PRIVILEGES ON DATABASE "$DBNAME" to $SOURCEUSERNAME;
GRANT $SOURCEUSERNAME to $PGUSERNAME;
CREATE SCHEMA $SOURCEUSERNAME AUTHORIZATION $SOURCEUSERNAME;
GRANT ALL PRIVILEGES ON SCHEMA $SOURCEUSERNAME to $PGUSERNAME;
GRANT $SOURCEUSERNAME to $MODULEOWNER;
GRANT USAGE ON SCHEMA $SOURCEUSERNAME TO $MODULEOWNER;
GRANT ALL ON SCHEMA $MODULEOWNER TO $SOURCEUSERNAME;
GRANT USAGE ON FOREIGN SERVER pgmv\$_instance TO $SOURCEUSERNAME;
EOF1
}
function createmvschema
{
echo "INFO: Creating MV Schema $MVUSERNAME " >> $LOG_FILE
PGPASSWORD=$PGPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$PGUSERNAME --dbname=$DBNAME << EOF2 >> $LOG_FILE 2>&1
DO
\$do\$
BEGIN
IF NOT EXISTS (
SELECT -- SELECT list can stay empty for this
FROM pg_catalog.pg_roles
WHERE rolname = '$MVUSERNAME') THEN
CREATE USER $MVUSERNAME WITH
LOGIN
NOSUPERUSER
NOCREATEDB
NOCREATEROLE
INHERIT
NOREPLICATION
CONNECTION LIMIT -1
PASSWORD '$MVPASSWORD';
END IF;
END
\$do\$;
GRANT $MVUSERNAME to $PGUSERNAME;
CREATE SCHEMA IF NOT EXISTS $MVUSERNAME AUTHORIZATION $MVUSERNAME;
GRANT ALL ON SCHEMA $MVUSERNAME TO $MVUSERNAME;
GRANT SELECT ON ALL TABLES IN SCHEMA $SOURCEUSERNAME TO $MVUSERNAME;
GRANT pgmv\$_role TO $MVUSERNAME;
ALTER ROLE $MODULEOWNER SET search_path TO public,$MODULEOWNER,$MVUSERNAME,$SOURCEUSERNAME;
ALTER ROLE $MVUSERNAME SET search_path TO public,$MODULEOWNER,$MVUSERNAME,$SOURCEUSERNAME;
ALTER ROLE $SOURCEUSERNAME SET search_path TO public,$MODULEOWNER,$MVUSERNAME,$SOURCEUSERNAME;
GRANT $SOURCEUSERNAME TO $MODULEOWNER;
GRANT USAGE ON SCHEMA $SOURCEUSERNAME TO $MODULEOWNER;
GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $MODULEOWNER;
GRANT ALL ON SCHEMA $MVUSERNAME TO $MODULEOWNER;
GRANT USAGE ON SCHEMA $MVUSERNAME TO $MODULEOWNER;
GRANT $MODULEOWNER TO $MVUSERNAME;
GRANT USAGE ON SCHEMA $MODULEOWNER TO $MVUSERNAME;
GRANT ALL ON SCHEMA $SOURCEUSERNAME TO $MODULEOWNER;
GRANT $SOURCEUSERNAME,$MVUSERNAME TO $PGUSERNAME;
GRANT $SOURCEUSERNAME TO $MODULEOWNER;
GRANT pgmv\$_view, pgmv\$_usage TO $MVUSERNAME;
GRANT pgmv\$_view, pgmv\$_usage, pgmv\$_execute TO $SOURCEUSERNAME;
GRANT USAGE ON FOREIGN SERVER pgmv\$_instance TO $MVUSERNAME;
EOF2
}
function createtestdata
{
echo "INFO: Creating Test Data for $SCHEMAUSERNAME " >> $LOG_FILE
PGPASSWORD=$SOURCEPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$SOURCEUSERNAME --dbname=$DBNAME << EOF3 >> $LOG_FILE 2>&1
-- FUNCTIONAL TEST SETUP
-- create test1 table
CREATE TABLE $SOURCEUSERNAME.test1
(
id numeric NOT NULL,
lookup_id numeric,
code character varying(10) COLLATE pg_catalog."default",
CONSTRAINT test1_pkey PRIMARY KEY (id)
);
-- create test2 table
CREATE TABLE $SOURCEUSERNAME.test2
(
id numeric NOT NULL,
description character varying(100) COLLATE pg_catalog."default",
metavals_id numeric,
age integer NOT NULL,
CONSTRAINT test2_pkey PRIMARY KEY (id)
);
-- create test3 table
CREATE TABLE $SOURCEUSERNAME.test3
(
lookup_id numeric NOT NULL,
lookup_code character varying(10) COLLATE pg_catalog."default" NOT NULL,
lookup_description character varying(50) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT test3_pkey PRIMARY KEY (lookup_id)
);
-- create test4 table
CREATE TABLE $SOURCEUSERNAME.test4
(
metavals_id numeric NOT NULL,
code character varying(10) COLLATE pg_catalog."default" NOT NULL,
description character varying(30) COLLATE pg_catalog."default",
CONSTRAINT test4_pkey PRIMARY KEY (metavals_id)
);
-- create test5 table
CREATE TABLE $SOURCEUSERNAME.test5
(
id numeric NOT NULL,
rep_ind character varying(1) COLLATE pg_catalog."default",
trans_id numeric,
CONSTRAINT test5_pkey PRIMARY KEY (id)
);
-- create test6 table
CREATE TABLE $SOURCEUSERNAME.test6
(
trans_id numeric NOT NULL,
payment_reference character varying(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT test6_pkey PRIMARY KEY (trans_id)
);
-- insert records into test1 table
INSERT INTO $SOURCEUSERNAME.test1(
id, lookup_id, code)
VALUES (1, 10, 'hello');
INSERT INTO $SOURCEUSERNAME.test1(
id, lookup_id, code)
VALUES (2, 20, 'bye');
INSERT INTO $SOURCEUSERNAME.test1(
id, lookup_id, code)
VALUES (3, 30, 'cya');
INSERT INTO $SOURCEUSERNAME.test1(
id, lookup_id, code)
VALUES (4, 50, 'goodbye');
INSERT INTO $SOURCEUSERNAME.test1(
id, lookup_id, code)
VALUES (5, 50, 'hi');
INSERT INTO $SOURCEUSERNAME.test1(
id, lookup_id, code)
VALUES (6, 20, 'bye');
-- insert records into test2 table
INSERT INTO $SOURCEUSERNAME.test2(
id, description, metavals_id, age)
VALUES (1, 'house', 100, 20);
INSERT INTO $SOURCEUSERNAME.test2(
id, description, metavals_id, age)
VALUES (2, 'flat', 200, 35);
INSERT INTO $SOURCEUSERNAME.test2(
id, description, metavals_id, age)
VALUES (3, 'bungalow', 300, 30);
INSERT INTO $SOURCEUSERNAME.test2(
id, description, metavals_id, age)
VALUES (4, 'palace', 300, 30);
INSERT INTO $SOURCEUSERNAME.test2(
id, description, metavals_id, age)
VALUES (5, 'office', 400, 50);
-- insert records into test3 table
INSERT INTO $SOURCEUSERNAME.test3(
lookup_id, lookup_code, lookup_description)
VALUES (10, 'ENG', 'ENGLAND');
INSERT INTO $SOURCEUSERNAME.test3(
lookup_id, lookup_code, lookup_description)
VALUES (20, 'WAL', 'WALES');
INSERT INTO $SOURCEUSERNAME.test3(
lookup_id, lookup_code, lookup_description)
VALUES (30, 'SCO', 'SCOTLAND');
INSERT INTO $SOURCEUSERNAME.test3(
lookup_id, lookup_code, lookup_description)
VALUES (40, 'IRE', 'IRELAND');
INSERT INTO $SOURCEUSERNAME.test3(
lookup_id, lookup_code, lookup_description)
VALUES (50, 'FRA', 'FRANCE');
-- insert records into test4 table
INSERT INTO $SOURCEUSERNAME.test4(
metavals_id, code, description)
VALUES (100,'CHAIR','SMALL CHAIR');
INSERT INTO $SOURCEUSERNAME.test4(
metavals_id, code, description)
VALUES (200,'TABLE','SMALL TABLE');
INSERT INTO $SOURCEUSERNAME.test4(
metavals_id, code, description)
VALUES (300,'LIGHT','BRIGHT LIGHT');
INSERT INTO $SOURCEUSERNAME.test4(
metavals_id, code, description)
VALUES (400,'BED','KING SIZE BED');
INSERT INTO $SOURCEUSERNAME.test4(
metavals_id, code, description)
VALUES (500,'CUPBOARD','BEDSIDE CUPBOARD');
-- insert records into test5 table
INSERT INTO $SOURCEUSERNAME.test5(
id, rep_ind, trans_id)
VALUES (1, 'Y', 1000);
INSERT INTO $SOURCEUSERNAME.test5(
id, rep_ind, trans_id)
VALUES (2, 'Y', 2000);
INSERT INTO $SOURCEUSERNAME.test5(
id, rep_ind, trans_id)
VALUES (3, 'N', 3000);
INSERT INTO $SOURCEUSERNAME.test5(
id, rep_ind, trans_id)
VALUES (4, 'Y', 4000);
INSERT INTO $SOURCEUSERNAME.test5(
id, rep_ind, trans_id)
VALUES (5, 'N', 5000);
-- insert records into test6 table
INSERT INTO $SOURCEUSERNAME.test6(
trans_id, payment_reference)
VALUES (1000, 'GZ-1000');
INSERT INTO $SOURCEUSERNAME.test6(
trans_id, payment_reference)
VALUES (2000, 'AZ-2000');
INSERT INTO $SOURCEUSERNAME.test6(
trans_id, payment_reference)
VALUES (3000, 'BZ-3000');
INSERT INTO $SOURCEUSERNAME.test6(
trans_id, payment_reference)
VALUES (4000, 'QZ-4000');
INSERT INTO $SOURCEUSERNAME.test6(
trans_id, payment_reference)
VALUES (5000, 'VZ-5000');
EOF3
}
function createmvlogs
{
echo "INFO: Creating MV Logs for $SCHEMAUSERNAME " >> $LOG_FILE
PGPASSWORD=$SOURCEPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$SOURCEUSERNAME --dbname=$DBNAME << EOF4 >> $LOG_FILE 2>&1
DO
\$do\$
DECLARE
cResult CHAR(1) := NULL;
BEGIN
cResult := $MODULEOWNER.mv\$createMaterializedViewlog( 'test1','$SOURCEUSERNAME');
cResult := $MODULEOWNER.mv\$createMaterializedViewlog( 'test2','$SOURCEUSERNAME');
cResult := $MODULEOWNER.mv\$createMaterializedViewlog( 'test3','$SOURCEUSERNAME');
cResult := $MODULEOWNER.mv\$createMaterializedViewlog( 'test4','$SOURCEUSERNAME');
cResult := $MODULEOWNER.mv\$createMaterializedViewlog( 'test5','$SOURCEUSERNAME');
cResult := $MODULEOWNER.mv\$createMaterializedViewlog( 'test6','$SOURCEUSERNAME');
END
\$do\$;
EOF4
}
function createtestmv
{
echo "INFO: Creating MV in $MVUSERNAME " >> $LOG_FILE
PGPASSWORD=$MVPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$MVUSERNAME --dbname=$DBNAME << EOF5 >> $LOG_FILE 2>&1
DO
\$do\$
DECLARE
tStartTime TIMESTAMP := clock_timestamp();
cResult CHAR(1) := NULL;
iTableCounter SMALLINT := 10;
pSqlStatement TEXT;
BEGIN
pSqlStatement := 'SELECT test1.id test1_id,
test1.lookup_id test1_lookup_id,
test1.code test1_code,
test2.id test2_id,
test2.description test2_desc,
test2.metavals_id test2_metavals_id,
test2.age test2_age,
test3.lookup_id test3_lookup_id,
test3.lookup_code test3_lookup_code,
test3.lookup_description test3_lookup_desc,
test4.metavals_id test4_metavals_id,
test4.code test4_code,
test4.description test4_desc,
test5.id test5_id,
test5.rep_ind test5_rep_ind,
test5.trans_id test5_trans_id,
test6.trans_id test6_trans_id,
test6.payment_reference test6_payment_ref
FROM
test1
INNER JOIN test2 ON test1.id = test2.id
LEFT JOIN test3 ON test1.lookup_id = test3.lookup_id
LEFT JOIN test4 ON test2.metavals_id = test4.metavals_id
INNER JOIN test5 ON test1.id = test5.id
LEFT JOIN test6 ON test5.trans_id = test6.trans_id';
FOR iTableCounter IN 10 .. 100
LOOP
cResult := mv\$createMaterializedView
(
pViewName => 'mvtesting' || iTableCounter,
pSelectStatement => pSqlStatement,
pOwner => '$MVUSERNAME',
pFastRefresh => TRUE
);
END LOOP;
RAISE NOTICE 'Simple Snapshot Creation took %', clock_timestamp() - tStartTime;
END
\$do\$;
EOF5
}
function dropmvschema
{
echo "INFO: Dropping MV's" >> $LOG_FILE
PGPASSWORD=$MVPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$MVUSERNAME --dbname=$DBNAME << EOF5 >> $LOG_FILE 2>&1
DO
\$do\$
DECLARE
cResult CHAR(1) := NULL;
iTableCounter SMALLINT := 10;
BEGIN
FOR iTableCounter IN 10 .. 100
LOOP
cResult := mv\$removeMaterializedView( 'mvtesting' || iTableCounter, '$MVUSERNAME' );
END LOOP;
END
\$do\$;
EOF5
echo "INFO: Dropping MV logs" >> $LOG_FILE
PGPASSWORD=$SOURCEPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$SOURCEUSERNAME --dbname=$DBNAME << EOF4 >> $LOG_FILE 2>&1
DO
\$do\$
DECLARE
cResult CHAR(1) := NULL;
BEGIN
cResult := mv\$removeMaterializedViewLog( 'test1', '$SOURCEUSERNAME' );
cResult := mv\$removeMaterializedViewLog( 'test2', '$SOURCEUSERNAME' );
cResult := mv\$removeMaterializedViewLog( 'test3', '$SOURCEUSERNAME' );
cResult := mv\$removeMaterializedViewLog( 'test4', '$SOURCEUSERNAME' );
cResult := mv\$removeMaterializedViewLog( 'test5', '$SOURCEUSERNAME' );
cResult := mv\$removeMaterializedViewLog( 'test6', '$SOURCEUSERNAME' );
END
\$do\$;
EOF4
echo "INFO: Dropping MV user $MVUSERNAME " >> $LOG_FILE
PGPASSWORD=$PGPASS
psql --host=$HOSTNAME --port=$PORT --username=$PGUSERNAME --dbname=$DBNAME << EOF3 >> $LOG_FILE 2>&1
REVOKE ALL PRIVILEGES ON DATABASE "$DBNAME" from $MVUSERNAME;
GRANT ALL PRIVILEGES ON SCHEMA $MVUSERNAME to $PGUSERNAME;
REVOKE USAGE ON FOREIGN SERVER pgmv\$_instance FROM $MVUSERNAME;
ALTER SCHEMA $MVUSERNAME OWNER TO $PGUSERNAME;
DROP SCHEMA $MVUSERNAME CASCADE;
REVOKE $SOURCEUSERNAME from $MVUSERNAME;
revoke $SOURCEUSERNAME from $MODULEOWNER;
revoke $MVUSERNAME from $MODULEOWNER;
revoke $MODULEOWNER from $MVUSERNAME ;
revoke $MVUSERNAME from $PGUSERNAME;
revoke USAGE ON SCHEMA $MODULEOWNER from $MVUSERNAME ;
DROP USER $MVUSERNAME;
EOF3
}
function dropsourceschema
{
echo "INFO: Dropping Schema user $SCHEMAUSERNAME " >> $LOG_FILE
PGPASSWORD=$PGPASS
psql --host=$HOSTNAME --port=$PORT --username=$PGUSERNAME --dbname=$DBNAME << EOF4 >> $LOG_FILE 2>&1
REVOKE ALL PRIVILEGES ON DATABASE "$DBNAME" from $SOURCEUSERNAME;
GRANT ALL PRIVILEGES ON SCHEMA $SOURCEUSERNAME to $PGUSERNAME;
REVOKE USAGE ON FOREIGN SERVER pgmv\$_instance FROM $SOURCEUSERNAME;
ALTER SCHEMA $SOURCEUSERNAME OWNER TO $PGUSERNAME;
DROP SCHEMA $SOURCEUSERNAME CASCADE;
REVOKE $SOURCEUSERNAME from $PGUSERNAME;
REVOKE $SOURCEUSERNAME FROM $MODULEOWNER;
REVOKE ALL ON SCHEMA $MODULEOWNER FROM $SOURCEUSERNAME;
DROP USER $SOURCEUSERNAME;
EOF4
}
function dropmodule
{
echo "INFO: Dropping modules objects" >> $LOG_FILE
PGPASSWORD=$PGPASS
psql --host=$HOSTNAME --port=$PORT --username=$PGUSERNAME --dbname=$DBNAME << EOF1 >> $LOG_FILE 2>&1
REVOKE ALL PRIVILEGES ON DATABASE "$DBNAME" from $MODULEOWNER;
REVOKE USAGE ON FOREIGN SERVER pgmv\$_instance FROM $MODULEOWNER;
DROP SERVER IF EXISTS pgmv\$_instance CASCADE;
DROP EXTENSION IF EXISTS postgres_fdw;
DROP EXTENSION IF EXISTS dblink;
DROP SCHEMA $MODULEOWNER CASCADE;
DROP ROLE pgmv\$_role;
REVOKE $MODULEOWNER from $PGUSERNAME;
DROP EXTENSION "uuid-ossp";
DROP ROLE $MODULEOWNER;
DROP ROLE pgmv\$_execute;
DROP ROLE pgmv\$_view;
DROP ROLE pgmv\$_usage;
EOF1
}
function testing1
{
echo "INFO: Updating test1 table $SCHEMAUSERNAME " >> $LOG_FILE
PGPASSWORD=$SOURCEPASSWORD
psql --host=$HOSTNAME --port=$PORT --username=$SOURCEUSERNAME --dbname=$DBNAME << EOF3 >> $LOG_FILE 2>&1
UPDATE $SOURCEUSERNAME.test1 set code='yo' where code='hello';
EOF3
echo "INFO: Refreshing the MV's" >> $LOG_FILE
psql --host=$HOSTNAME --port=$PORT --username=$MVUSERNAME --dbname=$DBNAME << EOF6 >> $LOG_FILE 2>&1
DO
\$do\$
DECLARE
tStartTime TIMESTAMP := clock_timestamp();
cResult CHAR(1) := NULL;
iTableCounter SMALLINT := 10;
BEGIN
FOR iTableCounter IN 10 .. 20
LOOP
cResult := mv\$refreshMaterializedView
(
pViewName => 'mvtesting' || iTableCounter,
pOwner => '$MVUSERNAME',
pFastRefresh => TRUE
);
END LOOP;
RAISE NOTICE 'Fast Snapshot Refresh took % % %', clock_timestamp() - tStartTime, chr(10), chr(10);
END
\$do\$;
DO
\$do\$
DECLARE
tStartTime TIMESTAMP := clock_timestamp();
cResult CHAR(1) := NULL;
iTableCounter SMALLINT := 10;
BEGIN
FOR iTableCounter IN 70 .. 100
LOOP
cResult := mv\$refreshMaterializedView
(
pViewName => 'mvtesting' || iTableCounter,
pOwner => '$MVUSERNAME',
pFastRefresh => TRUE
);
END LOOP;
RAISE NOTICE 'Fast Snapshot Refresh took % % %', clock_timestamp() - tStartTime, chr(10), chr(10);
END
\$do\$;
DO
\$do\$
DECLARE
tStartTime TIMESTAMP := clock_timestamp();
cResult CHAR(1) := NULL;
iTableCounter SMALLINT := 10;
BEGIN
FOR iTableCounter IN 21 .. 69
LOOP
cResult := mv\$refreshMaterializedView
(
pViewName => 'mvtesting' || iTableCounter,
pOwner => '$MVUSERNAME',
pFastRefresh => TRUE
);
END LOOP;
RAISE NOTICE 'Fast Snapshot Refresh took % % %', clock_timestamp() - tStartTime, chr(10), chr(10);
END
\$do\$;
EOF6
echo "INFO: Check the MV's Data" >> $LOG_FILE
psql --host=$HOSTNAME --port=$PORT --username=$MVUSERNAME --dbname=$DBNAME << EOF6 >> $LOG_FILE 2>&1
DO
\$\$
DECLARE
each_row record;
testout bigint;
BEGIN
FOR each_row IN
SELECT 'select count(*) from ' || table_name || ' where test1_code=''hello'';' AS alter_sql
FROM information_schema.tables
WHERE table_schema = 'testpocview'
LOOP
EXECUTE each_row.alter_sql into testout;
raise info '%', each_row.alter_sql;
raise info '%', testout;
END LOOP;
END
\$\$;
EOF6
echo "test output $checktestoutput" >> $LOG_FILE
checkuser_checktestoutput=`cat $LOG_FILE |grep "INFO: 1" | wc -l`
if [ $checkuser_checktestoutput -gt 0 ]; then
echo "ERROR Problems with the test data" >> $LOG_FILE
echo "Output from checks" >> $LOG_FILE
echo "$checktestoutput" >> $LOG_FILE
else
echo "Test data checks all ok" | tee -a $LOG
fi
}
function problemcheck
{
#Check for Problem
chk_errors=`cat $LOG_FILE |grep -E "ERROR|error|Error|refused|failed" |wc -l`
if [ $chk_errors -lt 1 ]; then
echo "Pipeline run type $INSTALLTYPE ran with no issues" | tee -a $LOG_FILE
echo "Run completion time - $(date)" | tee -a $LOG_FILE
else
echo "Problems with run type $INSTALLTYPE check logfile for detailed view - $LOG_FILE" | tee -a $LOG_FILE
runerrors=`cat $LOG_FILE |grep -E "ERROR|error|Error|refused|failed"`
echo "Problems with the run below" | tee -a $LOG_FILE
echo "$runerrors" | tee -a $LOG_FILE
echo "Run completion time - $(date)" | tee -a $LOG_FILE
exit 1
fi
}
#Install Type
if [ "$INSTALLTYPE" = all ]; then
echo "Stage 1: Creating the fast refresh module objects in schemas $MODULEOWNER" | tee -a $LOG_FILE
buildmodule
echo "Stage 2: Creating the schemas $SOURCEUSERNAME and $MVUSERNAME" | tee -a $LOG_FILE
createsourceschema
createmvschema
echo "Stage 3: Creating the test objects and data in schema $SOURCEUSERNAME" | tee -a $LOG_FILE
createtestdata
echo "Stage 4: Creating the MV logs in $SOURCEUSERNAME" | tee -a $LOG_FILE
createmvlogs
echo "Stage 5: Creating 90 test MV's in schema $MVUSERNAME" | tee -a $LOG_FILE
createtestmv
echo "Stage 6: Test phase " | tee -a $LOG_FILE
echo "Stage 6.1: Update 1 row and refresh all MV's " | tee -a $LOG_FILE
testing1
echo "Stage 7: Dropping the test harness objects" | tee -a $LOG_FILE
dropmvschema
dropsourceschema
dropmodule
echo "Stage 8: Check for problems" | tee -a $LOG_FILE
problemcheck
elif [ "$INSTALLTYPE" = build ]; then
echo "Stage 1: Creating the fast refresh module objects in schemas $MODULEOWNER" | tee -a $LOG_FILE
buildmodule
echo "Stage 2: Creating the schemas $SOURCEUSERNAME and $MVUSERNAME" | tee -a $LOG_FILE
createsourceschema
createmvschema
echo "Stage 3: Creating the test objects and data in schema $SOURCEUSERNAME" | tee -a $LOG_FILE
createtestdata
echo "Stage 4: Creating the MV logs in $SOURCEUSERNAME" | tee -a $LOG_FILE
createmvlogs
echo "Stage 5: Creating 90 test MV's in schema $MVUSERNAME" | tee -a $LOG_FILE
createtestmv
echo "Stage 6: Check for problems" | tee -a $LOG_FILE
problemcheck
elif [ "$INSTALLTYPE" = test ]; then
echo "Stage 1: Test phase " | tee -a $LOG_FILE
echo "Stage 1.1: Update 1 row and refresh all MV's " | tee -a $LOG_FILE
testing1
echo "Stage 2: Check for problems" | tee -a $LOG_FILE
problemcheck
elif [ "$INSTALLTYPE" = destroy ]; then
echo "Stage 1: Dropping the test harness objects" | tee -a $LOG_FILE
dropmvschema
dropsourceschema
dropmodule
echo "Stage 2: Check for problems" | tee -a $LOG_FILE
problemcheck
else
echo "$(date): Incorrect option must be all, build, test or destroy"
exit 1
fi