From: Daniel Ruoso Date: Mon, 14 Apr 2008 14:04:44 +0000 (+0000) Subject: producer_args->{delay_constraints} can be used to add primary keys later X-Git-Tag: v0.11008~325 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=65ffb46ce790d0913350b20e5a0b9a789b052893;p=dbsrgits%2FSQL-Translator.git producer_args->{delay_constraints} can be used to add primary keys later --- diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index 59e499a..fd41b8e 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -154,7 +154,8 @@ sub produce { my $no_comments = $translator->no_comments; my $add_drop_table = $translator->add_drop_table; my $schema = $translator->schema; - my ($output, $create, @table_defs, @fk_defs, @trigger_defs, @index_defs); + my $delay_constraints = $translator->producer_args->{delay_constraints}; + my ($output, $create, @table_defs, @fk_defs, @trigger_defs, @index_defs, @constraint_defs); $create .= header_comment unless ($no_comments); @@ -169,18 +170,20 @@ sub produce { } for my $table ( $schema->get_tables ) { - my ( $table_def, $fk_def, $trigger_def, $index_def) = create_table( + my ( $table_def, $fk_def, $trigger_def, $index_def, $constraint_def) = create_table( $table, { add_drop_table => $add_drop_table, show_warnings => $WARN, no_comments => $no_comments, + delay_constraints => $delay_constraints } ); push @table_defs, @$table_def; push @fk_defs, @$fk_def; push @trigger_defs, @$trigger_def; push @index_defs, @$index_def; + push @constraint_defs, @$constraint_def; } my (@view_defs); @@ -188,7 +191,7 @@ sub produce { push @view_defs, create_view($view); } - return wantarray ? (defined $create ? $create : (), @table_defs, @view_defs, @fk_defs, @trigger_defs, @index_defs) : $create . join ("\n\n", @table_defs, @view_defs, @fk_defs, @trigger_defs, @index_defs, ''); + return wantarray ? (defined $create ? $create : (), @table_defs, @view_defs, @fk_defs, @trigger_defs, @index_defs, @constraint_defs) : $create . join ("\n\n", @table_defs, @view_defs, @fk_defs, @trigger_defs, @index_defs, @constraint_defs, ''); } sub create_table { @@ -545,8 +548,12 @@ sub create_table { my $table_options = @table_options ? "\n".join("\n", @table_options) : ''; push @create, "CREATE TABLE $table_name_ur (\n" . - join( ",\n", map { " $_" } @field_defs, @constraint_defs ) . - "\n)$table_options;"; + join( ",\n", map { " $_" } @field_defs, + ($options->{delay_constraints} ? () : @constraint_defs) ) . + "\n)$table_options;"; + + @constraint_defs = map { 'ALTER TABLE '.$table_name_ur.' ADD '.$_ } + @constraint_defs; if ( $WARN ) { if ( %truncated ) { @@ -561,7 +568,7 @@ sub create_table { } } - return \@create, \@fk_defs, \@trigger_defs, \@index_defs; + return \@create, \@fk_defs, \@trigger_defs, \@index_defs, ($options->{delay_constraints} ? \@constraint_defs : []); } sub create_view { diff --git a/t/53-oracle-delay-constraints.t b/t/53-oracle-delay-constraints.t new file mode 100644 index 0000000..a60fe20 --- /dev/null +++ b/t/53-oracle-delay-constraints.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl +use strict; + +use FindBin qw/$Bin/; +use Test::More; +use Test::SQL::Translator; +use Test::Exception; +use Data::Dumper; +use SQL::Translator; +use SQL::Translator::Schema::Constants; + +BEGIN { + maybe_plan(2, 'SQL::Translator::Parser::YAML', + 'SQL::Translator::Producer::Oracle'); +} + +my $yamlfile = "$Bin/data/oracle/schema_with_options.yaml"; + +my $sqlt; +$sqlt = SQL::Translator->new( + show_warnings => 0, + add_drop_table => 0, + producer_args => { 'delay_constraints' => 1 } +); + +my $sql_string = $sqlt->translate( + from => 'YAML', + to => 'Oracle', + filename => $yamlfile, +); + +warn $sql_string; +ok($sql_string, 'Translation successfull'); +ok($sql_string =~ /ADD PRIMARY KEY/, 'constraints delayed');