PostgreSQL/Diff/Trigger.pm

filter: Statementstmt 100% 41 / 41 Branchbran 92.8% 13 / 14 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 93.4% 71 / 76 CC 16 SCAR 20.8
CC
16
Cov
95%
diff
12.0
as_sql
4.0
summary
2.0
CRAP 8.0
« PostgreSQL/Introspect/Normalize.pm ↑ Summary PostgreSQL/Introspect/Extensions.pm »
1 package DBIO::PostgreSQL::Diff::Trigger;
2 # ABSTRACT: Diff operations for PostgreSQL triggers
3  
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 __PACKAGE__->mk_diff_accessors(qw(table_key trigger_name trigger_info));
10  
11  
12  
13  
14  
15  
16 6 ▶sub diff {
Subroutine diff: called CC=12 SCAR=24.9
Pod documented
17 6 my ($class, $source, $target) = @_;
18 6 my @ops;
19  
20 6 for my $table_key (sort keys %$target) {
21 5 ▶ my $src_trgs = $source->{$table_key} // {};
Condition100%2/2
$source->{$table_key} // {}
l!l
23
MC/DC100%1/1
$source->{$table_key} // {}
$source->{$table_key}
Truth table100%2/2
A
A= $source->{$table_key}
Aresult
11
00
22 5 my $tgt_trgs = $target->{$table_key};
23  
24 5 for my $name (sort keys %$tgt_trgs) {
25 6 ▶ if (!exists $src_trgs->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_trgs->{$name}) 4 2 6
26 push @ops, $class->new(
27 action => 'create',
28 table_key => $table_key,
29 trigger_name => $name,
30 4 trigger_info => $tgt_trgs->{$name},
31 );
32 4 next;
33 }
34 # Changed: compare definitions
35 2 ▶ my $src_def = $src_trgs->{$name}{definition} // '';
Condition50%1/2
$src_trgs->{$name}{"definition"} // ""
l!l
20
MC/DC0%0/1
$src_trgs->{$name}{"definition"} // ""
$src_trgs->{$name}{"definition"}
Truth table50%1/2
A
A= $src_trgs->{$name}{"definition"}
Aresult
11
00
36 2 ▶ my $tgt_def = $tgt_trgs->{$name}{definition} // '';
Condition50%1/2
$tgt_trgs->{$name}{"definition"} // ""
l!l
20
MC/DC0%0/1
$tgt_trgs->{$name}{"definition"} // ""
$tgt_trgs->{$name}{"definition"}
Truth table50%1/2
A
A= $tgt_trgs->{$name}{"definition"}
Aresult
11
00
37 2 ▶ if ($src_def ne $tgt_def) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_def ne $tgt_def) 1 1 2
38 push @ops, $class->new(
39 action => 'drop',
40 table_key => $table_key,
41 trigger_name => $name,
42 1 trigger_info => $src_trgs->{$name},
43 );
44 push @ops, $class->new(
45 action => 'create',
46 table_key => $table_key,
47 trigger_name => $name,
48 1 trigger_info => $tgt_trgs->{$name},
49 );
50 }
51 }
52 }
53  
54 6 for my $table_key (sort keys %$source) {
55 4 my $src_trgs = $source->{$table_key};
56 4 ▶ my $tgt_trgs = $target->{$table_key} // {};
Condition100%2/2
$target->{$table_key} // {}
l!l
22
MC/DC100%1/1
$target->{$table_key} // {}
$target->{$table_key}
Truth table100%2/2
A
A= $target->{$table_key}
Aresult
11
00
57  
58 4 for my $name (sort keys %$src_trgs) {
59 4 ▶ next if exists $tgt_trgs->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_trgs->{$name} 2 2 4
60 push @ops, $class->new(
61 action => 'drop',
62 table_key => $table_key,
63 trigger_name => $name,
64 2 trigger_info => $src_trgs->{$name},
65 );
66 }
67 }
68  
69 6 return @ops;
70 }
71  
72  
73 5 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
74 5 my ($self) = @_;
75 5 ▶ if ($self->action eq 'create') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->action eq 'create') { } 3 2 5
elsif ($self->action eq 'drop') { } 2 0 2
76 3 ▶ if ($self->trigger_info->{definition}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trigger_info->{"definition"}) 2 1 3
77 2 return $self->trigger_info->{definition} . ';';
78 }
79 1 return sprintf '-- CREATE TRIGGER %s ON %s (definition unavailable)',
80 $self->trigger_name, $self->table_key;
81 }
82 elsif ($self->action eq 'drop') {
83 2 return sprintf 'DROP TRIGGER %s ON %s;', $self->trigger_name, $self->table_key;
84 }
85 }
86  
87  
88 2 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
89 2 my ($self) = @_;
90 2 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 1 1 2
91 2 return sprintf ' %strigger: %s on %s', $prefix, $self->trigger_name, $self->table_key;
92 }
93  
94 9 1;
95  
96 __END__
97  
98 =pod
99  
100 =encoding UTF-8
101  
102 =head1 NAME
103  
104 DBIO::PostgreSQL::Diff::Trigger - Diff operations for PostgreSQL triggers
105  
106 =head1 VERSION
107  
108 version 0.900001
109  
110 =head1 DESCRIPTION
111  
112 Represents a trigger-level diff operation: C<CREATE TRIGGER> or C<DROP
113 TRIGGER>. When a trigger definition changes, it produces a C<DROP> followed by
114 a C<CREATE>. Triggers are compared using the full C<pg_get_triggerdef> output.
115  
116 =head1 ATTRIBUTES
117  
118 =head2 table_key
119  
120 The C<schema.table> key identifying which table the trigger belongs to.
121  
122 =head2 trigger_name
123  
124 The trigger name.
125  
126 =head2 trigger_info
127  
128 Trigger metadata hashref (C<definition>, C<timing>, C<event>, etc.).
129  
130 =head1 METHODS
131  
132 =head2 diff
133  
134 my @ops = DBIO::PostgreSQL::Diff::Trigger->diff($source, $target);
135  
136 Compares trigger sets per table. Definition changes produce a drop-then-create
137 pair.
138  
139 =head2 as_sql
140  
141 Returns C<CREATE TRIGGER ...;> using the full definition from introspection,
142 or C<DROP TRIGGER name ON table;>.
143  
144 =head2 summary
145  
146 Returns a one-line description such as C<+trigger: users_modified_at on auth.users>.
147  
148 =head1 AUTHOR
149  
150 DBIO & DBIx::Class Authors
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 Copyright (C) 2026 DBIO Authors
155 Portions Copyright (C) 2005-2025 DBIx::Class Authors
156 Based on DBIx::Class, heavily modified.
157  
158 This is free software; you can redistribute it and/or modify it under
159 the same terms as the Perl 5 programming language system itself.
160  
161 =cut
« PostgreSQL/Introspect/Normalize.pm ↑ Summary PostgreSQL/Introspect/Extensions.pm »