Firebird/Diff.pm

filter: Statementstmt 100% 118 / 118 Branchbran 100% 26 / 26 Conditioncond 94.1% 16 / 17 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 98.4% 185 / 188 CC 25 SCAR 18.4
CC
25
Cov
99%
_case_only_pairs
10.0
_refuse_case_only_pairs
9.0
as_sql
7.0
CRAP 6.3
« Firebird/Diff/View.pm ↑ Summary Firebird/Introspect/ForeignKeys.pm »
1 package DBIO::Firebird::Diff;
2 # ABSTRACT: Compare two introspected Firebird models
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '0.900007';
8  
9 11 ▶use base 'DBIO::Diff::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 11 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use DBIO::Firebird::DDL qw(_firebird_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use DBIO::Firebird::Diff::Table ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use DBIO::Firebird::Diff::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use DBIO::Firebird::Diff::Index ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use DBIO::Firebird::Diff::Constraint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use DBIO::Firebird::Diff::View ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 146 ▶sub _build_operations {
Subroutine _build_operations: called CC=1 SCAR=0.0
21 146 my ($self) = @_;
22 146 my @ops;
23  
24 # Views to drop go first, views to create last (a changed view is both; karr k61).
25 my %views = DBIO::Firebird::Diff::View->diff(
26 146 $self->source->{tables}, $self->target->{tables});
27 146 push @ops, @{ $views{drops} };
28  
29 # The foreign keys, primary keys and unique constraints of the tables on
30 # both sides and of the new ones (karr k34, k62), ordered below.
31 my @constraints = DBIO::Firebird::Diff::Constraint->diff(
32 $self->source->{unique_constraints}, $self->target->{unique_constraints},
33 $self->source->{foreign_keys}, $self->target->{foreign_keys},
34 $self->source->{tables}, $self->target->{tables},
35 $self->source->{columns}, $self->target->{columns},
36 146 );
37 584 ▶ my $constraints = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
38 584 my ($action, @kinds) = @_;
39 584 ▶ return grep { my $o = $_; $o->action eq $action && grep { $o->kind eq $_ } @kinds } @constraints;
Condition100%3/3
$o->action eq $action && grep({$o->kind eq $_;} @kinds)
!ll&&!rl&&r
703535
MC/DC100%2/2
$o->action eq $action && grep({$o->kind eq $_;} @kinds)
$o->action eq $action grep({$o->kind eq $_;} @kinds)
Truth table100%3/3
A && B
A= $o->action eq $actionB= grep({$o->kind eq $_;} @kinds)
ABresult
0X0
100
111
40 146 };
41 my @indexes = DBIO::Firebird::Diff::Index->diff(
42 $self->source->{indexes}, $self->target->{indexes},
43 $self->source->{tables}, $self->target->{tables},
44 146 );
45  
46 # Foreign keys to drop come before the tables, so a table another one points
47 # at can go.
48 146 push @ops, $constraints->(drop => 'fk');
49 push @ops, DBIO::Firebird::Diff::Table->diff(
50 $self->source->{tables}, $self->target->{tables},
51 $self->target->{columns}, $self->target->{foreign_keys},
52 $self->source->{columns}, $self->source->{foreign_keys},
53 146 );
54 # Keys and indexes over a column go before the column does.
55 146 push @ops, $constraints->(drop => qw(pk unique));
56 146 push @ops, grep { $_->action eq 'drop' } @indexes;
57 push @ops, DBIO::Firebird::Diff::Column->diff(
58 $self->source->{columns}, $self->target->{columns},
59 $self->source->{tables}, $self->target->{tables},
60 $self->source->{unique_constraints}, $self->source->{foreign_keys},
61 146 );
62 146 push @ops, grep { $_->action ne 'drop' } @indexes;
63 146 push @ops, $constraints->(create => qw(pk unique));
64 146 push @ops, $constraints->(create => 'fk');
65  
66 146 push @ops, @{ $views{creates} };
67  
68 146 $self->_refuse_case_only_pairs(\@ops);
69  
70 129 return \@ops;
71 }
72  
73  
74 123 ▶sub as_sql {
Subroutine as_sql: called CC=7 SCAR=19.5
Pod documented
75 123 my ($self) = @_;
76  
77 160 ▶ my @refused = grep { $_->isa('DBIO::Firebird::Diff::Column') && $_->refusal }
Condition100%3/3
$_->isa('DBIO::Firebird::Diff::Column') && $_->refusal
!ll&&!rl&&r
763747
MC/DC100%2/2
$_->isa('DBIO::Firebird::Diff::Column') && $_->refusal
$_->isa('DBIO::Firebird::Diff::Column') $_->refusal
Truth table100%3/3
A && B
A= $_->isa('DBIO::Firebird::Diff::Column')B= $_->refusal
ABresult
0X0
100
111
78 123 @{ $self->operations };
79 160 ▶ my @views = grep { $_->isa('DBIO::Firebird::Diff::Table') && $_->refusal }
Condition100%3/3
$_->isa('DBIO::Firebird::Diff::Table') && $_->refusal
!ll&&!rl&&r
136222
MC/DC100%2/2
$_->isa('DBIO::Firebird::Diff::Table') && $_->refusal
$_->isa('DBIO::Firebird::Diff::Table') $_->refusal
Truth table100%3/3
A && B
A= $_->isa('DBIO::Firebird::Diff::Table')B= $_->refusal
ABresult
0X0
100
111
80 115 @{ $self->operations };
81  
82 115 ▶ DBIO::Exception->throw(join "\n",
Branch: 3 branches
BranchTrueFalseTotal
if @refused or @views 46 69 115
@views ? : 2 44 46
@refused ? : 44 2 46
Condition100%3/3
@refused or @views
l!l&&r!l&&!r
44269
MC/DC100%2/2
@refused or @views
@refused @views
Truth table100%3/3
A or B
A= @refusedB= @views
ABresult
1X1
011
000
83 (@views ? DBIO::Firebird::Diff::Table->refusal_error(@views) : ()),
84 (@refused ? DBIO::Firebird::Diff::Column->refusal_error(@refused) : ()),
85 ) if @refused || @views;
86  
87 69 return $self->SUPER::as_sql;
88 }
89  
90 # The names a list of operations drops and the names it creates, paired where
91 # they are equal but for letter case. One [ $kind, $source_name, $target_name,
92 # $table ] per pair, tables first, then columns, then indexes; $table is undef
93 # for a table.
94 #
95 # Read off the operations rather than the models: an operation is exactly what
96 # an unpaired name becomes, so what is found here is what would be rendered --
97 # and what the op classes leave out (the columns and indexes of a table that
98 # is created or dropped as a whole) is left out here too.
99 146 ▶sub _case_only_pairs {
Subroutine _case_only_pairs: called CC=10 SCAR=23.0
100 146 my ($self, $ops) = @_;
101  
102 146 my @kinds = (
103 [ table => 'DBIO::Firebird::Diff::Table', 'table_name', 'create' ],
104 [ column => 'DBIO::Firebird::Diff::Column', 'column_name', 'add' ],
105 [ index => 'DBIO::Firebird::Diff::Index', 'index_name', 'create' ],
106 );
107  
108 146 my @pairs;
109 146 for my $kind (@kinds) {
110 438 my ($word, $class, $name_of, $creates) = @$kind;
111  
112 # Columns and indexes are scoped by their table, tables by nothing.
113 438 my (%gone, %new);
114 438 for my $op (grep { $_->isa($class) } @$ops) {
115 168 ▶ my $scope = $word eq 'table' ? '' : $op->table_name;
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'table' ? : 44 124 168
116 168 ▶ if ($op->action eq 'drop') { $gone{$scope}{ $op->$name_of } = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op->action eq 'drop') { } 46 122 168
elsif ($op->action eq $creates) { } 44 78 122
117 44 elsif ($op->action eq $creates) { $new{$scope}{ $op->$name_of } = 1 }
118 }
119  
120 438 for my $scope (sort keys %gone) {
121 39 my %gone_left = %{ $gone{$scope} };
122 39 ▶ my %new_left = %{ $new{$scope} // {} };
Condition100%2/2
$new{$scope} // {}
l!l
2712
MC/DC100%1/1
$new{$scope} // {}
$new{$scope}
Truth table100%2/2
A
A= $new{$scope}
Aresult
11
00
123  
124 # A changed index is dropped and created under ONE name. That is one
125 # object; it is on neither side of a pair.
126 39 my @both = grep { $new_left{$_} } keys %gone_left;
127 39 delete @gone_left{@both};
128 39 delete @new_left{@both};
129  
130 39 for my $from (sort keys %gone_left) {
131 push @pairs,
132 21 ▶ map { [ $word, $from, $_, ($word eq 'table' ? undef : $scope) ] }
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'table' ? : 8 13 21
133 45 grep { lc $_ eq lc $from } sort keys %new_left;
134 }
135 }
136 }
137  
138 146 return @pairs;
139 }
140  
141 # Die instead of handing out operations that drop an object and create the
142 # same name in another case (karr k41). See NAMES THAT DIFFER ONLY IN CASE.
143 # Every way out the message names was run against Firebird 5.0.4, as were the
144 # two renames it says do not exist.
145 146 ▶sub _refuse_case_only_pairs {
Subroutine _refuse_case_only_pairs: called CC=9 SCAR=22.0
146 146 my ($self, $ops) = @_;
147  
148 146 ▶ my @pairs = $self->_case_only_pairs($ops) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@pairs) = $self->_case_only_pairs($ops) or return 17 129 146
149  
150 17 my (@found, %example);
151 17 for my $pair (@pairs) {
152 21 my ($kind, $from, $to, $table) = @$pair;
153 21 ▶ $example{$kind} //= $pair;
Condition66%2/3
$example{$kind} //= $pair
l!l&&r!l&&!r
2190
MC/DC0%0/2
$example{$kind} //= $pair
$example{$kind} $pair
Truth table66%2/3
A //= B
A= $example{$kind}B= $pair
ABresult
1X1
011
000
154 21 ▶ push @found, sprintf ' %s %s in the database, %s in the schema%s',
Branch: 1 branch
BranchTrueFalseTotal
defined $table ? : 13 8 21
155 $kind, _firebird_quote_ident($from), _firebird_quote_ident($to),
156 defined $table ? sprintf(' (table %s)', _firebird_quote_ident($table)) : '';
157 }
158  
159 17 my @advice;
160 17 ▶ if (my $pair = $example{table}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pair = $example{"table"}) 7 10 17
161 7 my ($from, $to) = map { _firebird_quote_ident($_) } @{$pair}[1, 2];
162 7 push @advice,
163 " table: declare it in the schema as it is stored -- table('$pair->[1]'), and its",
164 ' columns in their stored spelling too. Nothing in the database',
165 ' changes. Or move the data: Firebird cannot rename a table, so',
166 " create $to, copy the rows over from $from and drop $from.";
167 }
168 17 ▶ if (my $pair = $example{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pair = $example{"column"}) 6 11 17
169 6 my ($from, $to, $table) = map { _firebird_quote_ident($_) } @{$pair}[1, 2, 3];
170 6 push @advice,
171 ' column: declare it in the schema as it is stored, or rename it:',
172 " ALTER TABLE $table ALTER COLUMN $from TO $to;",
173 ' Firebird refuses that while a PRIMARY KEY, UNIQUE or FOREIGN KEY',
174 ' constraint uses the column: drop the constraint, rename, add it',
175 ' back.';
176 }
177 17 ▶ if (my $pair = $example{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pair = $example{"index"}) 6 11 17
178 6 my ($from, $to) = map { _firebird_quote_ident($_) } @{$pair}[1, 2];
179 6 push @advice,
180 ' index: Firebird cannot rename an index, and an index holds no data:',
181 " DROP INDEX $from;",
182 " The next diff creates $to.";
183 }
184  
185 17 ▶ DBIO::Exception->throw(join "\n",
Branch: 1 branch
BranchTrueFalseTotal
@found == 1 ? : 16 1 17
186 sprintf(
187 'refusing to diff: %d name%s in the database differ%s from the schema only in letter case',
188 scalar @found, (@found == 1 ? ('', 's') : ('s', '')),
189 ),
190 '',
191 @found,
192 '',
193 'Firebird folds an unquoted identifier to upper case, and DBIO::Firebird quotes',
194 'every identifier it emits. An object that was created unquoted -- by an earlier',
195 'DBIO::Firebird, or by hand -- is stored upper-cased and is not the object the',
196 'schema names. To this diff the two spellings are two objects: it would drop the',
197 'one in the database and create the one in the schema, and for a table or a',
198 'column that destroys the data. No statement was generated.',
199 '',
200 'What you can do:',
201 '',
202 @advice,
203 # throw() takes one newline off and appends where it was called from; the
204 # second one keeps that off the last line of advice.
205 '', '',
206 );
207 }
208  
209  
210 11 1;
211  
212 __END__
« Firebird/Diff/View.pm ↑ Summary Firebird/Introspect/ForeignKeys.pm »