MSSQL/Diff.pm

filter: Statementstmt 100% 227 / 227 Branchbran 83.3% 25 / 30 Conditioncond 67.2% 72 / 107 MC/DCmcdc 35.5% 21 / 59 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 82.4% 366 / 444 CC 82 SCAR 36.5
CC
82
Cov
89%
_build_operations
62.3
_fold_names
17.7
__ANON__
5.0
CRAP 38.4
« MSSQL/Introspect.pm ↑ Summary MSSQL/Introspect/Columns.pm »
1 package DBIO::MSSQL::Diff;
2 # ABSTRACT: Compare two introspected MSSQL models
3  
4 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 36 our $VERSION = '0.900007';
8  
9 36 ▶use base 'DBIO::Diff::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 36 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 36 ▶use DBIO::MSSQL::Diff::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 36 ▶use DBIO::MSSQL::Diff::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 36 ▶use DBIO::MSSQL::Diff::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 36 ▶use DBIO::MSSQL::Diff::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶use DBIO::MSSQL::Diff::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶use DBIO::MSSQL::Diff::ForeignKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use DBIO::MSSQL::Diff::PrimaryKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶use DBIO::MSSQL::DDL qw(_check_cascade_paths _check_constraint_names _constraint_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 94 ▶sub _build_operations {
Subroutine _build_operations: called CC=57 SCAR=41.3
23 94 my ($self) = @_;
24  
25 # names follow the collation of the live database (karr k77)
26 94 my $target = $self->_fold_names($self->target);
27  
28 # a schema the target needs and the live database lacks (karr k72)
29 94 my @schemas = DBIO::MSSQL::Diff::Schema->diff($self->source, $target);
30  
31 my @tables = DBIO::MSSQL::Diff::Table->diff(
32 $self->source->{tables}, $target->{tables}, $target->{columns},
33 94 );
34 my @columns = DBIO::MSSQL::Diff::Column->diff(
35 $self->source->{columns}, $target->{columns},
36 $self->source->{tables}, $target->{tables},
37 94 );
38 my @views = DBIO::MSSQL::Diff::View->diff(
39 $self->source->{tables}, $target->{tables},
40 94 );
41 my @indexes = DBIO::MSSQL::Diff::Index->diff(
42 $self->source->{indexes}, $target->{indexes},
43 $self->source->{tables}, $target->{tables},
44 91 );
45 my @fks = DBIO::MSSQL::Diff::ForeignKey->diff(
46 $self->source->{foreign_keys}, $target->{foreign_keys},
47 $self->source->{tables}, $target->{tables},
48 91 );
49  
50 # The primary key of a table that exists on both sides (karr k74).
51 my @pks = DBIO::MSSQL::Diff::PrimaryKey->diff(
52 $self->source->{columns}, $target->{columns},
53 91 $self->source->{tables}, $target->{tables}, \@columns,
54 );
55  
56 # An ALTER COLUMN the server refuses while an index or unique constraint
57 # covers the column: drop those in phase 2 and create them again in phase 6
58 # (unless the diff drops or creates that very index anyway).
59 {
60 90 my %have = map { join("\0", $_->action, $_->table_name, $_->index_name) => 1 } @indexes;
61 90 ▶ my $src = $self->source->{indexes} // {};
Condition50%1/2
$self->source->{"indexes"} // {}
l!l
900
MC/DC0%0/1
$self->source->{"indexes"} // {}
$self->source->{"indexes"}
Truth table50%1/2
A
A= $self->source->{"indexes"}
Aresult
11
00
62 90 ▶ my $tgt = $target->{indexes} // {};
Condition50%1/2
$target->{"indexes"} // {}
l!l
900
MC/DC0%0/1
$target->{"indexes"} // {}
$target->{"indexes"}
Truth table50%1/2
A
A= $target->{"indexes"}
Aresult
11
00
63 90 my (@extra_drop, @extra_create);
64 90 for my $op (grep { $_->unbinds_dependents } @columns) {
65 16 my ($t, $c) = ($op->table_name, $op->column_name);
66 16 ▶ for my $name (sort keys %{ $src->{$t} // {} }) {
Condition100%2/2
$src->{$t} // {}
l!l
610
MC/DC100%1/1
$src->{$t} // {}
$src->{$t}
Truth table100%2/2
A
A= $src->{$t}
Aresult
11
00
67 14 my $i = $src->{$t}{$name};
68 14 ▶ next unless grep { $_ eq $c } @{ $i->{columns} // [] };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $c;} @{$i->{"columns"} // [];} 4 10 14
Condition50%1/2
$i->{"columns"} // []
l!l
140
MC/DC0%0/1
$i->{"columns"} // []
$i->{"columns"}
Truth table50%1/2
A
A= $i->{"columns"}
Aresult
11
00
69 push @extra_drop, DBIO::MSSQL::Diff::Index->new(action => 'drop',
70 table_name => $t, index_name => $name, index_info => $i)
71 10 ▶ unless $have{join "\0", 'drop', $t, $name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $have{join "\0", "drop", $t, $name}++ 7 3 10
72 10 my $n = $tgt->{$t}{$name};
73 push @extra_create, DBIO::MSSQL::Diff::Index->new(action => 'create',
74 table_name => $t, index_name => $name, index_info => $n)
75 10 ▶ if $n && !$have{join "\0", 'create', $t, $name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $n and not $have{join "\0", "create", $t, $name}++ 7 3 10
Condition100%3/3
$n and not $have{join "\0", "create", $t, $name}++
!ll&&!rl&&r
217
MC/DC100%2/2
$n and not $have{join "\0", "create", $t, $name}++
$n not $have{join "\0", "create", $t, $name}++
Truth table100%3/3
A and not(B)
A= $nB= not $have{join "\0", "create", $t, $name}++
ABresult
0X0
100
111
76 }
77 }
78 90 push @indexes, @extra_drop, @extra_create;
79 }
80  
81 # Foreign keys around a key or a column that changes. The server refuses to
82 # drop a primary key or unique key a foreign key references, to ALTER COLUMN
83 # to another type (a width change too) a column a foreign key stands on --
84 # child or referenced column -- and cannot create a foreign key onto columns
85 # no primary or unique key covers (all measured, karr k74). One rule: a
86 # foreign key that references a key the diff drops (primary key, unique
87 # constraint or unique index, wherever the drop comes from) or that stands on
88 # a column the diff retypes is dropped in phase 1 and, if the classes still
89 # declare it, added again in phase 7 -- as an operation of the foreign key
90 # diff, once, whatever else the diff does with it.
91 {
92 90 my %have = map { join("\0", $_->action, $_->table_name, $_->constraint_name) => 1 } @fks;
93 90 ▶ my $src = $self->source->{foreign_keys} // {};
Condition100%2/2
$self->source->{"foreign_keys"} // {}
l!l
891
MC/DC100%1/1
$self->source->{"foreign_keys"} // {}
$self->source->{"foreign_keys"}
Truth table100%2/2
A
A= $self->source->{"foreign_keys"}
Aresult
11
00
94 90 ▶ my $tgt = $target->{foreign_keys} // {};
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
900
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
95 90 ▶ my $tgt_idx = $target->{indexes} // {};
Condition50%1/2
$target->{"indexes"} // {}
l!l
900
MC/DC0%0/1
$target->{"indexes"} // {}
$target->{"indexes"}
Truth table50%1/2
A
A= $target->{"indexes"}
Aresult
11
00
96 90 ▶ my $tgt_cols = $target->{columns} // {};
Condition50%1/2
$target->{"columns"} // {}
l!l
900
MC/DC0%0/1
$target->{"columns"} // {}
$target->{"columns"}
Truth table50%1/2
A
A= $target->{"columns"}
Aresult
11
00
97 90 ▶ my $set = sub { join "\0", sort @{ $_[0] // [] } };
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$_[0] // []
l!l
640
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
98  
99 12 ▶ my $around = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
100 12 my ($table, $fk) = @_;
101 12 my $name = $fk->{constraint_name};
102 push @fks, DBIO::MSSQL::Diff::ForeignKey->new(action => 'drop',
103 table_name => $table, constraint_name => $name, fk_info => $fk)
104 12 ▶ unless $have{join "\0", 'drop', $table, $name}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $have{join "\0", "drop", $table, $name}++ 6 6 12
105 12 ▶ my ($new) = grep { $_->{constraint_name} eq $name } @{ $tgt->{$table} // [] };
Condition100%2/2
$tgt->{$table} // []
l!l
111
MC/DC100%1/1
$tgt->{$table} // []
$tgt->{$table}
Truth table100%2/2
A
A= $tgt->{$table}
Aresult
11
00
106 push @fks, DBIO::MSSQL::Diff::ForeignKey->new(action => 'add',
107 table_name => $table, constraint_name => $name, fk_info => $new)
108 12 ▶ if $new && !$have{join "\0", 'add', $table, $name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $new and not $have{join "\0", "add", $table, $name}++ 6 6 12
Condition100%3/3
$new and not $have{join "\0", "add", $table, $name}++
!ll&&!rl&&r
246
MC/DC100%2/2
$new and not $have{join "\0", "add", $table, $name}++
$new not $have{join "\0", "add", $table, $name}++
Truth table100%3/3
A and not(B)
A= $newB= not $have{join "\0", "add", $table, $name}++
ABresult
0X0
100
111
109 90 };
110  
111 # the keys the diff drops: [ table, [ columns ] ]
112 my @dropped = (
113 13 (map { [ $_->table_name, $_->columns ] } grep { $_->action eq 'drop' } @pks),
114 12 ▶ (map { [ $_->table_name, $_->index_info->{columns} // [] ] }
Condition50%1/2
$_->index_info->{"columns"} // []
l!l
120
MC/DC0%0/1
$_->index_info->{"columns"} // []
$_->index_info->{"columns"}
Truth table50%1/2
A
A= $_->index_info->{"columns"}
Aresult
11
00
115 90 ▶ grep { $_->action eq 'drop' && $_->index_info->{is_unique} } @indexes),
Condition100%3/3
$_->action eq 'drop' && $_->index_info->{'is_unique'}
!ll&&!rl&&r
291112
MC/DC100%2/2
$_->action eq 'drop' && $_->index_info->{'is_unique'}
$_->action eq 'drop' $_->index_info->{'is_unique'}
Truth table100%3/3
A && B
A= $_->action eq 'drop'B= $_->index_info->{'is_unique'}
ABresult
0X0
100
111
116 );
117 90 for my $key (@dropped) {
118 25 my ($table, $cols) = @$key;
119 25 my $want = $set->($cols);
120  
121 # a foreign key the classes declare onto columns that no key covers
122 # afterwards could not be created: refuse before anything is dropped
123 my $candidate =
124 18 ▶ (grep { $_->{is_unique} && $set->($_->{columns}) eq $want }
Condition100%3/3
$_->{'is_unique'} && &$set($_->{'columns'}) eq $want
!ll&&!rl&&r
459
MC/DC100%2/2
$_->{'is_unique'} && &$set($_->{'columns'}) eq $want
$_->{'is_unique'} &$set($_->{'columns'}) eq $want
Truth table100%3/3
A && B
A= $_->{'is_unique'}B= &$set($_->{'columns'}) eq $want
ABresult
0X0
100
111
125 25 ▶ values %{ $tgt_idx->{$table} // {} })
Condition100%2/2
$tgt_idx->{$table} // {}
l!l
1213
MC/DC100%1/1
$tgt_idx->{$table} // {}
$tgt_idx->{$table}
Truth table100%2/2
A
A= $tgt_idx->{$table}
Aresult
11
00
126 8 || ($set->([ map { $_->{column_name} } grep { $_->{is_pk} }
127 25 ▶ @{ $tgt_cols->{$table} // [] } ]) eq $want);
Condition100%5/5
grep({$_->{'is_unique'} and &$set($_->{'columns'}) eq $want;} values %{$tgt_idx->{$table} // {};}) || &$set([map({$_->{'column_name'};} grep({$_->{'is_pk'};} @{$tgt_cols->{$table} // [];}))]) eq $want
l!l&&r!l&&!r
9511
$tgt_cols->{$table} // []
l!l
151
MC/DC100%2/2
grep({$_->{'is_unique'} and &$set($_->{'columns'}) eq $want;} values %{$tgt_idx->{$table} // {};}) || &$set([map({$_->{'column_name'};} grep({$_->{'is_pk'};} @{$tgt_cols->{$table} // [];}))]) eq $want
grep({$_->{'is_unique'} and &$set($_->{'columns'}) eq $want;} values %{$tgt_idx->{$table} // {};}) &$set([map({$_->{'column_name'};} grep({$_->{'is_pk'};} @{$tgt_cols->{$table} // [];}))]) eq $want
MC/DC100%1/1
$tgt_cols->{$table} // []
$tgt_cols->{$table}
Truth table100%3/3
A || B
A= grep({$_->{'is_unique'} and &$set($_->{'columns'}) eq $want;} values %{$tgt_idx->{$table} // {};})B= &$set([map({$_->{'column_name'};} grep({$_->{'is_pk'};} @{$tgt_cols->{$table} // [];}))]) eq $want
ABresult
1X1
011
000
Truth table100%2/2
A
A= $tgt_cols->{$table}
Aresult
11
00
128 25 ▶ unless ($candidate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($candidate) 11 14 25
129 11 for my $t (sort keys %$tgt) {
130 3 for my $fk (@{ $tgt->{$t} }) {
131 3 ▶ next unless $fk->{to_table} eq $table && $set->($fk->{to_columns}) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $fk->{"to_table"} eq $table and &$set($fk->{"to_columns"}) eq $want 0 3 3
Condition33%1/3
$fk->{"to_table"} eq $table and &$set($fk->{"to_columns"}) eq $want
!ll&&!rl&&r
003
MC/DC0%0/2
$fk->{"to_table"} eq $table and &$set($fk->{"to_columns"}) eq $want
$fk->{"to_table"} eq $table &$set($fk->{"to_columns"}) eq $want
Truth table33%1/3
A and B
A= $fk->{"to_table"} eq $tableB= &$set($fk->{"to_columns"}) eq $want
ABresult
0X0
100
111
132 DBIO::Exception->throw(sprintf
133 "DBIO::MSSQL: the key of table '%s' (%s) cannot be dropped: "
134 . "the classes declare no primary key or unique key over these columns, "
135 . "but declare the foreign key '%s' of table '%s' that references them and "
136 . "would have nothing to reference (no primary or candidate key). Declare "
137 . "the primary key, or a unique constraint over the same columns, or drop "
138 . "the foreign key",
139 3 $table, join(', ', @$cols), $fk->{constraint_name}, $t);
140 }
141 }
142 }
143  
144 22 for my $t (sort keys %$src) {
145 6 for my $fk (@{ $src->{$t} }) {
146 6 ▶ $around->($t, $fk) if $fk->{to_table} eq $table && $set->($fk->{to_columns}) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->{"to_table"} eq $table and &$set($fk->{"to_columns"}) eq $want 6 0 6
Condition33%1/3
$fk->{"to_table"} eq $table and &$set($fk->{"to_columns"}) eq $want
!ll&&!rl&&r
006
MC/DC0%0/2
$fk->{"to_table"} eq $table and &$set($fk->{"to_columns"}) eq $want
$fk->{"to_table"} eq $table &$set($fk->{"to_columns"}) eq $want
Truth table33%1/3
A and B
A= $fk->{"to_table"} eq $tableB= &$set($fk->{"to_columns"}) eq $want
ABresult
0X0
100
111
147 }
148 }
149 }
150  
151 87 for my $op (grep { $_->retypes } @columns) {
152 10 my ($table, $col) = ($op->table_name, $op->column_name);
153 10 for my $t (sort keys %$src) {
154 6 for my $fk (@{ $src->{$t} }) {
155 $around->($t, $fk)
156 4 ▶ if ($t eq $table && grep { $_ eq $col } @{ $fk->{from_columns} // [] })
Condition40%2/5
$fk->{"from_columns"} // []
l!l
40
$fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
!ll&&!rl&&r
002
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/2
$fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
$fk->{"to_table"} eq $table grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
Truth table33%1/3
A and B
A= $fk->{"to_table"} eq $tableB= grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
ABresult
0X0
100
111
157 6 ▶ || ($fk->{to_table} eq $table && grep { $_ eq $col } @{ $fk->{to_columns} // [] });
Branch: 1 branch
BranchTrueFalseTotal
if $t eq $table and grep {$_ eq $col;} @{$fk->{"from_columns"} // [];} or $fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];} 6 0 6
Condition62%5/8
$t eq $table and grep {$_ eq $col;} @{$fk->{"from_columns"} // [];} or $fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
l!l&&r!l&&!r
420
$t eq $table and grep {$_ eq $col;} @{$fk->{"from_columns"} // [];}
!ll&&!rl&&r
204
$fk->{"to_columns"} // []
l!l
20
MC/DC0%0/3
$t eq $table and grep {$_ eq $col;} @{$fk->{"from_columns"} // [];} or $fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
$t eq $table grep {$_ eq $col;} @{$fk->{"from_columns"} // [];} $fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table40%2/5
A and B or C
A= $t eq $tableB= grep {$_ eq $col;} @{$fk->{"from_columns"} // [];}C= $fk->{"to_table"} eq $table and grep {$_ eq $col;} @{$fk->{"to_columns"} // [];}
ABCresult
11X1
0X11
1011
0X00
1000
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
158 }
159 }
160 }
161 }
162  
163 # The keys the target will hold are checked as install_ddl checks the ones
164 # of the classes: the server refuses cascading cycles and multiple cascade
165 # paths (Msg 1785) when a key is added. Only when one is added, and on the
166 # target model, which has every table; the source is what the server holds.
167 87 ▶ if (grep { $_->action eq 'add' } @fks) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->action eq "add";} @fks) 19 68 87
168 19 ▶ my $by_table = $target->{foreign_keys} // {};
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
190
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
169 _check_cascade_paths([
170 map {
171 19 my $table = $_;
172 map {
173 +{
174 table => $table,
175 label => "constraint '$_->{constraint_name}'",
176 from_cols => $_->{from_columns},
177 to_table => $_->{to_table},
178 to_cols => $_->{to_columns},
179 on_delete => $_->{on_delete} || 'NO ACTION',
180 22 ▶ on_update => $_->{on_update} || 'NO ACTION',
Condition100%4/4
$_->{'on_delete'} || 'NO ACTION'
l!l
184
$_->{'on_update'} || 'NO ACTION'
l!l
184
MC/DC100%1/1
$_->{'on_delete'} || 'NO ACTION'
$_->{'on_delete'}
MC/DC100%1/1
$_->{'on_update'} || 'NO ACTION'
$_->{'on_update'}
Truth table100%2/2
A
A= $_->{'on_delete'}
Aresult
11
00
Truth table100%2/2
A
A= $_->{'on_update'}
Aresult
11
00
181 }
182 21 } @{ $by_table->{$table} };
183 } sort keys %$by_table
184 ]);
185 }
186  
187 # Constraint and object names share one namespace (Msg 2714): the target
188 # model is judged as install_ddl judges the classes.
189 {
190 86 ▶ my %objects = map { $_ => 1 } keys %{ $target->{tables} // {} };
Condition50%1/2
$target->{"tables"} // {}
l!l
860
MC/DC0%0/1
$target->{"tables"} // {}
$target->{"tables"}
Truth table50%1/2
A
A= $target->{"tables"}
Aresult
11
00
191 86 my @constraints;
192 86 ▶ my $fks = $target->{foreign_keys} // {};
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
860
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
193 86 for my $table (sort keys %$fks) {
194 push @constraints, map { +{
195 table => $table, name => $_->{constraint_name} // '',
196 label => 'foreign key', columns => $_->{from_columns},
197 30 ▶ } } @{ $fks->{$table} };
Condition50%1/2
$_->{"constraint_name"} // ""
l!l
290
MC/DC0%0/1
$_->{"constraint_name"} // ""
$_->{"constraint_name"}
Truth table50%1/2
A
A= $_->{"constraint_name"}
Aresult
11
00
198 }
199 86 ▶ my $idx = $target->{indexes} // {};
Condition50%1/2
$target->{"indexes"} // {}
l!l
860
MC/DC0%0/1
$target->{"indexes"} // {}
$target->{"indexes"}
Truth table50%1/2
A
A= $target->{"indexes"}
Aresult
11
00
200 86 for my $table (sort keys %$idx) {
201 36 for my $name (sort keys %{ $idx->{$table} }) {
202 45 my $i = $idx->{$table}{$name};
203 45 ▶ next unless $i->{is_unique_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $i->{"is_unique_constraint"} 30 15 45
204 push @constraints, { table => $table, name => $i->{index_name} // $name,
205 15 ▶ label => 'unique constraint', columns => $i->{columns} };
Condition66%2/3
$i->{"index_name"} // $name
l!l&&r!l&&!r
1140
MC/DC0%0/2
$i->{"index_name"} // $name
$i->{"index_name"} $name
Truth table66%2/3
A // B
A= $i->{"index_name"}B= $name
ABresult
1X1
011
000
206 }
207 }
208 # the primary keys the diff writes: those of new tables and the added ones
209 86 my %new_table = map { $_->table_name => 1 } grep { $_->action eq 'create' } @tables;
210 86 my %add_pk = map { $_->table_name => 1 } grep { $_->action eq 'create' } @pks;
211 86 ▶ my $cols = $target->{columns} // {};
Condition50%1/2
$target->{"columns"} // {}
l!l
860
MC/DC0%0/1
$target->{"columns"} // {}
$target->{"columns"}
Truth table50%1/2
A
A= $target->{"columns"}
Aresult
11
00
212 86 for my $table (sort keys %new_table, sort keys %add_pk) {
213 39 ▶ my @pk = grep { $_->{is_pk} } @{ $cols->{$table} // [] };
Condition100%2/2
$cols->{$table} // []
l!l
309
MC/DC100%1/1
$cols->{$table} // []
$cols->{$table}
Truth table100%2/2
A
A= $cols->{$table}
Aresult
11
00
214 push @constraints, { table => $table, name => _constraint_name('PK', $table),
215 39 ▶ label => 'primary key', columns => [ map { $_->{column_name} } @pk ] }
Branch: 1 branch
BranchTrueFalseTotal
if @pk 23 16 39
216 if @pk;
217 }
218 86 _check_constraint_names(\%objects, \@constraints);
219 }
220  
221 83 ▶ my $drop = sub { $_->action eq 'drop' };
Subroutine __ANON__: called CC=1 SCAR=0.0
222 return [
223 @schemas,
224 37 (grep { $drop->() } @fks),
225 14 (grep { $drop->() } @views),
226 50 (grep { $drop->() } @indexes),
227 21 (grep { $drop->() } @pks),
228 35 (grep { $drop->() } @tables),
229 @columns,
230 35 (grep { !$drop->() } @tables),
231 21 (grep { !$drop->() } @pks),
232 50 (grep { !$drop->() } @indexes),
233 37 (grep { !$drop->() } @fks),
234 83 (grep { !$drop->() } @views),
235 ];
236 }
237  
238  
239 94 ▶sub _fold_names {
Subroutine _fold_names: called CC=17 SCAR=28.8
240 94 my ($self, $target) = @_;
241 94 my $source = $self->source;
242 94 my $coll = $source->{collation};
243 94 ▶ return $target unless $coll && !$coll->{case_sensitive};
Branch: 1 branch
BranchTrueFalseTotal
unless $coll and not $coll->{"case_sensitive"} 89 5 94
Condition100%3/3
$coll and not $coll->{"case_sensitive"}
!ll&&!rl&&r
8815
MC/DC100%2/2
$coll and not $coll->{"case_sensitive"}
$coll not $coll->{"case_sensitive"}
Truth table100%3/3
A and not(B)
A= $collB= not $coll->{"case_sensitive"}
ABresult
0X0
100
111
244  
245 # a function from a name to the live spelling of the same name (or itself)
246 41 ▶ my $map = sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
247 41 my %by = map { lc($_) => $_ } @_;
248 41 ▶ return sub { defined $_[0] ? ($by{ lc $_[0] } // $_[0]) : undef };
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 127 0 127
Condition66%2/3
$by{lc $_[0]} // $_[0]
l!l&&r!l&&!r
12070
MC/DC0%0/2
$by{lc $_[0]} // $_[0]
$by{lc $_[0]} $_[0]
Truth table66%2/3
A // B
A= $by{lc $_[0]}B= $_[0]
ABresult
1X1
011
000
249 5 };
250 5 ▶ my $table_of = $map->(keys %{ $source->{tables} // {} }, keys %{ $source->{columns} // {} });
Condition50%2/4
$source->{"tables"} // {}
l!l
50
$source->{"columns"} // {}
l!l
50
MC/DC0%0/1
$source->{"tables"} // {}
$source->{"tables"}
MC/DC0%0/1
$source->{"columns"} // {}
$source->{"columns"}
Truth table50%1/2
A
A= $source->{"tables"}
Aresult
11
00
Truth table50%1/2
A
A= $source->{"columns"}
Aresult
11
00
251  
252 # the live column spelling of the table the target calls $tname
253 26 ▶ my $col_of = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
254 26 my ($tname) = @_;
255 26 my $live = $table_of->($tname);
256 26 ▶ return $map->(map { $_->{column_name} } @{ $source->{columns}{$live} // [] });
Condition100%2/2
$source->{"columns"}{$live} // []
l!l
251
MC/DC100%1/1
$source->{"columns"}{$live} // []
$source->{"columns"}{$live}
Truth table100%2/2
A
A= $source->{"columns"}{$live}
Aresult
11
00
257 5 };
258 5 ▶ my $cols = sub { my ($f, $list) = @_; [ map { $f->($_) } @{ $list // [] } ] };
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition50%1/2
$list // []
l!l
150
MC/DC0%0/1
$list // []
$list
Truth table50%1/2
A
A= $list
Aresult
11
00
259  
260 5 my (%tables, %columns, %indexes, %fks);
261 5 ▶ for my $t (keys %{ $target->{tables} // {} }) {
Condition50%1/2
$target->{"tables"} // {}
l!l
50
MC/DC0%0/1
$target->{"tables"} // {}
$target->{"tables"}
Truth table50%1/2
A
A= $target->{"tables"}
Aresult
11
00
262 11 $tables{ $table_of->($t) } = { %{ $target->{tables}{$t} }, table_name => $table_of->($t) };
263 }
264 5 ▶ for my $t (keys %{ $target->{columns} // {} }) {
Condition50%1/2
$target->{"columns"} // {}
l!l
50
MC/DC0%0/1
$target->{"columns"} // {}
$target->{"columns"}
Truth table50%1/2
A
A= $target->{"columns"}
Aresult
11
00
265 11 my $f = $col_of->($t);
266 22 $columns{ $table_of->($t) } = [ map { +{ %$_, column_name => $f->($_->{column_name}) } }
267 11 @{ $target->{columns}{$t} } ];
268 }
269 5 ▶ for my $t (keys %{ $target->{indexes} // {} }) {
Condition50%1/2
$target->{"indexes"} // {}
l!l
50
MC/DC0%0/1
$target->{"indexes"} // {}
$target->{"indexes"}
Truth table50%1/2
A
A= $target->{"indexes"}
Aresult
11
00
270 5 my $live = $table_of->($t);
271 5 my $f = $col_of->($t);
272 5 ▶ my $iname = $map->(keys %{ $source->{indexes}{$live} // {} });
Condition50%1/2
$source->{"indexes"}{$live} // {}
l!l
50
MC/DC0%0/1
$source->{"indexes"}{$live} // {}
$source->{"indexes"}{$live}
Truth table50%1/2
A
A= $source->{"indexes"}{$live}
Aresult
11
00
273 5 for my $n (keys %{ $target->{indexes}{$t} }) {
274 5 my $i = $target->{indexes}{$t}{$n};
275 $indexes{$live}{ $iname->($n) } = { %$i,
276 (exists $i->{index_name} ? (index_name => $iname->($i->{index_name})) : ()),
277 5 ▶ columns => $cols->($f, $i->{columns}) };
Branch: 1 branch
BranchTrueFalseTotal
exists $i->{'index_name'} ? : 5 0 5
278 }
279 }
280 5 ▶ for my $t (keys %{ $target->{foreign_keys} // {} }) {
Condition50%1/2
$target->{"foreign_keys"} // {}
l!l
50
MC/DC0%0/1
$target->{"foreign_keys"} // {}
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
Aresult
11
00
281 5 my $live = $table_of->($t);
282 5 my $f = $col_of->($t);
283 5 ▶ my $cname = $map->(map { $_->{constraint_name} } @{ $source->{foreign_keys}{$live} // [] });
Condition50%1/2
$source->{"foreign_keys"}{$live} // []
l!l
50
MC/DC0%0/1
$source->{"foreign_keys"}{$live} // []
$source->{"foreign_keys"}{$live}
Truth table50%1/2
A
A= $source->{"foreign_keys"}{$live}
Aresult
11
00
284 5 push @{ $fks{$live} }, map {
285 +{ %$_,
286 constraint_name => $cname->($_->{constraint_name}),
287 from_columns => $cols->($f, $_->{from_columns}),
288 to_table => $table_of->($_->{to_table}),
289 5 to_columns => $cols->($col_of->($_->{to_table}), $_->{to_columns}) }
290 5 } @{ $target->{foreign_keys}{$t} };
291 }
292  
293 5 return { %$target,
294 tables => \%tables, columns => \%columns, indexes => \%indexes, foreign_keys => \%fks };
295 }
296  
297  
298 36 1;
299  
300 __END__
« MSSQL/Introspect.pm ↑ Summary MSSQL/Introspect/Columns.pm »