-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdb_migrator--1.1.0.sql
More file actions
2190 lines (1956 loc) · 77.5 KB
/
db_migrator--1.1.0.sql
File metadata and controls
2190 lines (1956 loc) · 77.5 KB
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
/* tools for migration of other databases to PostgreSQL */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION db_migrator" to load this file. \quit
CREATE FUNCTION adjust_to_bigint(numeric) RETURNS bigint
LANGUAGE sql STABLE CALLED ON NULL INPUT SET search_path = pg_catalog AS
$$SELECT CASE WHEN $1 < -9223372036854775808
THEN BIGINT '-9223372036854775808'
WHEN $1 > 9223372036854775807
THEN BIGINT '9223372036854775807'
ELSE $1::bigint
END$$;
CREATE FUNCTION materialize_foreign_table(
schema name,
table_name name,
with_data boolean DEFAULT TRUE,
pgstage_schema name DEFAULT NAME 'pgsql_stage'
) RETURNS boolean
LANGUAGE plpgsql VOLATILE STRICT SET search_path = pg_catalog AS
$$DECLARE
ft name;
stmt text;
statements text[];
cur_partitions refcursor;
cur_subpartitions refcursor;
partition_count integer;
subpartition_count integer;
v_schema text;
v_table text;
v_partition text;
v_subpartition text;
v_type text;
v_exp text;
v_values text[];
v_default boolean;
BEGIN
/* set "search_path" to the PostgreSQL stage, and extension schema */
EXECUTE format('SET LOCAL search_path = %I, @extschema@', pgstage_schema);
/* rename the foreign table */
ft := substr(table_name, 1, 62) || E'\x07';
statements := statements ||
format('ALTER FOREIGN TABLE %I.%I RENAME TO %I', schema, table_name, ft);
/* start a CREATE TABLE statement */
stmt := format('CREATE TABLE %I.%I (LIKE %I.%I)', schema, table_name, schema, ft);
/* cursor for partitions */
OPEN cur_partitions FOR EXECUTE
format(
E'SELECT schema, table_name, partition_name, type, key, is_default, values\n'
'FROM %I.partitions\n'
'WHERE schema = $1 AND table_name = $2',
pgstage_schema
)
USING schema, table_name;
/* the number of result rows is the partition count */
MOVE FORWARD ALL IN cur_partitions;
GET DIAGNOSTICS partition_count = ROW_COUNT;
MOVE ABSOLUTE 0 IN cur_partitions;
FETCH cur_partitions INTO
v_schema, v_table, v_partition,
v_type, v_exp, v_default, v_values;
/* add a partitioning clause if appropriate */
IF partition_count > 0 THEN
stmt := format('%s PARTITION BY %s (%s)', stmt, v_type, v_exp);
END IF;
/* create the table */
statements := statements || stmt;
/* iterate through the table's partitions (first one is already fetched) */
IF partition_count > 0 THEN
LOOP
stmt := format(
'CREATE TABLE %1$I.%3$I PARTITION OF %1$I.%2$I %4$s',
v_schema, v_table, v_partition,
CASE WHEN v_default
THEN 'DEFAULT'
WHEN v_type = 'LIST'
THEN format(
'FOR VALUES IN (%s)',
array_to_string(v_values, ',')
)
WHEN v_type = 'RANGE'
THEN format(
'FOR VALUES FROM (%s) TO (%s)',
v_values[1], v_values[2]
)
WHEN v_type = 'HASH'
THEN format(
'FOR VALUES WITH (modulus %s, remainder %s)',
partition_count, v_values[1]
)
END
);
/* cursor for the partition's subpartitions */
OPEN cur_subpartitions FOR EXECUTE
format(
E'SELECT schema, table_name, partition_name, subpartition_name, type, key, is_default, values\n'
'FROM %I.subpartitions\n'
'WHERE schema = $1 AND table_name = $2 AND partition_name = $3',
pgstage_schema
)
USING schema, table_name, v_partition;
/* the number of result rows is the sub partition count */
MOVE FORWARD ALL IN cur_subpartitions;
GET DIAGNOSTICS subpartition_count = ROW_COUNT;
MOVE ABSOLUTE 0 IN cur_subpartitions;
FETCH cur_subpartitions INTO
v_schema, v_table, v_partition, v_subpartition,
v_type, v_exp, v_default, v_values;
IF subpartition_count > 0 THEN
stmt := format('%s PARTITION BY %s (%s)', stmt, v_type, v_exp);
END IF;
/* create the partition */
statements := statements || stmt;
/* iterate through the subpartitions (first one is already fetched) */
IF subpartition_count > 0 THEN
LOOP
/* create the subpartition */
statements := statements ||
format(
'CREATE TABLE %1$I.%3$I PARTITION OF %1$I.%2$I %4$s',
v_schema, v_partition, v_subpartition,
CASE WHEN v_default
THEN 'DEFAULT'
WHEN v_type = 'LIST'
THEN format(
'FOR VALUES IN (%s)',
array_to_string(v_values, ',')
)
WHEN v_type = 'RANGE'
THEN format(
'FOR VALUES FROM (%s) TO (%s)',
v_values[1], v_values[2]
)
WHEN v_type = 'HASH'
THEN format(
'FOR VALUES WITH (modulus %s, remainder %s)',
subpartition_count, v_values[1]
)
END
);
FETCH FROM cur_subpartitions INTO
v_schema, v_table, v_partition, v_subpartition,
v_type, v_exp, v_default, v_values;
EXIT WHEN NOT FOUND;
END LOOP;
END IF;
CLOSE cur_subpartitions;
FETCH cur_partitions INTO
v_schema, v_table, v_partition,
v_type, v_exp, v_default, v_values;
EXIT WHEN NOT FOUND;
END LOOP;
END IF;
CLOSE cur_partitions;
/* move the data if desired */
IF with_data THEN
statements := statements ||
format('INSERT INTO %I.%I SELECT * FROM %I.%I', schema, table_name, schema, ft);
END IF;
/* drop the foreign table */
statements := statements ||
format('DROP FOREIGN TABLE %I.%I', schema, ft);
IF NOT execute_statements(
operation => 'copy table data',
schema => schema,
object_name => table_name,
statements => statements,
pgstage_schema => pgstage_schema
) THEN
RETURN false;
END IF;
RETURN true;
END;$$;
COMMENT ON FUNCTION materialize_foreign_table(name,name,boolean,name) IS
'turn a foreign table into a PostgreSQL table';
CREATE FUNCTION db_migrate_refresh(
plugin name,
staging_schema name DEFAULT NAME 'fdw_stage',
pgstage_schema name DEFAULT NAME 'pgsql_stage',
only_schemas name[] DEFAULT NULL
) RETURNS integer
LANGUAGE plpgsql VOLATILE CALLED ON NULL INPUT SET search_path = pg_catalog AS
$$DECLARE
old_msglevel text;
v_plugin_schema text;
v_create_metadata_views regproc;
v_translate_datatype regproc;
v_translate_identifier regproc;
v_translate_expression regproc;
c_col refcursor;
v_trans_schema name;
v_trans_table name;
v_trans_column name;
v_schema text;
v_table text;
v_column text;
v_pos integer;
v_type text;
v_length integer;
v_precision integer;
v_scale integer;
v_nullable boolean;
v_default text;
n_type text;
geom_type text;
expr text;
s text;
BEGIN
/* remember old setting */
old_msglevel := current_setting('client_min_messages');
/* make the output less verbose */
SET LOCAL client_min_messages = warning;
EXECUTE 'SET LOCAL client_min_messages = ' || old_msglevel;
SET LOCAL client_min_messages = warning;
/* get the plugin callback functions */
SELECT extnamespace::regnamespace::text INTO v_plugin_schema
FROM pg_extension
WHERE extname = plugin;
IF NOT FOUND THEN
RAISE EXCEPTION 'extension "%" is not installed', plugin;
END IF;
EXECUTE format(
E'SELECT create_metadata_views_fun,\n'
' translate_datatype_fun,\n'
' translate_identifier_fun,\n'
' translate_expression_fun\n'
'FROM %s.db_migrator_callback()',
v_plugin_schema
) INTO v_create_metadata_views,
v_translate_datatype,
v_translate_identifier,
v_translate_expression;
RAISE NOTICE 'Copying definitions to PostgreSQL staging schema "%" ...', pgstage_schema;
/* set "search_path" to the foreign stage */
EXECUTE format('SET LOCAL search_path = %I', staging_schema);
/* open cursors for columns */
OPEN c_col FOR
SELECT schema, table_name, column_name, position, type_name,
length, precision, scale, nullable, default_value
FROM columns
WHERE only_schemas IS NULL
OR schema =ANY (only_schemas);
/* set "search_path" to the PostgreSQL stage, and extension schema */
EXECUTE format('SET LOCAL search_path = %I, @extschema@', pgstage_schema);
/* refresh "schemas" table */
EXECUTE format(E'INSERT INTO schemas (schema, orig_schema)\n'
' SELECT %s(schema),\n'
' schema\n'
' FROM %I.schemas\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT schemas_pkey DO NOTHING',
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "tables" table */
EXECUTE format(E'INSERT INTO tables (schema, table_name, orig_table)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' table_name\n'
' FROM %I.tables\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT tables_pkey DO NOTHING',
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* loop through foreign columns and translate them to PostgreSQL columns */
LOOP
FETCH c_col INTO v_schema, v_table, v_column, v_pos, v_type,
v_length, v_precision, v_scale, v_nullable, v_default;
EXIT WHEN NOT FOUND;
EXECUTE format(
'SELECT %s(%L, %L, %L, %L)',
v_translate_datatype,
v_type, v_length, v_precision, v_scale
) INTO n_type;
EXECUTE format(
'SELECT %s(%L)',
v_translate_expression,
v_default
) INTO expr;
EXECUTE format(
'SELECT %s(%L)',
v_translate_identifier,
v_schema
) INTO v_trans_schema;
EXECUTE format(
'SELECT %s(%L)',
v_translate_identifier,
v_table
) INTO v_trans_table;
EXECUTE format(
'SELECT %s(%L)',
v_translate_identifier,
v_column
) INTO v_trans_column;
/* insert a row into the columns table */
INSERT INTO columns (schema, table_name, column_name, orig_column, position, type_name, orig_type, nullable, default_value)
VALUES (
v_trans_schema,
v_trans_table,
v_trans_column,
v_column,
v_pos,
n_type,
CASE WHEN position('(' in v_type) <> 0 /* type name contains modifier */
THEN v_type
WHEN v_length <> 0
THEN v_type || '(' || v_length || ')'
ELSE CASE WHEN coalesce(v_scale, 0) <> 0
THEN concat(v_type, '(' || v_precision || ',' || v_scale || ')')
ELSE CASE WHEN coalesce(v_precision, 0) <> 0
THEN v_type || '(' || v_precision || ')'
ELSE v_type
END
END
END,
v_nullable,
expr
)
ON CONFLICT ON CONSTRAINT columns_pkey DO UPDATE SET
orig_column = EXCLUDED.orig_column,
orig_type = EXCLUDED.orig_type;
END LOOP;
CLOSE c_col;
/* Refresh "checks" table.
* Don't migrate constraints of the form "col IS NOT NULL", because we expect
* "columns.nullable" to be FALSE in that case, and we'd rather define the
* column as NOT NULL than create a (possibly redundant) CHECK constraint.
*/
EXECUTE format(E'INSERT INTO checks (schema, table_name, constraint_name, orig_name, "deferrable", deferred, condition)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' %s(constraint_name),\n'
' constraint_name,\n'
' "deferrable",\n'
' deferred,\n'
' %s(condition)\n'
' FROM %I.checks\n'
' WHERE ($1 IS NULL OR schema =ANY ($1))\n'
' AND condition !~* ''^([[:alnum:]_]*|"[^"]*") IS NOT NULL$''\n'
'ON CONFLICT ON CONSTRAINT checks_pkey DO UPDATE SET\n'
' "deferrable" = EXCLUDED."deferrable",\n'
' deferred = EXCLUDED.deferred,\n'
' condition = EXCLUDED.condition',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_expression,
staging_schema)
USING only_schemas;
/* refresh "foreign_keys" table */
EXECUTE format(E'INSERT INTO foreign_keys (schema, table_name, constraint_name, orig_name, "deferrable", deferred, delete_rule,\n'
' column_name, position, remote_schema, remote_table, remote_column)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' %s(constraint_name),\n'
' constraint_name,\n'
' "deferrable",\n'
' deferred,\n'
' delete_rule,\n'
' %s(column_name),\n'
' position,\n'
' %s(remote_schema),\n'
' %s(remote_table),\n'
' %s(remote_column)\n'
' FROM %I.foreign_keys\n'
' WHERE ($1 IS NULL OR schema =ANY ($1) AND remote_schema =ANY ($1))\n'
'ON CONFLICT ON CONSTRAINT foreign_keys_pkey DO UPDATE SET\n'
' "deferrable" = EXCLUDED."deferrable",\n'
' deferred = EXCLUDED.deferred,\n'
' delete_rule = EXCLUDED.delete_rule,\n'
' column_name = EXCLUDED.column_name,\n'
' remote_schema = EXCLUDED.remote_schema,\n'
' remote_table = EXCLUDED.remote_table,\n'
' remote_column = EXCLUDED.remote_column',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "keys" table */
EXECUTE format(E'INSERT INTO keys (schema, table_name, constraint_name, orig_name, "deferrable", deferred, column_name, position, is_primary)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' %s(constraint_name),\n'
' constraint_name,\n'
' "deferrable",\n'
' deferred,\n'
' %s(column_name),\n'
' position,\n'
' is_primary\n'
' FROM %I.keys\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT keys_pkey DO UPDATE SET\n'
' "deferrable" = EXCLUDED."deferrable",\n'
' deferred = EXCLUDED.deferred,\n'
' column_name = EXCLUDED.column_name,\n'
' is_primary = EXCLUDED.is_primary',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "views" table */
EXECUTE format(E'INSERT INTO views (schema, view_name, definition, orig_def)\n'
' SELECT %s(schema),\n'
' %s(view_name),\n'
' definition,\n'
' definition\n'
' FROM %I.views\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT views_pkey DO UPDATE SET\n'
' orig_def = EXCLUDED.definition',
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "functions" table */
EXECUTE format(E'INSERT INTO functions (schema, function_name, is_procedure, source, orig_source)\n'
' SELECT %s(schema),\n'
' %s(function_name),\n'
' is_procedure,\n'
' source,\n'
' source\n'
' FROM %I.functions\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT functions_pkey DO UPDATE SET\n'
' is_procedure = EXCLUDED.is_procedure,\n'
' orig_source = EXCLUDED.source',
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "sequences" table */
EXECUTE format(E'INSERT INTO sequences (schema, sequence_name, min_value, max_value, increment_by,\n'
' cyclical, cache_size, last_value, orig_value)\n'
' SELECT %s(schema),\n'
' %s(sequence_name),\n'
' adjust_to_bigint(min_value),\n'
' adjust_to_bigint(max_value),\n'
' adjust_to_bigint(increment_by),\n'
' cyclical,\n'
' GREATEST(cache_size, 1) AS cache_size,\n'
' adjust_to_bigint(last_value),\n'
' adjust_to_bigint(last_value)\n'
' FROM %I.sequences\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT sequences_pkey DO UPDATE SET\n'
' min_value = EXCLUDED.min_value,\n'
' max_value = EXCLUDED.max_value,\n'
' increment_by = EXCLUDED.increment_by,\n'
' cyclical = EXCLUDED.cyclical,\n'
' cache_size = EXCLUDED.cache_size,\n'
' orig_value = EXCLUDED.orig_value',
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "indexes" table */
EXECUTE format(E'INSERT INTO indexes (schema, table_name, index_name, orig_name, uniqueness, where_clause)\n'
' SELECT DISTINCT %s(schema),\n'
' %s(table_name),\n'
' %s(index_name),\n'
' index_name,\n'
' uniqueness,\n'
' %s(where_clause)\n'
' FROM %I.indexes\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT indexes_pkey DO UPDATE SET\n'
' uniqueness = EXCLUDED.uniqueness',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_expression,
staging_schema)
USING only_schemas;
/* refresh "index_columns" table */
EXECUTE format(E'INSERT INTO index_columns (schema, table_name, index_name, position, descend, is_expression, column_name)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' %s(index_name),\n'
' position,\n'
' descend,\n'
' is_expression,\n'
' CASE WHEN is_expression THEN ''('' || lower(column_name) || '')'' ELSE %s(column_name)::text END\n'
' FROM %I.index_columns\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT index_columns_pkey DO UPDATE SET\n'
' table_name = EXCLUDED.table_name,\n'
' descend = EXCLUDED.descend,\n'
' is_expression = EXCLUDED.is_expression,\n'
' column_name = EXCLUDED.column_name',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "partitions" table */
EXECUTE format(E'INSERT INTO partitions (schema, table_name, partition_name, orig_name,\n'
' type, key, is_default, values)\n'
' SELECT %1$s(schema),\n'
' %1$s(table_name),\n'
' %1$s(partition_name),\n'
' partition_name,\n'
' type, %2$s(key), is_default,\n'
' array_agg(%2$s(value) ORDER BY pos)\n'
' FROM %3$I.partitions\n'
' CROSS JOIN LATERAL unnest(values) WITH ORDINALITY AS i (value, pos)\n'
' WHERE $1 IS NULL OR schema = ANY($1)\n'
' GROUP BY schema, table_name, partition_name,\n'
' type, key, is_default\n'
'ON CONFLICT ON CONSTRAINT partitions_pkey DO UPDATE SET\n'
' type = EXCLUDED.type,\n'
' key = EXCLUDED.key,\n'
' is_default = EXCLUDED.is_default,\n'
' values = EXCLUDED.values',
v_translate_identifier,
v_translate_expression,
staging_schema)
USING only_schemas;
/* refresh "subpartitions" table */
EXECUTE format(E'INSERT INTO subpartitions (schema, table_name, partition_name, subpartition_name,\n'
' orig_name, type, key, is_default, values)\n'
' SELECT %1$s(schema),\n'
' %1$s(table_name),\n'
' %1$s(partition_name),\n'
' %1$s(subpartition_name),\n'
' subpartition_name,\n'
' type, %2$s(key), is_default,\n'
' array_agg(%2$s(value) ORDER BY pos)\n'
' FROM %3$I.subpartitions\n'
' CROSS JOIN LATERAL unnest(values) WITH ORDINALITY AS i (value, pos)\n'
' WHERE $1 IS NULL OR schema = ANY($1)\n'
' GROUP BY schema, table_name, partition_name, subpartition_name,\n'
' type, key, is_default\n'
'ON CONFLICT ON CONSTRAINT subpartitions_pkey DO UPDATE SET\n'
' type = EXCLUDED.type,\n'
' key = EXCLUDED.key,\n'
' is_default = EXCLUDED.is_default,\n'
' values = EXCLUDED.values',
v_translate_identifier,
v_translate_expression,
staging_schema)
USING only_schemas;
/* refresh "triggers" table */
EXECUTE format(E'INSERT INTO triggers (schema, table_name, trigger_name, trigger_type, triggering_event,\n'
' for_each_row, when_clause, trigger_body, orig_source)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' %s(trigger_name),\n'
' trigger_type,\n'
' triggering_event,\n'
' for_each_row,\n'
' when_clause,\n'
' trigger_body,\n'
' trigger_body\n'
' FROM %I.triggers\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT triggers_pkey DO UPDATE SET\n'
' trigger_type = EXCLUDED.trigger_type,\n'
' triggering_event = EXCLUDED.triggering_event,\n'
' for_each_row = EXCLUDED.for_each_row,\n'
' when_clause = EXCLUDED.when_clause,\n'
' orig_source = EXCLUDED.orig_source',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "table_privs" table */
EXECUTE format(E'INSERT INTO table_privs (schema, table_name, privilege, grantor, grantee, grantable)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' privilege,\n'
' %s(grantor),\n'
' %s(grantee),\n'
' grantable\n'
' FROM %I.table_privs\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT table_privs_pkey DO UPDATE SET\n'
' grantor = EXCLUDED.grantor,\n'
' grantable = EXCLUDED.grantable',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* refresh "column_privs" table */
EXECUTE format(E'INSERT INTO column_privs (schema, table_name, column_name, privilege, grantor, grantee, grantable)\n'
' SELECT %s(schema),\n'
' %s(table_name),\n'
' %s(column_name),\n'
' privilege,\n'
' %s(grantor),\n'
' %s(grantee),\n'
' grantable\n'
' FROM %I.column_privs\n'
' WHERE $1 IS NULL OR schema =ANY ($1)\n'
'ON CONFLICT ON CONSTRAINT column_privs_pkey DO UPDATE SET\n'
' grantor = EXCLUDED.grantor,\n'
' grantable = EXCLUDED.grantable',
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
v_translate_identifier,
staging_schema)
USING only_schemas;
/* reset client_min_messages */
EXECUTE 'SET LOCAL client_min_messages = ' || old_msglevel;
RETURN 0;
END;$$;
COMMENT ON FUNCTION db_migrate_refresh(name,name,name,name[]) IS
'update the PostgreSQL stage with values from the remote stage';
CREATE FUNCTION db_migrate_prepare(
plugin name,
server name,
staging_schema name DEFAULT NAME 'fdw_stage',
pgstage_schema name DEFAULT NAME 'pgsql_stage',
only_schemas name[] DEFAULT NULL,
options jsonb DEFAULT NULL
) RETURNS integer
LANGUAGE plpgsql VOLATILE CALLED ON NULL INPUT SET search_path = pg_catalog AS
$$DECLARE
old_msglevel text;
v_plugin_schema text;
v_create_metadata_views regproc;
v_translate_datatype regproc;
v_translate_identifier regproc;
v_translate_expression regproc;
BEGIN
/* get the plugin callback functions */
SELECT extnamespace::regnamespace::text INTO v_plugin_schema
FROM pg_extension
WHERE extname = plugin;
IF NOT FOUND THEN
RAISE EXCEPTION 'extension "%" is not installed', plugin;
END IF;
EXECUTE format(
E'SELECT create_metadata_views_fun,\n'
' translate_datatype_fun,\n'
' translate_identifier_fun,\n'
' translate_expression_fun\n'
'FROM %s.db_migrator_callback()',
v_plugin_schema
) INTO v_create_metadata_views,
v_translate_datatype,
v_translate_identifier,
v_translate_expression;
/* remember old setting */
old_msglevel := current_setting('client_min_messages');
/* make the output less verbose */
SET LOCAL client_min_messages = warning;
EXECUTE 'SET LOCAL client_min_messages = ' || old_msglevel;
RAISE NOTICE 'Creating staging schemas "%" and "%" ...', staging_schema, pgstage_schema;
SET LOCAL client_min_messages = warning;
/* create remote staging schema */
BEGIN
EXECUTE format('CREATE SCHEMA %I', staging_schema);
EXCEPTION
WHEN insufficient_privilege THEN
RAISE insufficient_privilege USING
MESSAGE = 'you do not have permission to create a schema in this database';
WHEN duplicate_schema THEN
RAISE duplicate_schema USING
MESSAGE = 'staging schema "' || staging_schema || '" already exists',
HINT = 'Drop the staging schema first or use a different one.';
END;
/* create PostgreSQL staging schema */
BEGIN
EXECUTE format('CREATE SCHEMA %I', pgstage_schema);
EXCEPTION
WHEN duplicate_schema THEN
RAISE duplicate_schema USING
MESSAGE = 'staging schema "' || pgstage_schema || '" already exists',
HINT = 'Drop the staging schema first or use a different one.';
END;
EXECUTE 'SET LOCAL client_min_messages = ' || old_msglevel;
RAISE NOTICE 'Creating foreign metadata views in schema "%" ...', staging_schema;
SET LOCAL client_min_messages = warning;
/* create the migration views in the remote staging schema */
EXECUTE format('SET LOCAL search_path = %I', v_plugin_schema);
EXECUTE format(
'SELECT %s($1, $2, $3)',
v_create_metadata_views
) USING
server,
staging_schema,
options;
/* set "search_path" to the PostgreSQL stage */
EXECUTE format('SET LOCAL search_path = %I, %I, %s', pgstage_schema, staging_schema, v_plugin_schema);
/* create tables in the PostgreSQL stage */
CREATE TABLE schemas (
schema name NOT NULL PRIMARY KEY,
orig_schema text NOT NULL
);
CREATE TABLE tables (
schema name NOT NULL REFERENCES schemas,
table_name name NOT NULL,
orig_table text NOT NULL,
migrate boolean NOT NULL DEFAULT TRUE,
CONSTRAINT tables_pkey
PRIMARY KEY (schema, table_name)
);
CREATE TABLE columns(
schema name NOT NULL REFERENCES schemas,
table_name name NOT NULL,
column_name name NOT NULL,
orig_column text NOT NULL,
position integer NOT NULL,
type_name text NOT NULL,
orig_type text NOT NULL,
nullable boolean NOT NULL,
options jsonb,
default_value text,
CONSTRAINT columns_pkey
PRIMARY KEY (schema, table_name, column_name),
CONSTRAINT columns_unique
UNIQUE (schema, table_name, position)
);
CREATE TABLE checks (
schema name NOT NULL,
table_name name NOT NULL,
constraint_name name NOT NULL,
orig_name text NOT NULL,
"deferrable" boolean NOT NULL,
deferred boolean NOT NULL,
condition text NOT NULL,
migrate boolean NOT NULL DEFAULT TRUE,
CONSTRAINT checks_pkey
PRIMARY KEY (schema, table_name, constraint_name),
CONSTRAINT checks_fkey
FOREIGN KEY (schema, table_name) REFERENCES tables
);
CREATE TABLE foreign_keys (
schema name NOT NULL,
table_name name NOT NULL,
constraint_name name NOT NULL,
orig_name text NOT NULL,
"deferrable" boolean NOT NULL,
deferred boolean NOT NULL,
delete_rule text NOT NULL,
column_name name NOT NULL,
position integer NOT NULL,
remote_schema name NOT NULL,
remote_table name NOT NULL,
remote_column name NOT NULL,
migrate boolean NOT NULL DEFAULT TRUE,
CONSTRAINT foreign_keys_pkey
PRIMARY KEY (schema, table_name, constraint_name, position),
CONSTRAINT foreign_keys_fkey
FOREIGN KEY (schema, table_name) REFERENCES tables,
CONSTRAINT foreign_keys_remote_fkey
FOREIGN KEY (remote_schema, remote_table) REFERENCES tables
);
CREATE TABLE keys (
schema name NOT NULL,
table_name name NOT NULL,
constraint_name name NOT NULL,
orig_name text NOT NULL,
"deferrable" boolean NOT NULL,
deferred boolean NOT NULL,
column_name name NOT NULL,
position integer NOT NULL,
is_primary boolean NOT NULL,
migrate boolean NOT NULL DEFAULT TRUE,
CONSTRAINT keys_pkey
PRIMARY KEY (schema, table_name, constraint_name, position),
CONSTRAINT keys_fkey
FOREIGN KEY (schema, table_name, column_name) REFERENCES columns
);
CREATE TABLE views (
schema name NOT NULL REFERENCES schemas,
view_name name NOT NULL,
definition text NOT NULL,
orig_def text NOT NULL,
migrate boolean NOT NULL DEFAULT TRUE,
verified boolean NOT NULL DEFAULT FALSE,
CONSTRAINT views_pkey
PRIMARY KEY (schema, view_name)
);
CREATE TABLE functions (
schema name NOT NULL REFERENCES schemas,
function_name name NOT NULL,
is_procedure boolean NOT NULL,
source text NOT NULL,
orig_source text NOT NULL,
migrate boolean NOT NULL DEFAULT FALSE,
verified boolean NOT NULL DEFAULT FALSE,
CONSTRAINT functions_pkey
PRIMARY KEY (schema, function_name)
);
CREATE TABLE sequences (
schema name NOT NULL REFERENCES schemas,
sequence_name name NOT NULL,
min_value bigint,
max_value bigint,
increment_by bigint NOT NULL,
cyclical boolean NOT NULL,
cache_size integer NOT NULL,
last_value bigint NOT NULL,
orig_value bigint NOT NULL,
CONSTRAINT sequences_pkey
PRIMARY KEY (schema, sequence_name)
);
CREATE TABLE indexes (
schema name NOT NULL,
table_name name NOT NULL,
index_name name NOT NULL,
orig_name text NOT NULL,
uniqueness boolean NOT NULL,
where_clause text,
migrate boolean NOT NULL DEFAULT TRUE,
CONSTRAINT indexes_pkey
PRIMARY KEY (schema, index_name),
CONSTRAINT indexes_fkey
FOREIGN KEY (schema, table_name) REFERENCES tables,
CONSTRAINT indexes_unique
UNIQUE (schema, table_name, index_name)
);
CREATE TABLE index_columns (
schema name NOT NULL,
table_name name NOT NULL,
index_name name NOT NULL,
position integer NOT NULL,
descend boolean NOT NULL,
is_expression boolean NOT NULL,
column_name text NOT NULL,
CONSTRAINT index_columns_pkey
PRIMARY KEY (schema, index_name, position),
CONSTRAINT index_columns_fkey
FOREIGN KEY (schema, table_name, index_name)
REFERENCES indexes (schema, table_name, index_name)
);
CREATE TABLE partitions (
schema name NOT NULL,
table_name name NOT NULL,
partition_name name NOT NULL,
orig_name name NOT NULL,
type text NOT NULL,
key text NOT NULL,
is_default boolean NOT NULL DEFAULT TRUE,
values text[],
CONSTRAINT partitions_pkey
PRIMARY KEY (schema, table_name, partition_name),
CONSTRAINT partitions_fkey
FOREIGN KEY (schema, table_name) REFERENCES tables,
CONSTRAINT partition_chk_type
CHECK (type IN ('LIST', 'RANGE', 'HASH'))
);
CREATE TABLE subpartitions (
schema name NOT NULL,
table_name name NOT NULL,
partition_name name NOT NULL,
subpartition_name name NOT NULL,
orig_name name NOT NULL,
type text NOT NULL,
key text NOT NULL,
is_default boolean NOT NULL DEFAULT TRUE,
values text[],
CONSTRAINT subpartitions_pkey
PRIMARY KEY (schema, table_name, partition_name, subpartition_name),
CONSTRAINT subpartitions_fkey
FOREIGN KEY (schema, table_name, partition_name) REFERENCES partitions,
CONSTRAINT subpartition_chk_type
CHECK (type IN ('LIST', 'RANGE', 'HASH'))
);
CREATE TABLE triggers (
schema name NOT NULL,
table_name name NOT NULL,
trigger_name name NOT NULL,
trigger_type text NOT NULL,
triggering_event text NOT NULL,
for_each_row boolean NOT NULL,
when_clause text,
trigger_body text NOT NULL,
orig_source text NOT NULL,
migrate boolean NOT NULL DEFAULT FALSE,
verified boolean NOT NULL DEFAULT FALSE,
CONSTRAINT triggers_pkey
PRIMARY KEY (schema, table_name, trigger_name),
CONSTRAINT triggers_fkey
FOREIGN KEY (schema) REFERENCES schemas
);
CREATE TABLE table_privs (
schema name NOT NULL,
table_name name NOT NULL,
privilege text NOT NULL,
grantor name NOT NULL,
grantee name NOT NULL,
grantable boolean NOT NULL,
CONSTRAINT table_privs_pkey
PRIMARY KEY (schema, table_name, grantee, privilege, grantor),
CONSTRAINT table_privs_fkey
FOREIGN KEY (schema, table_name) REFERENCES tables
);
CREATE TABLE column_privs (
schema name NOT NULL,
table_name name NOT NULL,
column_name name NOT NULL,
privilege text NOT NULL,
grantor name NOT NULL,
grantee name NOT NULL,
grantable boolean NOT NULL,