From: Mark Addison Date: Fri, 10 Mar 2006 15:04:12 +0000 (+0000) Subject: Added constraint copy to Globals filter. (Seem to be getting lots of warnings from... X-Git-Tag: v0.11008~458 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7abd9a69ef8a7eb5c3bef4a703522b3b20d4faa4;p=dbsrgits%2FSQL-Translator.git Added constraint copy to Globals filter. (Seem to be getting lots of warnings from YAML now...) --- diff --git a/lib/SQL/Translator/Filter/Globals.pm b/lib/SQL/Translator/Filter/Globals.pm index 9262590..abf5a43 100644 --- a/lib/SQL/Translator/Filter/Globals.pm +++ b/lib/SQL/Translator/Filter/Globals.pm @@ -1,7 +1,7 @@ package SQL::Translator::Filter::Globals; # ------------------------------------------------------------------- -# $Id: Globals.pm,v 1.1 2006-03-06 17:46:57 grommit Exp $ +# $Id: Globals.pm,v 1.2 2006-03-10 15:04:12 grommit Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -45,6 +45,10 @@ SQL::Translator::Filter::Globals - Add global fields and indices to all tables. fields => 'modifed', }, ] + constraints => [ + { + } + ] }, ], ) || die "SQLFairy error : ".SQL::Translator->error; @@ -61,9 +65,10 @@ sub filter { my %args = @_; my $global_table = $args{global_table} ||= '_GLOBAL_'; - my (@global_fields, @global_indices); - push @global_fields, @{ $args{fields} } if $args{fields}; - push @global_indices, @{ $args{indices} } if $args{indices}; + my (@global_fields, @global_indices, @global_constraints); + push @global_fields, @{ $args{fields} } if $args{fields}; + push @global_indices, @{ $args{indices} } if $args{indices}; + push @global_constraints, @{ $args{constraints} } if $args{constraints}; # Pull fields and indices off global table and then remove it. if ( my $gtbl = $schema->get_table( $global_table ) ) { @@ -97,6 +102,23 @@ sub filter { }; } + foreach ( $gtbl->get_constraints ) { + push @global_constraints, { + name => $_->name, + fields => [$_->fields], + deferrable => $_->deferrable, + expression => $_->expression, + match_type => $_->match_type, + options => [$_->options], + on_delete => $_->on_delete, + on_update => $_->on_update, + reference_fields => [$_->reference_fields], + reference_table => $_->reference_table, + table => $_->table, + type => $_->type, + }; + } + $schema->drop_table($gtbl); } @@ -110,10 +132,12 @@ sub filter { } foreach my $new_index ( @global_indices ) { - # Don't add if already there - #next if $tbl->get_index( $new_index->{name} ); $tbl->add_index( %$new_index ); } + + foreach my $new_constraint ( @global_constraints ) { + $tbl->add_constraint( %$new_constraint ); + } } } @@ -123,7 +147,7 @@ __END__ =head1 DESCRIPTION -Adds global fields and indices to all tables in the schema. +Adds global fields, indices and constraints to all tables in the schema. The globals to add can either be defined in the filter args or using a _GLOBAL_ table (see below). @@ -148,9 +172,10 @@ L, L Will generate duplicate indices if an index already exists on a table the same as one added globally. -=head1 TODO +Will generate duplicate constraints if a constraint already exists on a table +the same as one added globally. -Global addition of constraints. +=head1 TODO Some extra data values that can be used to control the global addition. e.g. 'skip_global'. diff --git a/t/39-filter-globals.t b/t/39-filter-globals.t index 4bc19c1..633773d 100644 --- a/t/39-filter-globals.t +++ b/t/39-filter-globals.t @@ -31,6 +31,10 @@ schema: indices: - fields: - modified + constraints: + - fields: + - modified + type: UNIQUE Person: name: Person fields: @@ -39,13 +43,27 @@ schema: name: first_name }; +# Should include the the items added from the Global table defined above in the +# schema as well as those defined in the filter args below. my $ans_yaml = qq{--- schema: procedures: {} tables: Person: comments: '' - constraints: [] + constraints: + - deferrable: 1 + expression: '' + fields: + - modified + match_type: '' + name: '' + on_delete: '' + on_update: '' + options: [] + reference_fields: [] + reference_table: '' + type: UNIQUE fields: created: comments: '' @@ -78,7 +96,7 @@ schema: extra: {} is_nullable: 1 is_primary_key: 0 - is_unique: 0 + is_unique: 1 name: modified order: 4 size: @@ -112,6 +130,7 @@ translator: version: 0.07 }; + # Parse the test XML schema my $obj; $obj = SQL::Translator->new(