From: Jess Robinson Date: Wed, 7 Jun 2006 16:37:33 +0000 (+0000) Subject: Update Trigger to insist on a valid table for on_table X-Git-Tag: v0.11008~428 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FSQL-Translator.git;a=commitdiff_plain;h=8ce5d6158b9fb1e09bda97b1061eea5232c268ad Update Trigger to insist on a valid table for on_table on_table now stores a known table obj in the table() attribute, which can also be used directly Updated tests to match --- diff --git a/lib/SQL/Translator/Schema/Trigger.pm b/lib/SQL/Translator/Schema/Trigger.pm index f974e8c..8aa4171 100644 --- a/lib/SQL/Translator/Schema/Trigger.pm +++ b/lib/SQL/Translator/Schema/Trigger.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Trigger; # ---------------------------------------------------------------------- -# $Id: Trigger.pm,v 1.8 2005-08-10 16:46:55 duality72 Exp $ +# $Id: Trigger.pm,v 1.9 2006-06-07 16:37:33 schiffbruechige Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -54,12 +54,12 @@ use base 'SQL::Translator::Schema::Object'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/; # ---------------------------------------------------------------------- __PACKAGE__->_attributes( qw/ - name perform_action_when database_event fields on_table action schema + name schema perform_action_when database_event fields table on_table action order /); @@ -171,22 +171,47 @@ Gets and set which fields to monitor for C. } # ---------------------------------------------------------------------- +sub table { + +=pod + +=head2 table + +Gets or set the table on which the trigger works, as a L object. + $trigger->table($triggered_table); + +=cut + + my ($self, $arg) = @_; + if ( @_ == 2 ) { + $self->error("Table attribute of a ".__PACKAGE__. + " must be a SQL::Translator::Schema::Table") + unless ref $arg and $arg->isa('SQL::Translator::Schema::Table'); + $self->{table} = $arg; + } + return $self->{table}; +} + +# ---------------------------------------------------------------------- sub on_table { =pod =head2 on_table -Gets or set the table name on which the trigger works. - - $trigger->table('foo'); +Gets or set the table name on which the trigger works, as a string. + $trigger->on_table('foo'); =cut - my $self = shift; - my $arg = shift || ''; - $self->{'on_table'} = $arg if $arg; - return $self->{'on_table'}; + my ($self, $arg) = @_; + if ( @_ == 2 ) { + my $table = $self->schema->get_table($arg); + die "Table named $arg doesn't exist" + if !$table; + $self->table($table); + } + return $self->table->name; } # ---------------------------------------------------------------------- diff --git a/t/13schema.t b/t/13schema.t index 53638f1..e72da02 100644 --- a/t/13schema.t +++ b/t/13schema.t @@ -581,6 +581,7 @@ require_ok( 'SQL::Translator::Schema' ); # { my $s = SQL::Translator::Schema->new(name => 'TrigTest'); + $s->add_table(name=>'foo') or die "Couldn't create table: ", $s->error; my $name = 'foo_trigger'; my $perform_action_when = 'after'; my $database_event = 'insert'; @@ -602,7 +603,7 @@ require_ok( 'SQL::Translator::Schema' ); qq[Perform action when is "$perform_action_when"] ); is( $t->database_event, $database_event, qq[Database event is "$database_event"] ); - is( $t->on_table, $on_table, qq[Table is "$on_table"] ); + isa_ok( $t->table, 'SQL::Translator::Schema::Table', qq[table is a Table"] ); is( $t->action, $action, qq[Action is "$action"] ); my @triggs = $s->get_triggers; diff --git a/t/16xml-parser.t b/t/16xml-parser.t index 49c0a26..3d1c568 100644 --- a/t/16xml-parser.t +++ b/t/16xml-parser.t @@ -168,7 +168,7 @@ schema_ok( $scma, { name => 'foo_trigger', perform_action_when => 'after', database_event => 'insert', - on_table => 'foo', + on_table => 'Basic', action => 'update modified=timestamp();', extra => { foo => "bar", diff --git a/t/17sqlfxml-producer.t b/t/17sqlfxml-producer.t index 21949ae..31bbf0c 100644 --- a/t/17sqlfxml-producer.t +++ b/t/17sqlfxml-producer.t @@ -23,7 +23,7 @@ my $file = "$Bin/data/mysql/sqlfxml-producer-basic.sql"; local $SIG{__WARN__} = sub { CORE::warn(@_) - unless $_[0] =~ m#XML/Writer#; + unless $_[0] =~ m!XML/Writer!; }; # Testing 1,2,3,4... @@ -171,10 +171,18 @@ my ($obj,$ans,$xml); $ans = < - + + + + + + + +
+
- + update modified=timestamp(); @@ -195,13 +203,13 @@ EOXML my $name = 'foo_trigger'; my $perform_action_when = 'after'; my $database_event = 'insert'; - my $on_table = 'foo'; my $action = 'update modified=timestamp();'; + my $table = $s->add_table( name => "Basic" ) or die $s->error; my $t = $s->add_trigger( name => $name, perform_action_when => $perform_action_when, database_event => $database_event, - on_table => $on_table, + table => $table, action => $action, extra => { hello => "world" }, ) or die $s->error; @@ -282,7 +290,7 @@ $ans = < - +
diff --git a/t/38-filter-names.t b/t/38-filter-names.t index 8802c86..670fd66 100644 --- a/t/38-filter-names.t +++ b/t/38-filter-names.t @@ -64,7 +64,7 @@ translator: producer_type: SQL::Translator::Producer::YAML show_warnings: 1 trace: 0 - version: 0.07 + version: SUPPRESSED }; # Parse the test schema @@ -89,4 +89,7 @@ my $out; lives_ok { $out = $obj->translate; } "Translate ran"; is $obj->error, '' ,"No errors"; ok $out ne "" ,"Produced something!"; +# Somewhat hackishly modify the yaml with a regex to avoid +# failing randomly on every change of version. +$out =~ s/version: .*/version: SUPPRESSED/; eq_or_diff $out, $ans_yaml ,"Output looks right"; diff --git a/t/data/xml/schema.xml b/t/data/xml/schema.xml index 7b8fa6d..c12173c 100644 --- a/t/data/xml/schema.xml +++ b/t/data/xml/schema.xml @@ -63,7 +63,7 @@ Created on Fri Aug 15 15:08:18 2003 - update modified=timestamp();