Skip to content

Commit

Permalink
Allow use of MODIFY_STRUCT with TABLE export. Table creation DDL will…
Browse files Browse the repository at this point in the history
… respect

the new list of columns and all indexes or foreign key pointing to or from a
column removed will not be exported. Thanks to Christian Bjornbak for the
feature request.
  • Loading branch information
darold committed Jun 18, 2017
1 parent cfc1993 commit 2315983
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,10 @@ CONFIGURATION

This will only extract columns 'id' and 'dossier' from table T_TEST1
and columns 'id' and 'fichier' from the T_TEST2 table. This
directive is only used with COPY or INSERT export.
directive can only be used with TABLE, COPY or INSERT export. With
TABLE export create table DDL will respect the new list of columns
and all indexes or foreign key pointing to or from a column removed
will not be exported.

REPLACE_TABLES
This directive allow you to remap a list of Oracle table name to a
Expand Down
7 changes: 5 additions & 2 deletions doc/Ora2Pg.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,11 @@ for example:
MODIFY_STRUCT T_TEST1(id,dossier) T_TEST2(id,fichier)

This will only extract columns 'id' and 'dossier' from table T_TEST1 and columns
'id' and 'fichier' from the T_TEST2 table. This directive is only used with COPY
or INSERT export.
'id' and 'fichier' from the T_TEST2 table. This directive can only be used with
TABLE, COPY or INSERT export. With TABLE export create table DDL will respect
the new list of columns and all indexes or foreign key pointing to or from a
column removed will not be exported.


=item REPLACE_TABLES

Expand Down
6 changes: 4 additions & 2 deletions doc/ora2pg.3
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,10 @@ for example:
.Ve
.Sp
This will only extract columns 'id' and 'dossier' from table T_TEST1 and columns
\&'id' and 'fichier' from the T_TEST2 table. This directive is only used with \s-1COPY\s0
or \s-1INSERT\s0 export.
\&'id' and 'fichier' from the T_TEST2 table. This directive can only be used with
\&\s-1TABLE, COPY\s0 or \s-1INSERT\s0 export. With \s-1TABLE\s0 export create table \s-1DDL\s0 will respect
the new list of columns and all indexes or foreign key pointing to or from a
column removed will not be exported.
.IP "\s-1REPLACE_TABLES\s0" 4
.IX Item "REPLACE_TABLES"
This directive allow you to remap a list of Oracle table name to a PostgreSQL table name during export. The value is a list of space-separated values with the following structure:
Expand Down
39 changes: 31 additions & 8 deletions lib/Ora2Pg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,13 @@ sub _init
return;
}

# Register export structure modification
if ($self->{type} =~ /^(INSERT|COPY|TABLE)$/) {
for my $t (keys %{$self->{'modify_struct'}}) {
$self->modify_struct($t, @{$self->{'modify_struct'}{$t}});
}
}

# Retreive all table information
foreach my $t (@{$self->{export_type}}) {
$self->{type} = $t;
Expand Down Expand Up @@ -1410,12 +1417,6 @@ sub _init
} else {
warn "type option must be (TABLE, VIEW, GRANT, SEQUENCE, TRIGGER, PACKAGE, FUNCTION, PROCEDURE, PARTITION, TYPE, INSERT, COPY, TABLESPACE, SHOW_REPORT, SHOW_VERSION, SHOW_SCHEMA, SHOW_TABLE, SHOW_COLUMN, SHOW_ENCODING, FDW, MVIEW, QUERY, KETTLE, DBLINK, SYNONYM, DIRECTORY, LOAD, TEST), unknown $self->{type}\n";
}
# Mofify export structure if required
if ($self->{type} =~ /^(INSERT|COPY)$/) {
for my $t (keys %{$self->{'modify_struct'}}) {
$self->modify_struct($t, @{$self->{'modify_struct'}{$t}});
}
}
$self->replace_tables(%{$self->{'replace_tables'}});
$self->replace_cols(%{$self->{'replace_cols'}});
$self->set_where_clause($self->{'global_where'}, %{$self->{'where'}});
Expand Down Expand Up @@ -6726,10 +6727,14 @@ sub _column_comments
my %data = ();
while (my $row = $sth->fetch) {
if (!$self->{schema} && $self->{export_schema}) {
$data{"$row->[3].$row->[2]"}{$row->[0]} = $row->[1];
$row->[2] = "$row->[3].$row->[2]";
}
if (!$self->{preserve_case}) {
next if (exists $self->{modify}{"\L$row->[2]\E"} && !grep(/^$row->[0]$/i, @{$self->{modify}{"\L$row->[2]\E"}}));
} else {
$data{$row->[2]}{$row->[0]} = $row->[1];
next if (exists $self->{modify}{$row->[2]} && !grep(/^$row->[0]$/i, @{$self->{modify}{$row->[2]}}));
}
$data{$row->[2]}{$row->[0]} = $row->[1];
}

