Skip to content

Commit a2d0ed6

Browse files
author
Marc Cousin
committed
Added the ability to defer foreign keys validation to the unsure script: they can now be created as not valid, and validated later
1 parent 64d1652 commit a2d0ed6

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ there is a default dbo=>public remapping, that can be cancelled with -nr. Use do
9292
-num : Converts numeric (xxx,0) to the appropriate smallint, integer or bigint. It won't keep the constraint on
9393
the size of the scale of the numeric
9494

95+
-validate_constraints=yes/after/no: for foreign keys, if yes: foreign keys are created as valid in the after script (default)
96+
if no: they are created as not valid (enforced only for new rows)
97+
if after: they are created as not valid, but the statements to validate them are put in the unsure file
98+
9599
If you want to also import data:
96100

97101
> ./sqlserver2pgsql.pl -b before.sql -a after.sql -u unsure.sql -k kettledir \

regression/reg.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
foreach my $file (<*.sql>)
99
{
10-
my @options_to_try=('-i','-nr','-num', '-keep_identifier_case');
10+
my @options_to_try=('-i','-nr','-num', '-keep_identifier_case', '-validate_constraints=after');
1111
my @all_combinations=('');
1212
foreach my $option (@options_to_try)
1313
{

sqlserver2pgsql.pl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
our $after_file;
4242
our $unsure_file;
4343
our $keep_identifier_case;
44+
our $validate_constraints='yes';
4445

4546
my $template
4647
; # These two variables are loaded in the BEGIN block at the end of this file (they are very big
@@ -77,7 +78,8 @@ sub parse_conf_file
7778
'no relabel dbo' => 'norelabel_dbo',
7879
'convert numeric to int' => 'convert_numeric_to_int',
7980
'relabel schemas' => 'relabel_schemas',
80-
'keep identifier case' => 'keep_identifier_case',
81+
'keep identifier case' => 'keep_identifier_case',
82+
'validate constraints' => 'validate_constraints',
8183
);
8284

8385
# Open the conf file or die
@@ -1823,8 +1825,18 @@ sub generate_schema
18231825
{
18241826
$consdef .= " ON UPDATE CASCADE";
18251827
}
1828+
# We need a name on the constraint to be able to validate it later. Maybe it would be better to generate one
1829+
# FIXME: we'll see later if a generator is needed (probably)
1830+
if ($constraint->{TYPE} eq 'FK' and ($validate_constraints =~ /^after|no$/) and defined($constraint->{NAME}))
1831+
{
1832+
$consdef .= " NOT VALID";
1833+
}
18261834
$consdef .= ";\n";
18271835
print AFTER $consdef;
1836+
if ($constraint->{TYPE} eq 'FK' and $validate_constraints eq 'after' and defined $constraint->{NAME})
1837+
{
1838+
print UNSURE "ALTER TABLE " . format_identifier($schema) . '.' . format_identifier($table) . " VALIDATE CONSTRAINT " . format_identifier($constraint->{NAME}) . ";\n";
1839+
}
18281840
}
18291841
elsif ($constraint->{TYPE} eq 'CHECK')
18301842
{
@@ -2075,7 +2087,8 @@ sub resolve_name_conflicts
20752087
"nr" => \$norelabel_dbo,
20762088
"num" => \$convert_numeric_to_int,
20772089
"relabel_schemas=s" => \$relabel_schemas,
2078-
"keep_identifier_case" =>\$keep_identifier_case,);
2090+
"keep_identifier_case" =>\$keep_identifier_case,
2091+
"validate_constraints=s" =>\$validate_constraints,);
20792092

20802093
# We don't understand command line or have been asked for usage
20812094
if (not $options or $help)
@@ -2101,6 +2114,11 @@ sub resolve_name_conflicts
21012114
exit 1;
21022115
}
21032116

2117+
if ($validate_constraints !~ '^(yes|after|no)$')
2118+
{
2119+
die "validate_constraints should be yes, after or no (default yes)\n";
2120+
}
2121+
21042122
# We have been asked for kettle, but the compulsory parameters are not there
21052123
if ($kettle
21062124
and ( not $sd

0 commit comments

Comments
 (0)