-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_msmov.sql
1313 lines (1171 loc) · 63.7 KB
/
create_msmov.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
create schema msmov;
CREATE TABLE msmov.data_imported_table (
id text,
date_time timestamp without time zone,
tab text,
cnt integer
);
CREATE TABLE msmov.error_table (
id text,
date_time timestamp without time zone,
command text,
error text
);
CREATE TABLE msmov.mssql_columns_type_change (
sch varchar NOT NULL,
tab varchar NOT NULL,
col varchar NOT NULL,
typ varchar NOT NULL
);
ALTER TABLE msmov.mssql_columns_type_change ADD CONSTRAINT type_change_unique UNIQUE (sch, tab,col );
CREATE OR REPLACE FUNCTION msmov.create_ftables(source_schema text, fdw_name text, tab_except text default null) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
except_list text := '';
BEGIN
BEGIN
command:= 'DROP SCHEMA IF EXISTS _'||$1||' CASCADE' ;
EXECUTE command;
command:= 'DROP SCHEMA IF EXISTS '||$1||' CASCADE' ;
EXECUTE command;
command:= 'CREATE SCHEMA _'||$1 ;
EXECUTE command;
command:= 'CREATE SCHEMA '||$1 ;
EXECUTE command;
--check except tables
if $3 is not null then
except_list:= ' EXCEPT ('||$3||') ';
end if;
command:= 'IMPORT FOREIGN SCHEMA "'||$1|| '" '|| except_list ||' FROM SERVER '||$2|| ' INTO _'||$1 || ' OPTIONS (import_default ''true'', import_not_null ''true'');';
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
SELECT count(*) into cnt from information_schema.foreign_tables WHERE foreign_table_schema='_'||$1;
RAISE NOTICE 'Create % FOREIGN tables in schema _%, corresponding to tables from MSSQL schema %', cnt,$1,$1;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_tables_from_ft(from_schema text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
indentity_rec record;
BEGIN
FOR tab IN SELECT foreign_table_name FROM Information_schema.foreign_tables where foreign_table_schema='_'||lower($1) LOOP
RAISE NOTICE 'CREATING TABLE: %.%',$1,tab;
command:= 'CREATE TABLE '||$1||'."'||tab||'" (LIKE _'||$1||'."'||tab||'")';
BEGIN
EXECUTE command;
EXCEPTION
WHEN SQLSTATE '42P07' THEN
RAISE NOTICE '%', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Table % exist ',$1||'.'||tab;
cnt:=cnt-1;
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
cnt:=cnt-1;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
cnt:=cnt+1;
END LOOP;
RAISE NOTICE 'TOTAL TABLES CREATED: %',cnt;
--analyzing identity columns
RAISE NOTICE 'ANALYZING IDENTITY COLUMNS';
FOR indentity_rec IN SELECT * from msmov.mssql_indetity_columns LOOP
BEGIN
IF indentity_rec.sch=$1 THEN
command:= 'ALTER TABLE "'|| indentity_rec.sch||'"."'|| indentity_rec.tab ||'" ALTER "'|| indentity_rec.col ||'" ADD GENERATED by default AS identity (START WITH '|| ((indentity_rec.curval)::bigint+1)::text||')';
EXECUTE command;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
END;
END LOOP;
--analyzing identity columns
RAISE NOTICE 'ANALYZING CHANGES IN COLUMNS TYPE';
FOR indentity_rec IN SELECT * from msmov.mssql_columns_type_change LOOP
BEGIN
IF indentity_rec.sch=$1 THEN
--change type in Foreign Table
command:= 'ALTER TABLE "_'|| indentity_rec.sch||'"."'|| indentity_rec.tab ||'" ALTER COLUMN "'|| indentity_rec.col ||'" TYPE '|| indentity_rec.typ;
EXECUTE command;
--change type in Local Table
command:= 'ALTER TABLE "'|| indentity_rec.sch||'"."'|| indentity_rec.tab ||'" ALTER COLUMN "'|| indentity_rec.col ||'" TYPE '|| indentity_rec.typ ||' USING "'||indentity_rec.col||'"::'||indentity_rec.typ;
EXECUTE command;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
END;
END LOOP;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_data_one_table(from_schema text, tab text)
RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
total bigint;
BEGIN
RAISE NOTICE 'DELETING DATA FROM TABLE: %.%',$1,$2;
command := 'TRUNCATE '|| $1||'."'||$2||'"';
EXECUTE command;
RAISE NOTICE 'IMPORTING DATA IN TABLE: %.%',$1,$2;
command:= 'INSERT INTO '|| $1||'."'||$2||'" SELECT * FROM _'||$1||'."'||$2||'"';
BEGIN
EXECUTE command;
command:= 'SELECT count(*) FROM '||$1||'."'||$2||'"';
EXECUTE command into total ;
RAISE NOTICE 'TABLE DATA: %, % ROWS',$1||'.'||$2, total;
INSERT INTO msmov.data_imported_table (id,date_time,tab, cnt) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,$1||'.'||$2, total);
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--cnt:=cnt-1;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
cnt:=cnt+1;
RAISE NOTICE 'TOTAL DATA TABLE IMPORTED: %',total;
RETURN total;
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
RAISE NOTICE 'Ignoring table %, no present in your schema % ',$2,$1;
RETURN 0;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftpkey(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.p_keys';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.p_keys(tab text, pk text ) server '|| $2 ||' options ( QUERY ''SELECT KU.table_name as tab,column_name as pk
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = ''''PRIMARY KEY'''' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
WHERE TC.TABLE_SCHEMA ='''''||$1||'''''
ORDER BY KU.TABLE_NAME, KU.ORDINAL_POSITION'')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_pk_tables(target_schema text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab record;
tmp text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
command := 'SELECT tab, string_agg(''"''||pk||''"'','','') as pk FROM _'||$1||'.p_keys group by 1';
FOR tab IN EXECUTE command LOOP
RAISE NOTICE 'IMPORTING PRIMARY KEY IN TABLE %', tab.tab;
command := 'ALTER TABLE '||$1||'."'||tab.tab||'" ADD PRIMARY KEY ('|| tab.pk ||')';
BEGIN
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
cnt:=cnt-1;
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
END;
cnt:=cnt+1;
END LOOP;
RAISE NOTICE 'TOTAL PKS IMPORTED: %',cnt;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftfkey(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.f_keys';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.f_keys(tab character varying,fkname character varying,col character varying,
tab_ref character varying,tab_ref_col character varying,m character varying,upt character varying,del character varying ) server '|| $2 ||' options ( query ''
SELECT TC.table_name as tab, TC.CONSTRAINT_NAME fkname,KU.COLUMN_NAME col, T.TABLE_NAME tab_ref, T.COLUMN_NAME tab_ref_col,
R.MATCH_OPTION m,R.UPDATE_RULE upt,R.DELETE_RULE del
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = ''''FOREIGN KEY'''' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as R
ON R.CONSTRAINT_NAME=TC.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE as T
ON R.UNIQUE_CONSTRAINT_NAME=T.CONSTRAINT_NAME
WHERE TC.TABLE_SCHEMA ='''''||$1||'''''
ORDER BY KU.TABLE_NAME, KU.ORDINAL_POSITION
'')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftukey(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.u_keys';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.u_keys(tab text, uname text, col text ) server '|| $2 ||' options ( query ''SELECT KU.table_name as tab,ku.CONSTRAINT_NAME uname,column_name as col
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = ''''UNIQUE'''' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
WHERE TC.TABLE_SCHEMA ='''''||$1||'''''
ORDER BY KU.TABLE_NAME, KU.ORDINAL_POSITION'')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_uk_tables(target_schema text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab record;
tmp text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
begin
command := 'CREATE TEMP TABLE uk as SELECT * FROM _'||$1||'.u_keys';
EXECUTE command;
command := 'SELECT tab,uname, string_agg(''"''||col||''"'','','') as col FROM uk group by 1,2';
FOR tab IN EXECUTE command LOOP
tmp:= 'ALTER TABLE '||$1||'."'||tab.tab||'" ADD CONSTRAINT "'||tab.uname||'" UNIQUE ('||tab.col||')';
command := tmp;
BEGIN
RAISE NOTICE 'IMPORTING UNIQUE KEY IN TABLE %', tab.tab;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
cnt:=cnt+1;
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
END;
cnt:=cnt+1;
END LOOP;
drop table uk;
RAISE NOTICE 'TOTAL UNIQUES IMPORTED: %',cnt;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftfkey(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.f_keys';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.f_keys(tab character varying,fkname character varying,col character varying,
tab_ref character varying,tab_ref_col character varying,m character varying,upt character varying,del character varying ) server '|| $2 ||' options ( query ''
SELECT TC.table_name as tab, TC.CONSTRAINT_NAME fkname,KU.COLUMN_NAME col, T.TABLE_NAME tab_ref, T.COLUMN_NAME tab_ref_col,
R.MATCH_OPTION m,R.UPDATE_RULE upt,R.DELETE_RULE del
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = ''''FOREIGN KEY'''' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS as R
ON R.CONSTRAINT_NAME=TC.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE as T
ON R.UNIQUE_CONSTRAINT_NAME=T.CONSTRAINT_NAME
WHERE TC.TABLE_SCHEMA ='''''||$1||'''''
ORDER BY KU.TABLE_NAME, KU.ORDINAL_POSITION
'')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_fk_tables(target_schema text, tpy text DEFAULT 'NO ACTION'::text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab record;
tmp text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
begin
command := 'CREATE TEMP TABLE fk as SELECT * FROM _'||$1||'.f_keys';
EXECUTE command;
command := 'SELECT distinct tab, fkname, string_agg( distinct ''"''||col||''"'','','') as col
,tab_ref,string_agg( distinct ''"''||tab_ref_col||''"'','','') as tab_ref_col, m, upt,del FROM fk group by tab ,fkname,tab_ref, m, upt,del ';
FOR tab IN EXECUTE command LOOP
tmp:='ALTER TABLE '||$1||'."'||tab.tab||'" ADD CONSTRAINT "'||tab.fkname||'" FOREIGN KEY ('||tab.col||') REFERENCES '||$1||'."'||tab.tab_ref||'" ('||tab.tab_ref_col||') MATCH '||tab.m||' ON UPDATE '||tab.upt||' ON DELETE '||tab.del ;
command := tmp;
--RAISE NOTICE '%', command;
BEGIN
RAISE NOTICE 'IMPORTING FK IN TABLE %', tab.tab;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
cnt:=cnt-1;
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
END;
cnt:=cnt+1;
END LOOP;
drop table fk;
RAISE NOTICE 'TOTAL FKS IMPORTED: %',cnt;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftckey(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.c_keys';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.c_keys(tab text, cname text, clause text ) server '|| $2 ||' options ( query ''
SELECT U.TABLE_NAME tab , C.CONSTRAINT_NAME cname ,C.CHECK_CLAUSE clause from INFORMATION_SCHEMA.CHECK_CONSTRAINTS as C
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as U
ON C.CONSTRAINT_NAME=U.CONSTRAINT_NAME
WHERE U.TABLE_SCHEMA ='''''||$1||''''' '')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_ck_tables(target_schema text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab record;
tmp text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
command := 'SELECT distinct * FROM _'||$1||'.c_keys ';
FOR tab IN EXECUTE command LOOP
tmp:='ALTER TABLE '||$1||'."'||tab.tab||'" ADD CONSTRAINT '||tab.cname||' CHECK '||replace(replace(tab.clause,'[','"'),']','"');
command := tmp;
--RAISE NOTICE '%', command;
BEGIN
RAISE NOTICE 'IMPORTING CHECK IN TABLE %', tab.tab;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
cnt:=cnt-1;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
END;
cnt:=cnt+1;
END LOOP;
RAISE NOTICE 'TOTAL CHECKS CONSTRAINTS IMPORTED: %',cnt;
RETURN cnt;
END;
$_$;
--SELECT msmov.import_ck_tables('dbo');
CREATE OR REPLACE FUNCTION msmov.create_ftindex(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.index_keys';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.index_keys(tab character varying,iname character varying,col character varying,filter int,
filter_def character varying ) server '|| $2 ||' options ( query ''
SELECT
t.name tab,
ind.name iname,
col.name col,
ind.has_filter filter,
ind.filter_definition filter_def,SCHEMA_NAME(t.schema_id)
FROM
sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
WHERE
ind.is_primary_key = 0
AND ind.is_unique = 0
AND ind.is_unique_constraint = 0
AND t.is_ms_shipped = 0
AND SCHEMA_NAME(t.schema_id)='''''||$1||'''''
ORDER BY
t.name, ind.name, ind.index_id, ic.index_column_id'')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_index_tables(target_schema text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab record;
tmp text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
command := 'SELECT tab,iname,string_agg(''"''||col||''"'','','') as col,filter,filter_def FROM _'||$1||'.index_keys group by 1,2,4,5';
FOR tab IN EXECUTE command LOOP
command := 'CREATE INDEX '||tab.iname||' ON '||$1||'."'||tab.tab||'" ('||tab.col||')';
IF tab.filter=1 THEN
command:=command|| 'WHERE '||replace(replace(tab.filter_def,'[','"'),']','"');
END IF;
RAISE NOTICE 'IMPORTING INDEX % IN TABLE %',tab.iname, tab.tab;
BEGIN
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
cnt:=cnt-1;
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
END;
cnt:=cnt+1;
END LOOP;
RAISE NOTICE 'TOTAL INDEX IMPORTED: %',cnt;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftviews(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.views';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.views(tab character varying, ddl character varying ) server '|| $2 ||' options ( query ''
SELECT table_name tab, VIEW_DEFINITION ddl,* FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = '''''||$1||''''' '')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.import_views(target_schema text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
tab record;
tmp text;
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
command := 'SELECT * FROM _'||$1||'.views';
FOR tab IN EXECUTE command LOOP
RAISE NOTICE 'IMPORTING VIEW: %', tab.tab;
command := 'set search_path ='||lower($1)||','||current_setting ('search_path')||'; ' ||tab.ddl ||';' ;
BEGIN
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
cnt:=cnt-1;
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
END;
cnt:=cnt+1;
set search_path to default;
END LOOP;
set search_path to default;
RAISE NOTICE 'TOTAL VIEWS IMPORTED: %',cnt;
RETURN cnt;
END;
$_$;
CREATE OR REPLACE FUNCTION msmov.create_ftdomains(source_schema text, fdw_name text) RETURNS integer
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
cnt int :=0;
BEGIN
BEGIN
command:= 'drop FOREIGN TABLE IF EXISTS _'||$1||'.domains';
EXECUTE command;
command:= 'CREATE FOREIGN TABLE _'||$1||'.domains(tab character varying, typ_base character varying,
max_length int, precision int,scale int, is_nullable int, is_table_type int ) server '|| $2 ||' options ( query ''
SELECT
o.name as tab,
(select i.name from sys.types as i where o.system_type_id=i.system_type_id and i.name<>o.name) as typ_base,
o.max_length ,o.[precision] ,o.scale,o.is_nullable,is_table_type
FROM sys.types o
WHERE o.is_user_defined = 1
AND SCHEMA_NAME(o.schema_id )= '''''||$1||''''' '')';
--RAISE NOTICE '%', command;
EXECUTE command;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'command: %', command;
GET STACKED DIAGNOSTICS men = MESSAGE_TEXT,mendetail = PG_EXCEPTION_DETAIL,sqlerror=RETURNED_SQLSTATE;
INSERT INTO msmov.error_table (id,date_time,command,error) VALUES ($1,clock_timestamp ( ) ::timestamp without time zone ,command,sqlerror||'-'||men||'-'||mendetail);
RAISE NOTICE 'Error %, %,% ',sqlerror,men,mendetail;
--RAISE EXCEPTION 'Error %, %,% ',sqlerror,men,mendetail;
END;
RETURN 1;
END;
$_$;
CREATE TYPE msmov.estimation_typ AS (details text, comments text, cost numeric);
CREATE OR REPLACE FUNCTION msmov.estimation_analysis (fdw_name text) RETURNS SETOF msmov.estimation_typ
LANGUAGE plpgsql
AS $_$
DECLARE
command text;
men text;
mendetail text;
sqlerror text;
result msmov.estimation_typ;
BEGIN
BEGIN
--create the FOREIGN TABLES in msmov schema to get the data estimation
--version
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_version' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_version (version varchar) SERVER %s OPTIONS
( QUERY ''SELECT @@version as version'')';
EXECUTE format(command,$1);
--size
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_db_size' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_db_size (database_name varchar,database_size varchar) SERVER %s OPTIONS
( QUERY ''SELECT DB_NAME() AS database_name, CAST(SUM( CAST( (size * 8.0/1024) AS DECIMAL(15,2) ) ) AS VARCHAR(20)) AS database_size FROM sys.database_files'')';
EXECUTE format(command,$1);
--users
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_users' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_users ( username varchar, datowner varchar) SERVER %s OPTIONS
(query ''SELECT DISTINCT username,datowner FROM (
SELECT SUSER_SNAME(owner_sid) AS username, name as datowner FROM sys.databases
WHERE name not in (''''master'''',''''model'''',''''msdb'''',''''tempdb'''')
UNION ALL
SELECT name as username, '''''''' as datowner
--,type_desc as type, authentication_type_desc as authentication_type
FROM sys.database_principals
WHERE type not in (''''A'''', ''''G'''', ''''R'''', ''''X'''') and
sid is not null and
name != ''''guest'''') as sub
ORDER BY datowner DESC '', row_estimate_method ''showplan_all'' ) ';
EXECUTE format(command,$1);
--schemas
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_schemas' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_schemas ( schema_name varchar, schema_id integer, schema_owner varchar) SERVER %s OPTIONS
(query ''SELECT s.name as schema_name, s.schema_id, u.name as schema_owner FROM sys.schemas s inner join sys.sysusers u on u.uid = s.principal_id
WHERE s.schema_id < 1000 and s.name not in (''''guest'''',''''INFORMATION_SCHEMA'''',''''sys'''') order by s.name'', row_estimate_method ''showplan_all'' ) ';
EXECUTE format(command,$1);
--linked servers, is_linekd=1
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_linked_servers' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_linked_servers ( server_name varchar, data_source varchar ) SERVER %s OPTIONS
(query ''SELECT name as server_name,data_source FROM sys.servers WHERE is_linked = 1'' , row_estimate_method ''showplan_all'' )';
EXECUTE format(command,$1);
--tables
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_tables' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_tables ( "TABLE_CATALOG" varchar, "TABLE_SCHEMA" varchar, "TABLE_NAME" varchar,"TABLE_TYPE" varchar ) SERVER %s OPTIONS
(query ''SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ''''BASE TABLE'''' '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--views
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_views' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_views ( "TABLE_CATALOG" varchar, "TABLE_SCHEMA" varchar, "TABLE_NAME" varchar,"TABLE_TYPE" varchar ) SERVER %s OPTIONS
(query ''SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ''''VIEW'''' '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--views
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_indexes' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_indexes ( table_name varchar, index_name varchar ) SERVER %s OPTIONS
(query ''SELECT t.name table_name, ind.name index_name FROM sys.indexes ind INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id INNER JOIN
sys.tables t ON ind.object_id = t.object_id
WHERE ind.is_primary_key = 0 AND ind.is_unique = 0 AND ind.is_unique_constraint = 0 AND t.is_ms_shipped = 0
ORDER BY t.name, ind.name, ind.index_id, ic.index_column_id '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--sequences
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_sequences' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_sequences ( "Schema" varchar, sequence_name varchar,current_value varchar,start_value varchar, increment varchar,
is_cycling varchar, system_type varchar,user_type varchar ) SERVER %s OPTIONS
(query ''SELECT SCHEMA_NAME(schema_id) AS "Schema", name as sequence_name, CAST (current_value as varchar) as current_value,
CAST (start_value as varchar) as start_value , CAST (increment as varchar) as increment, is_cycling,
TYPE_NAME(system_type_id) AS system_type, TYPE_NAME(user_type_id) AS user_type FROM sys.sequences ORDER BY name; '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--synonyms
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_synonyms' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_synonyms ( sch varchar,syn_name varchar, object_ref varchar, object_type varchar) SERVER %s OPTIONS
(query '' SELECT SCHEMA_NAME(schema_id) AS sch,name as syn_name ,base_object_name as object_ref
,CAST (CASE
WHEN OBJECTPROPERTYEX(object_id(name), ''''BaseType'''') = ''''U'''' THEN ''''TABLE''''
WHEN OBJECTPROPERTYEX(object_id(name), ''''BaseType'''') = ''''FN'''' THEN ''''FUNCTION''''
WHEN OBJECTPROPERTYEX(object_id(name), ''''BaseType'''') = ''''P'''' THEN ''''PROCEDURE''''
WHEN OBJECTPROPERTYEX(object_id(name), ''''BaseType'''') = ''''V'''' THEN ''''VIEW''''
ELSE OBJECTPROPERTYEX(object_id(name), ''''BaseType'''')
END as varchar )as object_type
FROM sys.synonyms '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--procedures
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_procedures' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_procedures ( routine_schema varchar, routine_name varchar, routine_definition varchar) SERVER %s OPTIONS
(query ''SELECT ROUTINE_SCHEMA as routine_schema ,ROUTINE_NAME as routine_name,ROUTINE_DEFINITION as routine_definition
FROM INFORMATION_SCHEMA.routines WHERE ROUTINE_TYPE = ''''PROCEDURE '''' '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--functions
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_functions' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_functions ( routine_schema varchar, routine_name varchar, routine_definition varchar) SERVER %s OPTIONS
(query ''SELECT ROUTINE_SCHEMA as routine_schema ,ROUTINE_NAME as routine_name,ROUTINE_DEFINITION as routine_definition
FROM INFORMATION_SCHEMA.routines WHERE ROUTINE_TYPE = ''''FUNCTION '''' '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--table triggers
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_triggers' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_triggers ( trigger_name varchar, table_schema varchar, table_name varchar,isupdate int,
isdelete int,isinsert int,isafter int,isinsteadof int,disabled int,trigger_code varchar) SERVER %s OPTIONS
(query ''SELECT o.name AS trigger_name ,s.name AS table_schema,OBJECT_NAME(parent_obj) AS table_name ,OBJECTPROPERTY( o.id, ''''ExecIsUpdateTrigger'''') AS isupdate
,OBJECTPROPERTY( o.id, ''''ExecIsDeleteTrigger'''') AS isdelete ,OBJECTPROPERTY( o.id, ''''ExecIsInsertTrigger'''') AS isinsert
,OBJECTPROPERTY( o.id, ''''ExecIsAfterTrligger'''') AS isafter,OBJECTPROPERTY( o.id, ''''ExecIsInsteadOfTrigger'''') AS isinsteadof
,OBJECTPROPERTY(o.id, ''''ExecIsTriggerDisabled'''') AS [disabled] ,sysComments.text as trigger_code
FROM sysobjects o
INNER JOIN sysusers ON o.uid = sysusers.uid
INNER JOIN sys.tables t ON o.parent_obj = t.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN sys.sysComments ON o.ID = sysComments.ID
WHERE o.type = ''''TR'''' '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--data types
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_datatypes' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_datatypes ( typ varchar, dat_typ varchar ) SERVER %s OPTIONS
(query '' SELECT DISTINCT ''''TYPE BASE'''' as typ, DATA_TYPE as dat_typ FROM INFORMATION_SCHEMA.COLUMNS
UNION
SELECT DISTINCT ''''CUSTOM TYPE'''' as typ, DOMAIN_NAME as dat_typ FROM INFORMATION_SCHEMA.COLUMNS where DOMAIN_NAME IS NOT NULL
ORDER BY 1 DESC '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--database triggers
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_database_triggers' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_database_triggers ( name varchar, parent_class_desc varchar, type_desc varchar, definition varchar ) SERVER %s OPTIONS
(query '' SELECT tr.name,tr.parent_class_desc, tre.type_desc, m.definition FROM sys.triggers tr
JOIN sys.sql_modules m ON tr.object_id =m.object_id
JOIN sys.trigger_events tre ON tr.object_id = tre.object_id
WHERE parent_class =0 AND parent_class_desc =''''DATABASE'''' '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--database roles members
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_roles_members' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_roles_members ( databaserolename varchar, databaseusername varchar ) SERVER %s OPTIONS
(query '' SELECT DP1.name AS databaserolename,
isnull (DP2.name, ''''No members'''') AS databaseusername
FROM sys.database_role_members AS DRM
RIGHT OUTER JOIN sys.database_principals AS DP1
ON DRM.role_principal_id = DP1.principal_id
LEFT OUTER JOIN sys.database_principals AS DP2
ON DRM.member_principal_id = DP2.principal_id
--WHERE DP1.type = R
WHERE isnull (DP2.name, ''''No members'''')<>''''No members''''
ORDER BY DP1.name; '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--database grants
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_grants' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_grants ( class_desc varchar, sch varchar, obj varchar, usr varchar, permission_name varchar, state_desc varchar) SERVER %s OPTIONS
(query '' SELECT
class_desc, isnull(schema_name(o.uid),'''''''') as sch
, CASE WHEN class = 0 THEN DB_NAME()
WHEN class = 1 THEN OBJECT_NAME(major_id)
WHEN class = 3 THEN SCHEMA_NAME(major_id) END as obj
, USER_NAME(grantee_principal_id) as usr , permission_name, state_desc
FROM sys.database_permissions dp
LEFT OUTER JOIN sysobjects o
ON o.id = dp.major_id
where major_id >= 1 '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--database indentity columns
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_indetity_columns' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_indetity_columns ( sch varchar, tab varchar, col varchar, typ varchar, curval varchar) SERVER %s OPTIONS
(query '' SELECT TABLE_SCHEMA as sch ,TABLE_NAME tab ,COLUMN_NAME col , DATA_TYPE typ , IDENT_CURRENT(TABLE_NAME) AS curval
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, ''''IsIdentity'''') = 1
ORDER BY TABLE_NAME '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--database jobs
command:= 'DROP FOREIGN TABLE IF EXISTS msmov.mssql_jobs' ;
EXECUTE format(command);
command:= 'CREATE FOREIGN TABLE msmov.mssql_jobs ( job_id varchar, name varchar, enabled int, description varchar, step_name varchar, command varchar, server varchar, database_name varchar) SERVER %s OPTIONS
(query '' SELECT job.job_id, name, enabled, description, step_name, command, server, database_name
FROM msdb.dbo.sysjobs job
INNER JOIN msdb.dbo.sysjobsteps steps ON job.job_id = steps.job_id
WHERE job.enabled = 1; '',row_estimate_method ''showplan_all'')';
EXECUTE format(command,$1);
--results of analysis
--results is composite type: msmov.estimation_typ
--version
EXECUTE format('SELECT left(version,50) FROM msmov.mssql_version') into result.details;
result.details:= 'Version: '||result.details;
return next result;
--database name
EXECUTE format('SELECT database_name FROM msmov.mssql_db_size') into result.details;
result.details:='Database name: '||result.details;
return next result;