return %data;
Expand Down Expand Up @@ -8075,8 +8080,14 @@ END
}

if (!$self->{schema} && $self->{export_schema}) {
next if (exists $self->{modify}{"\L$tmptable\E"} && !grep(/^$row->[0]$/i, @{$self->{modify}{"\L$tmptable\E"}}));
push(@{$data{$tmptable}{"$row->[0]"}}, (@$row, $pos, @geom_inf));
} else {
if (!$self->{preserve_case}) {
next if (exists $self->{modify}{"\L$row->[8]\E"} && !grep(/^$row->[0]$/i, @{$self->{modify}{"\L$row->[8]\E"}}));
} else {
next if (exists $self->{modify}{$row->[8]} && !grep(/^$row->[0]$/i, @{$self->{modify}{$row->[8]}}));
}
push(@{$data{"$row->[8]"}{"$row->[0]"}}, (@$row, $pos, @geom_inf));
}

Expand Down Expand Up @@ -8454,6 +8465,13 @@ END
$local_table = "$row->[10].$row->[0]";
$remote_table = "$row->[11].$row->[3]";
}
if (!$self->{preserve_case}) {
next if (exists $self->{modify}{"\L$local_table\E"} && !grep(/^$row->[2]$/i, @{$self->{modify}{"\L$local_table\E"}}));
next if (exists $self->{modify}{"\L$remote_table\E"} && !grep(/^$row->[5]$/i, @{$self->{modify}{"\L$remote_table\E"}}));
} else {
next if (exists $self->{modify}{$local_table} && !grep(/^$row->[2]$/i, @{$self->{modify}{$local_table}}));
next if (exists $self->{modify}{$remote_table} && !grep(/^$row->[5]$/i, @{$self->{modify}{$remote_table}}));
}
push(@{$data{$local_table}}, [ ($row->[1],$row->[4],$row->[6],$row->[7],$row->[8],$row->[9],$row->[11],$row->[0],$row->[10],$row->[14]) ]);
# TABLENAME CONSTNAME COLNAME
push(@{$link{$local_table}{$row->[1]}{local}}, $row->[2]);
Expand Down Expand Up @@ -8734,6 +8752,11 @@ AND IC.TABLE_OWNER = ?
if (!$self->{schema} && $self->{export_schema}) {
$row->[-6] = "$row->[-5].$row->[-6]";
}
if (!$self->{preserve_case}) {
next if (exists $self->{modify}{"\L$row->[-6]\E"} && !grep(/^$row->[1]$/i, @{$self->{modify}{"\L$row->[-6]\E"}}));
} else {
next if (exists $self->{modify}{$row->[-6]} && !grep(/^$row->[1]$/i, @{$self->{modify}{$row->[-6]}}));
}
# Show a warning when an index has the same name as the table
if ( !$self->{indexes_renaming} && !$self->{indexes_suffix} && (lc($row->[0]) eq lc($table)) ) {
print STDERR "WARNING: index $row->[0] has the same name as the table itself. Please rename it before export or enable INDEXES_RENAMING.\n";
Expand Down

0 comments on commit 2315983

Please sign in to comment.