From: Dagfinn Ilmari Mannsåker Date: Thu, 26 Jan 2017 17:42:30 +0000 (+0000) Subject: Improve trigger 'scope' attribute support (RT#119997) X-Git-Tag: v0.11022~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c0ec0e22d3f0e3852c00daac5ef5763010b410c3;p=dbsrgits%2FSQL-Translator.git Improve trigger 'scope' attribute support (RT#119997) - JSON, YAML and XML producers - XML and SQLite parsers TODO: Unify with the DB2 parser/producer's 'granularity' extra attribute --- diff --git a/Changes b/Changes index 1bcc13a..d9e4ba4 100644 --- a/Changes +++ b/Changes @@ -8,6 +8,8 @@ Changes for SQL::Translator * Add support for USING and WHERE on indexes in PostgreSQL producer and parser (RT#63814, GH#52) * Improve add_trigger consistency between producers (GH#48) + * Add trigger 'scope' attribute support to JSON, YAML and XML producers, + and XML and SQLite parsers (RT#119997) * Declare dependencies in deterministic order (RT#102859) * Multiple speedups of naive internal debugging mechanism (GH#54) * Remove dependency on List::MoreUtils ( http://is.gd/lmu_cac_debacle ) diff --git a/lib/SQL/Translator/Parser/SQLite.pm b/lib/SQL/Translator/Parser/SQLite.pm index 4787302..251c97b 100644 --- a/lib/SQL/Translator/Parser/SQLite.pm +++ b/lib/SQL/Translator/Parser/SQLite.pm @@ -730,6 +730,7 @@ sub parse { database_events => $def->{'db_events'}, action => $def->{'action'}, on_table => $def->{'on_table'}, + scope => 'row', # SQLite only supports row triggers ); } diff --git a/lib/SQL/Translator/Parser/XML/SQLFairy.pm b/lib/SQL/Translator/Parser/XML/SQLFairy.pm index 4e6e874..14d98b1 100644 --- a/lib/SQL/Translator/Parser/XML/SQLFairy.pm +++ b/lib/SQL/Translator/Parser/XML/SQLFairy.pm @@ -216,7 +216,7 @@ sub parse { foreach (@nodes) { my %data = get_tagfields($xp, $_, "sqlf:", qw/ name perform_action_when database_event database_events fields - on_table action order extra + on_table action order extra scope /); # back compat diff --git a/lib/SQL/Translator/Producer/JSON.pm b/lib/SQL/Translator/Producer/JSON.pm index 890a958..d057d8e 100644 --- a/lib/SQL/Translator/Producer/JSON.pm +++ b/lib/SQL/Translator/Producer/JSON.pm @@ -149,6 +149,7 @@ sub view_trigger { 'fields' => scalar $trigger->fields, 'on_table' => scalar $trigger->on_table, 'action' => scalar $trigger->action, + 'scope' => scalar $trigger->scope, keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (), }; } diff --git a/lib/SQL/Translator/Producer/XML/SQLFairy.pm b/lib/SQL/Translator/Producer/XML/SQLFairy.pm index 73411f6..ce9e151 100644 --- a/lib/SQL/Translator/Producer/XML/SQLFairy.pm +++ b/lib/SQL/Translator/Producer/XML/SQLFairy.pm @@ -271,7 +271,7 @@ sub produce { xml_obj_children( $xml, $schema, tag => 'trigger', methods => [qw/name database_events action on_table perform_action_when - fields order extra/], + fields order extra scope/], ); # diff --git a/lib/SQL/Translator/Producer/YAML.pm b/lib/SQL/Translator/Producer/YAML.pm index 49d7029..c5055ff 100644 --- a/lib/SQL/Translator/Producer/YAML.pm +++ b/lib/SQL/Translator/Producer/YAML.pm @@ -148,6 +148,7 @@ sub view_trigger { 'fields' => scalar $trigger->fields, 'on_table' => scalar $trigger->on_table, 'action' => scalar $trigger->action, + 'scope' => scalar $trigger->scope, keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (), }; } diff --git a/lib/Test/SQL/Translator.pm b/lib/Test/SQL/Translator.pm index 5803536..9856350 100644 --- a/lib/Test/SQL/Translator.pm +++ b/lib/Test/SQL/Translator.pm @@ -284,6 +284,9 @@ sub trigger_ok { is( $obj->on_table, $test->{on_table}, "$t_name on_table is '$test->{on_table}'" ); + is( $obj->scope, $test->{scope}, "$t_name scope is '$test->{scope}'" ) + if exists $test->{scope}; + is( $obj->action, $test->{action}, "$t_name action is '$test->{action}'" ); is_deeply( { $obj->extra }, $test->{extra}, "$t_name extra" ); diff --git a/t/16xml-parser.t b/t/16xml-parser.t index b8fad41..165d11f 100644 --- a/t/16xml-parser.t +++ b/t/16xml-parser.t @@ -27,7 +27,7 @@ use constant DEBUG => (exists $opt{d} ? 1 : 0); #============================================================================= BEGIN { - maybe_plan(238, 'SQL::Translator::Parser::XML::SQLFairy'); + maybe_plan(undef, 'SQL::Translator::Parser::XML::SQLFairy'); } my $testschema = "$Bin/data/xml/schema.xml"; @@ -235,6 +235,7 @@ schema_ok( $scma, { database_events => 'insert', on_table => 'Basic', action => 'update modified=timestamp();', + scope => 'row', extra => { foo => "bar", hello => "world", @@ -247,6 +248,7 @@ schema_ok( $scma, { database_events => 'insert,update', on_table => 'Basic', action => 'update modified2=timestamp();', + scope => 'row', extra => { hello => "aliens", }, @@ -269,3 +271,5 @@ schema_ok( $scma, { ], }); # end schema + +done_testing; diff --git a/t/17sqlfxml-producer.t b/t/17sqlfxml-producer.t index cded24b..7989e35 100644 --- a/t/17sqlfxml-producer.t +++ b/t/17sqlfxml-producer.t @@ -193,7 +193,7 @@ $ans = < - + update modified=timestamp(); @@ -222,6 +222,7 @@ EOXML database_events => [$database_event], table => $table, action => $action, + scope => 'row', extra => { hello => "world" }, ) or die $s->error; diff --git a/t/23json.t b/t/23json.t index cfc721c..0f96e47 100644 --- a/t/23json.t +++ b/t/23json.t @@ -260,7 +260,8 @@ my $json = to_json(from_json(< 1, pretty => 1 }); "name" : "pet_trig", "on_table" : "pet", "order" : "1", - "perform_action_when" : "after" + "perform_action_when" : "after", + "scope": "row" } }, "views" : { diff --git a/t/24yaml.t b/t/24yaml.t index e177907..89d4cbb 100644 --- a/t/24yaml.t +++ b/t/24yaml.t @@ -213,6 +213,7 @@ schema: on_table: pet order: 1 perform_action_when: after + scope: row views: person_pet: fields: [] diff --git a/t/46xml-to-pg.t b/t/46xml-to-pg.t index 8d8f8a6..d9dd327 100644 --- a/t/46xml-to-pg.t +++ b/t/46xml-to-pg.t @@ -66,11 +66,11 @@ CREATE VIEW "email_list" ( "email" ) AS DROP TRIGGER IF EXISTS "foo_trigger"; -CREATE TRIGGER "foo_trigger" after insert ON "Basic" update modified=timestamp();; +CREATE TRIGGER "foo_trigger" after insert ON "Basic" FOR EACH row update modified=timestamp();; DROP TRIGGER IF EXISTS "bar_trigger"; -CREATE TRIGGER "bar_trigger" before insert OR update ON "Basic" update modified2=timestamp();; +CREATE TRIGGER "bar_trigger" before insert OR update ON "Basic" FOR EACH row update modified2=timestamp();; ALTER TABLE "Basic" ADD FOREIGN KEY ("another_id") REFERENCES "Another" ("id") DEFERRABLE; diff --git a/t/data/xml/schema.xml b/t/data/xml/schema.xml index b57a4de..048d857 100644 --- a/t/data/xml/schema.xml +++ b/t/data/xml/schema.xml @@ -99,12 +99,12 @@ Created on Fri Aug 15 15:08:18 2003 + perform_action_when="after" order="1" scope="row"> update modified=timestamp(); + perform_action_when="before" order="1" scope="row"> update modified2=timestamp();