Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle producer add missing functions #143

Merged
merged 19 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ca601db
Added support for CURRENT_TIMESTAMP as a default value when parsing O…
hazardv Aug 10, 2022
0d8ce8f
Added tests to verify changes to oracle parser.
hazardv Aug 10, 2022
dbd1da1
Split the creation of contraints out from the creation of the table a…
hazardv Aug 18, 2022
929ae33
Missing a few small things
hazardv Aug 18, 2022
ed59a4d
Added drop_field to Oracle.pm and added alter constraint testing.
hazardv Aug 19, 2022
cdb544f
Added drop_table functionality along with related tests.
hazardv Aug 22, 2022
22a15de
Make sure the sqlt_args actually get passed in to the SQL::Translator…
hazardv Aug 24, 2022
ab42ef8
Removed batch alter stuff from Oracle.pm. It was just complicating th…
hazardv Aug 24, 2022
df7e62f
All quoting is working consistent to how it works for MySQL and SQLSe…
hazardv Aug 25, 2022
c90a599
Fixed issue with drop FK constraint syntax
hazardv Sep 7, 2022
b371f16
Fixed restriction stopping change of NOT NULL to NULL for non CLOB fi…
hazardv Sep 8, 2022
b14da74
Removed commented out section of code.
hazardv Sep 9, 2022
d2d080c
fixed issue with alter_drop_constraint when dropping unnamed primary …
hazardv Sep 26, 2022
2d47b5c
Reverted change to passing of $options and fixed test plan.
hazardv Sep 29, 2022
b4a8ca7
Added debug statements to figure out constraint order issue
hazardv Oct 5, 2022
a32992d
Separated FK_defs out so things would play nice and modified 51-xml-t…
hazardv Oct 5, 2022
090b5b4
Corrected quoting on index fields, updated 51-xml-to-oracle_quoted.t …
hazardv Oct 5, 2022
74c0312
Corrected changes previously made to 55-oracle-producer.t in error.
hazardv Oct 5, 2022
2a66d69
Removed some unnecessary debug comments.
hazardv Oct 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 68 additions & 1 deletion lib/SQL/Translator/Producer/Oracle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ $DEBUG = 0 unless defined $DEBUG;

use base 'SQL::Translator::Producer';
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(header_comment);
use SQL::Translator::Utils qw(header_comment batch_alter_table_statements);

my %translate = (
#
Expand Down Expand Up @@ -695,6 +695,73 @@ sub create_field {

}

sub batch_alter_table {
my ($table, $diff_hash, $options) = @_;

my %fks_to_alter;
my %fks_to_drop = map {
$_->type eq FOREIGN_KEY
? ( $_->name => $_ )
: ( )
} @{$diff_hash->{alter_drop_constraint} };

my %fks_to_create = map {
if ( $_->type eq FOREIGN_KEY) {
$fks_to_alter{$_->name} = $fks_to_drop{$_->name} if $fks_to_drop{$_->name};
( $_->name => $_ );
} else { ( ) }
} @{$diff_hash->{alter_create_constraint} };

my @drop_stmt;
if (scalar keys %fks_to_alter) {
$diff_hash->{alter_drop_constraint} = [
grep { !$fks_to_alter{$_->name} } @{ $diff_hash->{alter_drop_constraint} }
];

@drop_stmt = batch_alter_table($table, { alter_drop_constraint => [ values %fks_to_alter ] }, $options);

}

my @stmts = batch_alter_table_statements($diff_hash, $options);

# rename_table makes things a bit more complex
my $renamed_from = "";
$renamed_from = quote($diff_hash->{rename_table}[0][0]->name)
if $diff_hash->{rename_table} && @{$diff_hash->{rename_table}};

return unless @stmts;
# Just zero or one stmts. return now
return (@drop_stmt,@stmts) unless @stmts > 1;

# Now strip off the 'ALTER TABLE xyz' of all but the first one

my $table_name = quote($table->name);

my $re = $renamed_from
? qr/^ALTER TABLE (?:\Q$table_name\E|\Q$renamed_from\E) /
: qr/^ALTER TABLE \Q$table_name\E /;

my $first = shift @stmts;
my ($alter_table) = $first =~ /($re)/;

my $padd = " " x length($alter_table);

return @drop_stmt, join( ",\n", $first, map { s/$re//; $padd . $_ } @stmts);

}

sub drop_table {
my ($table, $options) = @_;

return (
# Drop (foreign key) constraints so table drops cleanly
batch_alter_table(
$table, { alter_drop_constraint => [ grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints ] }, $options
),
'DROP TABLE ' . quote($table),
);
}

sub alter_drop_constraint {
my ($c, $options) = @_;

Expand Down
8 changes: 6 additions & 2 deletions t/54-oracle-alter-constraint.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use SQL::Translator;
use SQL::Translator::Diff;

BEGIN {
maybe_plan(4, 'SQL::Translator::Parser::YAML',
maybe_plan(6, 'SQL::Translator::Parser::YAML',
'SQL::Translator::Producer::Oracle');
}

Expand Down Expand Up @@ -50,6 +50,10 @@ ok($diff, 'Diff generated.');

like($diff, '/ALTER TABLE d_operator DROP CONSTRAINT foo_unique/', 'DROP constraint foo_unique generated');

like($diff, '/ALTER TABLE d_operator DROP CONSTRAINT other_check/', 'DROP constraint other_check generated');
like($diff, '/DROP CONSTRAINT other_check/', 'DROP constraint other_check generated');

like($diff, '/ADD CONSTRAINT other_check CHECK \(other BETWEEN 100 and 99999\)/', 'ADD check constraint generated');

like($diff, '/ALTER TABLE supplier DROP FOREIGN KEY fk_customer/', 'DROP Foreign key constraint generated');

like($diff, '/DROP TABLE customer/', 'DROP TABLE customer generated');
49 changes: 49 additions & 0 deletions t/data/oracle/schema_diff_d.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,52 @@ schema:
- 5
name: d_operator
order: 11
supplier:
constraints:
- fields: cust_id
name: fk_customer
reference_table: customer
reference_fields:
- customer_id
type: FOREIGN KEY
fields:
id:
data_type: integer
is_nullable: 0
is_primary_key: 1
size: 11
name: id
order: 62
cust_id:
data_type: integer
is_nullable: 1
is_primary_key: 0
size: 11
name: cust_id
order: 63
supplier_name:
data_type: nvarchar2
is_nullable: 0
is_primary_key: 0
size: 256
name: supplier_name
order: 64
name: supplier
customer:
fields:
customer_id:
data_type: integer
is_nullable: 0
is_primary_key: 1
size: 11
name: customer_id
order: 65
customer_name:
data_type: nvarchar2
is_nullable: 0
is_primary_key: 0
size: 256
name: customer_name
order: 66
name: customer
order: 12
24 changes: 24 additions & 0 deletions t/data/oracle/schema_diff_e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,27 @@ schema:
- 5
name: d_operator
order: 11
supplier:
fields:
id:
data_type: integer
is_nullable: 0
is_primary_key: 1
size: 11
name: id
order: 62
cust_id:
data_type: integer
is_nullable: 1
is_primary_key: 0
size: 11
name: cust_id
order: 63
supplier_name:
data_type: nvarchar2
is_nullable: 0
is_primary_key: 0
size: 256
name: supplier_name
order: 65
name: supplier