Firebird/Diff/Constraint.pm

filter: Statementstmt 99.2% 135 / 136 Branchbran 96.0% 48 / 50 Conditioncond 72.2% 52 / 72 MC/DCmcdc 43.2% 16 / 37 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 86.0% 271 / 315 CC 72 SCAR 33.5
CC
72
Cov
91%
diff
49.1
_fk_key
7.5
__ANON__
5.0
CRAP 28.6
« Firebird/Storage/Common.pm ↑ Summary Firebird/Type.pm »
1 package DBIO::Firebird::Diff::Constraint;
2 # ABSTRACT: Diff operations for Firebird primary keys, foreign keys and unique constraints
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.900007';
8  
9 13 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use DBIO::Firebird::DDL qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 _firebird_quote_ident _firebird_add_foreign_key _firebird_add_unique
14 13 );
15  
16  
17  
18 # new() and the action accessor come from DBIO::Diff::Op.
19 13 __PACKAGE__->mk_diff_accessors(qw/table_name kind constraint_name info replaced/);
20  
21  
22 24 ▶sub _fk_key {
Subroutine _fk_key: called CC=7 SCAR=20.2
23 24 my ($fk) = @_;
24 24 ▶ return join "\0", join(',', @{ $fk->{from_columns} // [] }), $fk->{to_table} // '',
Condition50%2/4
$fk->{"from_columns"} // []
l!l
240
$fk->{"to_table"} // ""
l!l
240
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
Truth table50%1/2
A
A= $fk->{"to_table"}
Aresult
11
00
25 24 ▶ join(',', @{ $fk->{to_columns} // [] }),
Condition50%1/2
$fk->{"to_columns"} // []
l!l
240
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
26 24 ▶ map { uc($_ // 'RESTRICT') eq 'NO ACTION' ? 'NO ACTION' : uc($_ // 'RESTRICT') } @{$fk}{qw(on_update on_delete)};
Branch: 1 branch
BranchTrueFalseTotal
uc($_ // 'RESTRICT') eq 'NO ACTION' ? : 0 48 48
Condition100%4/4
$_ // "RESTRICT"
l!l
435
$_ // "RESTRICT"
l!l
435
MC/DC100%1/1
$_ // "RESTRICT"
$_
MC/DC100%1/1
$_ // "RESTRICT"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
Truth table100%2/2
A
A= $_
Aresult
11
00
27 }
28  
29 16 ▶sub _unique_key { join ',', @{ $_[0][1] // [] } }
Subroutine _unique_key: called CC=2 SCAR=7.2
Condition50%1/2
$_[0][1] // []
l!l
160
MC/DC0%0/1
$_[0][1] // []
$_[0][1]
Truth table50%1/2
A
A= $_[0][1]
Aresult
11
00
30  
31 # The primary key is not a model section of its own: it is the is_pk /
32 # pk_position of the columns. Its columns in key order; its name is the
33 # primary_key_name of the table.
34 276 ▶sub _pk_columns {
Subroutine _pk_columns: called CC=4 SCAR=14.5
35 276 my ($cols) = @_;
36 24 return [ map { $_->{column_name} }
37 4 ▶ sort { ($a->{pk_position} // 0) <=> ($b->{pk_position} // 0) }
Condition50%2/4
$DBIO::Firebird::Diff::Constraint::a->{"pk_position"} // 0
l!l
40
$DBIO::Firebird::Diff::Constraint::b->{"pk_position"} // 0
l!l
40
MC/DC0%0/1
$DBIO::Firebird::Diff::Constraint::a->{"pk_position"} // 0
$DBIO::Firebird::Diff::Constraint::a->{"pk_position"}
MC/DC0%0/1
$DBIO::Firebird::Diff::Constraint::b->{"pk_position"} // 0
$DBIO::Firebird::Diff::Constraint::b->{"pk_position"}
Truth table50%1/2
A
A= $DBIO::Firebird::Diff::Constraint::a->{"pk_position"}
Aresult
11
00
Truth table50%1/2
A
A= $DBIO::Firebird::Diff::Constraint::b->{"pk_position"}
Aresult
11
00
38 276 ▶ grep { $_->{is_pk} } @{ $cols // [] } ];
Condition50%1/2
$cols // []
l!l
2760
MC/DC0%0/1
$cols // []
$cols
Truth table50%1/2
A
A= $cols
Aresult
11
00
39 }
40  
41 # What a key is over, as the set a foreign key's target is matched against.
42 11 ▶sub _column_set { join ',', sort @{ $_[0] // [] } }
Subroutine _column_set: called CC=2 SCAR=7.2
Condition50%1/2
$_[0] // []
l!l
110
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
43  
44 146 ▶sub diff {
Subroutine diff: called CC=48 SCAR=38.9
Pod documented
45 146 my ($class, $src_uq, $tgt_uq, $src_fk, $tgt_fk, $src_tables, $tgt_tables, $src_cols, $tgt_cols) = @_;
46 146 ▶ $_ //= {} for $src_uq, $tgt_uq, $src_fk, $tgt_fk, $src_tables, $tgt_tables, $src_cols, $tgt_cols;
Condition100%2/2
$_ //= {}
l!l
110464
MC/DC100%1/1
$_ //= {}
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
47  
48 146 my (@unique, @fk, %added, %removed, %want_fk);
49 146 my %names = map { $_ => 1 } keys %$tgt_uq, keys %$tgt_fk, keys %$src_uq, keys %$src_fk,
50 keys %$tgt_cols, keys %$src_cols;
51  
52 146 my %dropped_key; # "table\0column set" of a primary key / unique constraint dropped
53 my @existing;
54  
55 146 for my $table (sort keys %names) {
56 192 ▶ if (exists $tgt_tables->{$table} && !exists $src_tables->{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tgt_tables->{$table} and not exists $src_tables->{$table}) 26 166 192
Condition100%3/3
exists $tgt_tables->{$table} and not exists $src_tables->{$table}
!ll&&!rl&&r
2314326
MC/DC100%2/2
exists $tgt_tables->{$table} and not exists $src_tables->{$table}
exists $tgt_tables->{$table} not exists $src_tables->{$table}
Truth table100%3/3
A and not(B)
A= exists $tgt_tables->{$table}B= not exists $src_tables->{$table}
ABresult
0X0
100
111
57 push @unique, $class->new(
58 action => 'create', kind => 'unique', table_name => $table,
59 constraint_name => $_->[0], info => $_,
60 26 ▶ ) for @{ $tgt_uq->{$table} // [] };
Condition100%2/2
$tgt_uq->{$table} // []
l!l
125
MC/DC100%1/1
$tgt_uq->{$table} // []
$tgt_uq->{$table}
Truth table100%2/2
A
A= $tgt_uq->{$table}
Aresult
11
00
61 push @fk, $class->new(
62 action => 'create', kind => 'fk', table_name => $table,
63 constraint_name => $_->{constraint_name}, info => $_,
64 0 ▶ ) for sort { $a->{constraint_name} cmp $b->{constraint_name} } @{ $tgt_fk->{$table} // [] };
Condition100%2/2
$tgt_fk->{$table} // []
l!l
323
MC/DC100%1/1
$tgt_fk->{$table} // []
$tgt_fk->{$table}
Truth table100%2/2
A
A= $tgt_fk->{$table}
Aresult
11
00
65 26 next;
66 }
67 166 ▶ next unless exists $tgt_tables->{$table} && exists $src_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tgt_tables->{$table} and exists $src_tables->{$table} 23 143 166
Condition66%2/3
exists $tgt_tables->{$table} and exists $src_tables->{$table}
!ll&&!rl&&r
230143
MC/DC50%1/2
exists $tgt_tables->{$table} and exists $src_tables->{$table}
exists $tgt_tables->{$table} exists $src_tables->{$table}
Truth table66%2/3
A and B
A= exists $tgt_tables->{$table}B= exists $src_tables->{$table}
ABresult
0X0
100
111
68 # A view has no constraints of its own.
69 143 ▶ next if ($src_tables->{$table}{kind} // '') eq 'view' || ($tgt_tables->{$table}{kind} // '') eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if ($src_tables->{$table}{"kind"} // "") eq "view" or ($tgt_tables->{$table}{"kind"} // "") eq "view" 5 138 143
Condition100%7/7
($src_tables->{$table}{"kind"} // "") eq "view" or ($tgt_tables->{$table}{"kind"} // "") eq "view"
l!l&&r!l&&!r
41138
$src_tables->{$table}{"kind"} // ""
l!l
1403
$tgt_tables->{$table}{"kind"} // ""
l!l
1363
MC/DC50%1/2
($src_tables->{$table}{"kind"} // "") eq "view" or ($tgt_tables->{$table}{"kind"} // "") eq "view"
$src_tables->{$table}{"kind"} $tgt_tables->{$table}{"kind"}
Truth table66%2/3
A or B
A= $src_tables->{$table}{"kind"}B= $tgt_tables->{$table}{"kind"}
ABresult
1X1
011
000
70 138 push @existing, $table;
71  
72 138 my ($have_pk, $want_pk) = (_pk_columns($src_cols->{$table}), _pk_columns($tgt_cols->{$table}));
73 138 ▶ if (join(',', @$have_pk) ne join(',', @$want_pk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (join(",", @$have_pk) ne join(",", @$want_pk)) 6 132 138
74 6 ▶ push @{ $removed{$table} }, [ pk => $have_pk, $src_tables->{$table}{primary_key_name} ] if @$have_pk;
Branch: 1 branch
BranchTrueFalseTotal
if @$have_pk 5 1 6
75 6 ▶ push @{ $added{$table} }, [ pk => $want_pk ] if @$want_pk;
Branch: 1 branch
BranchTrueFalseTotal
if @$want_pk 5 1 6
76 }
77 138 ▶ for my $pair ([ unique => $src_uq, $tgt_uq, \&_unique_key, sub { $_[0][0] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
78 8 ▶ [ fk => $src_fk, $tgt_fk, \&_fk_key, sub { $_[0]{constraint_name} } ]) {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 276 my ($kind, $src, $tgt, $key, $name) = @$pair;
80 276 ▶ my %have = map { $key->($_) => $_ } @{ $src->{$table} // [] };
Condition100%2/2
$src->{$table} // []
l!l
15261
MC/DC100%1/1
$src->{$table} // []
$src->{$table}
Truth table100%2/2
A
A= $src->{$table}
Aresult
11
00
81 276 ▶ my %want = map { $key->($_) => $_ } @{ $tgt->{$table} // [] };
Condition100%2/2
$tgt->{$table} // []
l!l
17259
MC/DC100%1/1
$tgt->{$table} // []
$tgt->{$table}
Truth table100%2/2
A
A= $tgt->{$table}
Aresult
11
00
82 276 push @{ $added{$table} }, map { [ $kind, $want{$_}, $name->($want{$_}) ] } grep { !exists $have{$_} } sort keys %want;
83 276 push @{ $removed{$table} }, map { [ $kind, $have{$_}, $name->($have{$_}) ] } grep { !exists $want{$_} } sort keys %have;
84 276 ▶ %{ $want_fk{$table} } = %want if $kind eq 'fk';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "fk" 138 138 276
85 }
86 $dropped_key{ join "\0", $table, _column_set($_->[0] eq 'pk' ? $_->[1] : $_->[1][1]) } = 1
87 138 ▶ for grep { $_->[0] ne 'fk' } @{ $removed{$table} // [] };
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq 'pk' ? : 5 4 9
Condition50%1/2
$removed{$table} // []
l!l
1380
MC/DC0%0/1
$removed{$table} // []
$removed{$table}
Truth table50%1/2
A
A= $removed{$table}
Aresult
11
00
88 }
89  
90 # A foreign key that stays but points at a key dropped here: the server
91 # refuses to drop the key under it, so it goes too and comes back.
92 146 ▶ if (%dropped_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (%dropped_key) 9 137 146
93 9 for my $table (@existing) {
94 14 ▶ my %want = %{ $want_fk{$table} // {} };
Condition50%1/2
$want_fk{$table} // {}
l!l
140
MC/DC0%0/1
$want_fk{$table} // {}
$want_fk{$table}
Truth table50%1/2
A
A= $want_fk{$table}
Aresult
11
00
95 14 ▶ my %gone = map { _fk_key($_->[1]) => 1 } grep { $_->[0] eq 'fk' } @{ $removed{$table} // [] };
Condition50%1/2
$removed{$table} // []
l!l
140
MC/DC0%0/1
$removed{$table} // []
$removed{$table}
Truth table50%1/2
A
A= $removed{$table}
Aresult
11
00
96 14 ▶ for my $fk (@{ $src_fk->{$table} // [] }) {
Condition100%2/2
$src_fk->{$table} // []
l!l
311
MC/DC100%1/1
$src_fk->{$table} // []
$src_fk->{$table}
Truth table100%2/2
A
A= $src_fk->{$table}
Aresult
11
00
97 4 my $key = _fk_key($fk);
98 4 ▶ next if $gone{$key} || !exists $want{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $gone{$key} or not exists $want{$key} 2 2 4
Condition66%2/3
$gone{$key} or not exists $want{$key}
l!l&&r!l&&!r
202
MC/DC50%1/2
$gone{$key} or not exists $want{$key}
$gone{$key} not exists $want{$key}
Truth table66%2/3
A or not(B)
A= $gone{$key}B= not exists $want{$key}
ABresult
1X1
011
000
99 2 ▶ next unless $dropped_key{ join "\0", $fk->{to_table} // '', _column_set($fk->{to_columns}) };
Branch: 1 branch
BranchTrueFalseTotal
unless $dropped_key{join "\0", $fk->{"to_table"} // "", _column_set($fk->{"to_columns"})} 0 2 2
Condition50%1/2
$fk->{"to_table"} // ""
l!l
20
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"to_table"}
Aresult
11
00
100 2 push @{ $removed{$table} }, [ fk => $fk, $fk->{constraint_name} ];
101 2 push @{ $added{$table} }, [ fk => $want{$key}, $want{$key}{constraint_name} ];
102 }
103 }
104 }
105  
106 # A drop whose constraint comes back in this diff (the primary key of the
107 # table, or a unique constraint / foreign key of the same name) is the first
108 # half of a change, not a removal: marked replaced (core ADR 0080).
109 146 my %comes_back;
110 146 for my $table (@existing) {
111 138 ▶ for my $item (@{ $added{$table} // [] }) {
Condition50%1/2
$added{$table} // []
l!l
1380
MC/DC0%0/1
$added{$table} // []
$added{$table}
Truth table50%1/2
A
A= $added{$table}
Aresult
11
00
112 17 ▶ $comes_back{ join "\0", $table, $item->[0], $item->[0] eq 'pk' ? '' : ($item->[2] // '') } = 1;
Branch: 1 branch
BranchTrueFalseTotal
$item->[0] eq 'pk' ? : 5 12 17
Condition50%1/2
$item->[2] // ""
l!l
120
MC/DC0%0/1
$item->[2] // ""
$item->[2]
Truth table50%1/2
A
A= $item->[2]
Aresult
11
00
113 }
114 }
115 32 ▶ my $op = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
116 32 my ($action, $table, $item) = @_;
117 32 my ($kind, $info, $name) = @$item;
118 my $replaced = $action eq 'drop'
119 32 ▶ && $comes_back{ join "\0", $table, $kind, $kind eq 'pk' ? '' : ($name // '') } ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$action eq 'drop' && $comes_back{join "\000", $table, $kind, $kind eq 'pk' ? '' : $name // ''} ? : 9 23 32
$kind eq 'pk' ? : 5 10 15
Condition80%4/5
$action eq "drop" and $comes_back{join "\0", $table, $kind, $kind eq "pk" ? "" : $name // ""}
!ll&&!rl&&r
1769
$name // ""
l!l
100
MC/DC100%2/2
$action eq "drop" and $comes_back{join "\0", $table, $kind, $kind eq "pk" ? "" : $name // ""}
$action eq "drop" $comes_back{join "\0", $table, $kind, $kind eq "pk" ? "" : $name // ""}
MC/DC0%0/1
$name // ""
$name
Truth table100%3/3
A and B
A= $action eq "drop"B= $comes_back{join "\0", $table, $kind, $kind eq "pk" ? "" : $name // ""}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $name
Aresult
11
00
120 32 return $class->new(
121 action => $action, kind => $kind, table_name => $table,
122 constraint_name => $name, info => $info, replaced => $replaced,
123 );
124 146 };
125 146 my (@drop_fk, @drop_key, @add_key, @add_fk);
126 146 for my $table (@existing) {
127 138 ▶ for my $item (@{ $removed{$table} // [] }) {
Condition50%1/2
$removed{$table} // []
l!l
1380
MC/DC0%0/1
$removed{$table} // []
$removed{$table}
Truth table50%1/2
A
A= $removed{$table}
Aresult
11
00
128 15 ▶ push @{ $item->[0] eq 'fk' ? \@drop_fk : \@drop_key }, $op->(drop => $table, $item);
Branch: 1 branch
BranchTrueFalseTotal
$item->[0] eq 'fk' ? : 6 9 15
129 }
130 138 ▶ for my $item (@{ $added{$table} // [] }) {
Condition50%1/2
$added{$table} // []
l!l
1380
MC/DC0%0/1
$added{$table} // []
$added{$table}
Truth table50%1/2
A
A= $added{$table}
Aresult
11
00
131 17 ▶ push @{ $item->[0] eq 'fk' ? \@add_fk : \@add_key }, $op->(create => $table, $item);
Branch: 1 branch
BranchTrueFalseTotal
$item->[0] eq 'fk' ? : 6 11 17
132 }
133 }
134  
135 146 return (@unique, @fk, @drop_fk, @drop_key, @add_key, @add_fk);
136 }
137  
138 # The statement for one constraint of the model.
139 18 ▶sub _add_sql {
Subroutine _add_sql: called CC=3 SCAR=11.0
140 18 my ($table, $kind, $info) = @_;
141 18 my $table_sql = _firebird_quote_ident($table);
142 return sprintf 'ALTER TABLE %s ADD PRIMARY KEY (%s);', $table_sql,
143 18 ▶ join(', ', map { _firebird_quote_ident($_) } @$info) if $kind eq 'pk';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "pk" 4 14 18
144 return $kind eq 'unique'
145 ? _firebird_add_unique(table_sql => $table_sql, name => $info->[0], columns => $info->[1])
146 : _firebird_add_foreign_key(
147 table_sql => $table_sql, name => $info->{constraint_name},
148 from => $info->{from_columns}, to_table_sql => _firebird_quote_ident($info->{to_table}),
149 to => $info->{to_columns}, on_update => $info->{on_update}, on_delete => $info->{on_delete},
150 14 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'unique' ? : 6 8 14
151 }
152  
153 17 ▶sub _drop_sql {
Subroutine _drop_sql: called CC=5 SCAR=16.1
154 17 my ($table, $kind, $info, $name) = @_;
155 17 ▶ $name = $info->[0] if $kind eq 'unique';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "unique" 4 13 17
156 17 ▶ $name = $info->{constraint_name} if $kind eq 'fk';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "fk" 8 9 17
157 17 ▶ DBIO::Exception->throw(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 16 17
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
1016
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
158 'cannot drop the primary key of table %s: the model holds no name for it (primary_key_name of the table in the tables section)',
159 _firebird_quote_ident($table)) unless defined $name && length $name;
160 16 return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', _firebird_quote_ident($table),
161 _firebird_quote_ident($name);
162 }
163  
164  
165 35 ▶sub as_sql {
Subroutine as_sql: called CC=2 SCAR=6.9
Pod documented
166 35 my ($self) = @_;
167 35 ▶ return _drop_sql($self->table_name, $self->kind, $self->info, $self->constraint_name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 17 18 35
168 if $self->action eq 'drop';
169 18 return _add_sql($self->table_name, $self->kind, $self->info);
170 }
171  
172  
173 14 ▶sub summary {
Subroutine summary: called CC=3 SCAR=11.0
Pod documented
174 14 my ($self) = @_;
175 14 ▶ if ($self->kind eq 'pk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kind eq "pk") 6 8 14
176 return sprintf '%s primary key: %s (%s)', $self->summary_prefix,
177 6 $self->table_name, join(', ', @{ $self->info });
178 }
179 8 ▶ return sprintf '%s %s: %s.%s', $self->summary_prefix,
Branch: 1 branch
BranchTrueFalseTotal
$self->kind eq 'fk' ? : 6 2 8
180 ($self->kind eq 'fk' ? 'foreign key' : 'unique constraint'),
181 $self->table_name, $self->constraint_name;
182 }
183  
184 13 1;
185  
186 __END__
« Firebird/Storage/Common.pm ↑ Summary Firebird/Type.pm »