| 1 | package DBIO::PostgreSQL::Diff::Trigger; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL triggers | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 103 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 103 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 103 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 103 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 103 | ▶ | use DBIO::PostgreSQL::Diff::NoStatement (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 103 | ▶ | use DBIO::PostgreSQL::Diff::View (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 103 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 103 | ▶ | use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 103 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||
| 17 | table_key schema_name table_name trigger_name trigger_info replaced | ||||||||||||||||||||||||||||
| 18 | )); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # The schema_name / table_name pair of an operation, from the entry filed | ||||||||||||||||||||||||||||
| 29 | # under the table key. | ||||||||||||||||||||||||||||
| 30 | 29 | ▶ | sub _table_names { | ||||||||||||||||||||||||||
|
Subroutine
_table_names: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 29 | my ($table_key, @entries) = @_; | |||||||||||||||||||||||||||
| 32 | 29 | my ($table_name, $schema_name) = reverse | |||||||||||||||||||||||||||
| 33 | DBIO::PostgreSQL::Introspect->key_names($table_key, table_name => @entries); | ||||||||||||||||||||||||||||
| 34 | 29 | return (schema_name => $schema_name, table_name => $table_name); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | sub _refuse_without_definition { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_without_definition: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 5 | my ($table_key, $name, $in_database) = @_; | |||||||||||||||||||||||||||
| 39 | 5 | DBIO::PostgreSQL::Diff::NoStatement->refuse_without_definition( | |||||||||||||||||||||||||||
| 40 | kind => 'trigger', | ||||||||||||||||||||||||||||
| 41 | name => "$name on $table_key", | ||||||||||||||||||||||||||||
| 42 | in_database => $in_database, | ||||||||||||||||||||||||||||
| 43 | from => 'pg_get_triggerdef', | ||||||||||||||||||||||||||||
| 44 | null_when_gone => 1, | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 26 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 26 | my ($class, $source, $target, $source_tables, $target_tables, $source_cols, $target_cols) = @_; | |||||||||||||||||||||||||||
| 50 | 26 | my @ops; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # The views that are dropped and created again have lost their triggers by | ||||||||||||||||||||||||||||
| 53 | # the time these operations run. | ||||||||||||||||||||||||||||
| 54 | 26 | my $rebuilt = DBIO::PostgreSQL::Diff::View->rebuilt( | |||||||||||||||||||||||||||
| 55 | $source_tables, $target_tables, $source_cols, $target_cols); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 26 | for my $table_key (sort keys %$target) { | |||||||||||||||||||||||||||
| 58 | 22 | ▶ | my $src_trgs = $rebuilt->{$table_key} ? {} : $source->{$table_key} // {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$source->{$table_key} // {}
MC/DC100%1/1
$source->{$table_key} // {}
$source->{$table_key}
Truth table100%2/2
A
A= $source->{$table_key}
| |||||||||||||||||||||||||||||
| 59 | 22 | my $tgt_trgs = $target->{$table_key}; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 22 | for my $name (sort keys %$tgt_trgs) { | |||||||||||||||||||||||||||
| 62 | # The trigger of a rebuilt view is created, and is in the database. | ||||||||||||||||||||||||||||
| 63 | 23 | ▶ | my $in_database = exists(($source->{$table_key} // {})->{$name}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$source->{$table_key} // {}
MC/DC100%1/1
$source->{$table_key} // {}
$source->{$table_key}
Truth table100%2/2
A
A= $source->{$table_key}
| |||||||||||||||||||||||||||||
| 64 | 23 | ▶ | my $tgt_def = $tgt_trgs->{$name}{definition} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$tgt_trgs->{$name}{"definition"} // ""
MC/DC100%1/1
$tgt_trgs->{$name}{"definition"} // ""
$tgt_trgs->{$name}{"definition"}
Truth table100%2/2
A
A= $tgt_trgs->{$name}{"definition"}
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 23 | ▶ | if (!exists $src_trgs->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 12 | ▶ | _refuse_without_definition($table_key, $name, $in_database) unless $tgt_def =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 69 | action => 'create', | ||||||||||||||||||||||||||||
| 70 | table_key => $table_key, | ||||||||||||||||||||||||||||
| 71 | _table_names($table_key, $tgt_trgs->{$name}), | ||||||||||||||||||||||||||||
| 72 | trigger_name => $name, | ||||||||||||||||||||||||||||
| 73 | 9 | trigger_info => $tgt_trgs->{$name}, | |||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | 9 | next; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | # Changed: compare definitions | ||||||||||||||||||||||||||||
| 78 | 11 | ▶ | my $src_def = $src_trgs->{$name}{definition} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$src_trgs->{$name}{"definition"} // ""
MC/DC100%1/1
$src_trgs->{$name}{"definition"} // ""
$src_trgs->{$name}{"definition"}
Truth table100%2/2
A
A= $src_trgs->{$name}{"definition"}
| |||||||||||||||||||||||||||||
| 79 | 11 | ▶ | if ($src_def ne $tgt_def) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 6 | ▶ | _refuse_without_definition($table_key, $name, $in_database) unless $tgt_def =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 82 | action => 'drop', | ||||||||||||||||||||||||||||
| 83 | table_key => $table_key, | ||||||||||||||||||||||||||||
| 84 | _table_names($table_key, $src_trgs->{$name}), | ||||||||||||||||||||||||||||
| 85 | trigger_name => $name, | ||||||||||||||||||||||||||||
| 86 | 4 | trigger_info => $src_trgs->{$name}, | |||||||||||||||||||||||||||
| 87 | replaced => 1, | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 90 | action => 'create', | ||||||||||||||||||||||||||||
| 91 | table_key => $table_key, | ||||||||||||||||||||||||||||
| 92 | _table_names($table_key, $tgt_trgs->{$name}), | ||||||||||||||||||||||||||||
| 93 | trigger_name => $name, | ||||||||||||||||||||||||||||
| 94 | 4 | trigger_info => $tgt_trgs->{$name}, | |||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 21 | for my $table_key (sort keys %$source) { | |||||||||||||||||||||||||||
| 101 | 21 | ▶ | next if $rebuilt->{$table_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 19 | my $src_trgs = $source->{$table_key}; | |||||||||||||||||||||||||||
| 103 | 19 | ▶ | my $tgt_trgs = $target->{$table_key} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target->{$table_key} // {}
MC/DC100%1/1
$target->{$table_key} // {}
$target->{$table_key}
Truth table100%2/2
A
A= $target->{$table_key}
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 19 | for my $name (sort keys %$src_trgs) { | |||||||||||||||||||||||||||
| 106 | 21 | ▶ | next if exists $tgt_trgs->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 108 | action => 'drop', | ||||||||||||||||||||||||||||
| 109 | table_key => $table_key, | ||||||||||||||||||||||||||||
| 110 | _table_names($table_key, $src_trgs->{$name}), | ||||||||||||||||||||||||||||
| 111 | trigger_name => $name, | ||||||||||||||||||||||||||||
| 112 | 12 | trigger_info => $src_trgs->{$name}, | |||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 21 | return @ops; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 107 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 107 | my ($self) = @_; | |||||||||||||||||||||||||||
| 123 | 107 | my $table = quote_qualified( | |||||||||||||||||||||||||||
| 124 | DBIO::PostgreSQL::Introspect->key_names($self->table_key, table_name => $self)); | ||||||||||||||||||||||||||||
| 125 | 107 | ▶ | my $action = $self->action // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->action // ""
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
| |||||||||||||||||||||||||||||
| 126 | 107 | ▶ | my $object = sprintf 'trigger %s on %s', $self->trigger_name // '', $self->table_key // ''; | ||||||||||||||||||||||||||
|
Condition50%2/4
$self->trigger_name // ""
$self->table_key // ""
MC/DC0%0/1
$self->trigger_name // ""
$self->trigger_name
MC/DC0%0/1
$self->table_key // ""
$self->table_key
Truth table50%1/2
A
A= $self->trigger_name
Truth table50%1/2
A
A= $self->table_key
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 107 | ▶ | if ($action eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 129 | 40 | ▶ | my $def = ($self->trigger_info // {})->{definition}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->trigger_info // {}
MC/DC0%0/1
$self->trigger_info // {}
$self->trigger_info
Truth table50%1/2
A
A= $self->trigger_info
| |||||||||||||||||||||||||||||
| 130 | 40 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $def and $def =~ /\S/
MC/DC50%1/2
defined $def and $def =~ /\S/
defined $def
$def =~ /\S/
Truth table66%2/3
A and B
A= defined $defB= $def =~ /\S/
| |||||||||||||||||||||||||||||
| 131 | $self, $object, 'its trigger_info has no definition', | ||||||||||||||||||||||||||||
| 132 | ) unless defined $def && $def =~ /\S/; | ||||||||||||||||||||||||||||
| 133 | 39 | return "$def;"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | elsif ($action eq 'drop') { | ||||||||||||||||||||||||||||
| 136 | 66 | return sprintf 'DROP TRIGGER %s ON %s;', quote_ident($self->trigger_name), $table; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 1 | DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 5 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 144 | 5 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 5 | return sprintf ' %strigger: %s on %s', $prefix, $self->trigger_name, $self->table_key; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 103 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||