blib/lib/DBIO/MySQL/Diff/PrimaryKey.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 16 / 16 Conditioncond 71.4% 10 / 14 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 93.1% 108 / 116 CC 17 SCAR 18.0
CC
17
Cov
96%
diff
11.1
as_sql
4.0
primary_key_columns
3.0
CRAP 6.1
« blib/lib/DBIO/MySQL/DefaultValue.pm ↑ Summary blib/lib/DBIO/MySQL/Diff/ForeignKey.pm »
1 package DBIO::MySQL::Diff::PrimaryKey;
2 # ABSTRACT: Diff operations for MySQL/MariaDB primary keys
3  
4 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 57 our $VERSION = '0.900007';
8  
9 57 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 57 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 57 ▶use DBIO::MySQL::Identifier qw(quote_ident quote_table_in);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 57 ▶use DBIO::MySQL::Diff::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 57 our @EXPORT_OK = ('primary_key_columns');
16  
17 57 __PACKAGE__->mk_diff_accessors(qw(
18 table_name columns old_columns with_columns database
19 ));
20  
21  
22  
23 429 ▶sub primary_key_columns {
Subroutine primary_key_columns: called CC=3 SCAR=11.0
Pod documented
24 429 my ($columns) = @_;
25 429 ▶ my @pk = grep { $_->{is_pk} } @{ $columns // [] };
Condition100%2/2
$columns // []
l!l
40326
MC/DC100%1/1
$columns // []
$columns
Truth table100%2/2
A
A= $columns
Aresult
11
00
26 # pk_position only when every column of the key has one; sort is stable
27 429 ▶ if (!grep { !defined $_->{pk_position} } @pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not defined $_->{"pk_position"};} @pk) 251 178 429
28 251 @pk = sort { $a->{pk_position} <=> $b->{pk_position} } @pk;
29 }
30 429 return map { $_->{column_name} } @pk;
31 }
32  
33  
34 169 ▶sub diff {
Subroutine diff: called CC=11 SCAR=24.1
Pod documented
35 169 my ($class, $source, $target, $source_tables, $target_tables) = @_;
36 169 ▶ $source //= {}; $target //= {};
Condition50%2/4
$source //= {}
l!l
1690
$target //= {}
l!l
1690
MC/DC0%0/1
$source //= {}
$source
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $source
Aresult
11
00
Truth table50%1/2
A
A= $target
Aresult
11
00
37 169 ▶ $source_tables //= {}; $target_tables //= {};
Condition50%2/4
$source_tables //= {}
l!l
1690
$target_tables //= {}
l!l
1690
MC/DC0%0/1
$source_tables //= {}
$source_tables
MC/DC0%0/1
$target_tables //= {}
$target_tables
Truth table50%1/2
A
A= $source_tables
Aresult
11
00
Truth table50%1/2
A
A= $target_tables
Aresult
11
00
38  
39 169 my @ops;
40 169 for my $table (sort keys %$target_tables) {
41 207 ▶ next unless exists $source_tables->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$table} 37 170 207
42 170 my @new = primary_key_columns($target->{$table});
43 170 my @was = primary_key_columns($source->{$table});
44 # the key of a column that goes is shortened by the server, and gone with
45 # its last column: what is left of it is the key this statement meets
46 170 ▶ my %stay = map { $_->{column_name} => 1 } @{ $target->{$table} // [] };
Condition100%2/2
$target->{$table} // []
l!l
15713
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
Aresult
11
00
47 170 my @old = grep { $stay{$_} } @was;
48 170 ▶ next if join("\0", @old) eq join("\0", @new);
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", @old) eq join("\0", @new) 142 28 170
49 28 ▶ if (@new) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new) { } 21 7 28
50 21 ▶ push @ops, $class->new(
Branch: 1 branch
BranchTrueFalseTotal
@old ? : 15 6 21
51 action => (@old ? 'replace' : 'add'), table_name => $table,
52 columns => \@new, old_columns => \@was,
53 );
54 }
55 else {
56 7 push @ops, $class->new(
57 action => 'drop', table_name => $table, columns => [], old_columns => \@was,
58 );
59 }
60 }
61 169 return @ops;
62 }
63  
64  
65 24 ▶sub as_sql {
Subroutine as_sql: called CC=4 SCAR=13.9
Pod documented
66 24 my ($self) = @_;
67 24 my $tbl = quote_table_in($self->database, $self->table_name);
68 24 ▶ return "ALTER TABLE $tbl DROP PRIMARY KEY;" if $self->action eq 'drop';
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 5 19 24
69 return sprintf 'ALTER TABLE %s %s%sADD PRIMARY KEY (%s);',
70 $tbl,
71 2 ▶ join('', map { $_->add_clause(1) . ', ' } @{ $self->with_columns // [] }),
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'replace' ? : 14 5 19
Condition100%2/2
$self->with_columns // []
l!l
217
MC/DC100%1/1
$self->with_columns // []
$self->with_columns
Truth table100%2/2
A
A= $self->with_columns
Aresult
11
00
72 ($self->action eq 'replace' ? 'DROP PRIMARY KEY, ' : ''),
73 19 join(', ', map { quote_ident($_) } @{ $self->columns });
74 }
75  
76  
77 2 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
78 2 my ($self) = @_;
79 2 ▶ return sprintf ' -pk: DROP PRIMARY KEY on %s (was %s)', $self->table_name, join(', ', @{ $self->old_columns })
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 1 1 2
80 if $self->action eq 'drop';
81 1 return sprintf ' ~primary key: %s on %s', join(', ', @{ $self->columns }), $self->table_name;
82 }
83  
84 57 1;
85  
86 __END__
« blib/lib/DBIO/MySQL/DefaultValue.pm ↑ Summary blib/lib/DBIO/MySQL/Diff/ForeignKey.pm »