| 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 // []
MC/DC100%1/1
$columns // []
$columns
Truth table100%2/2
A
A= $columns
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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 //= {}
$target //= {}
MC/DC0%0/1
$source //= {}
$source
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $source
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||
| 37 | 169 | ▶ | $source_tables //= {}; $target_tables //= {}; | ||||||||||||||||||||
|
Condition50%2/4
$source_tables //= {}
$target_tables //= {}
MC/DC0%0/1
$source_tables //= {}
$source_tables
MC/DC0%0/1
$target_tables //= {}
$target_tables
Truth table50%1/2
A
A= $source_tables
Truth table50%1/2
A
A= $target_tables
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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} // []
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
| |||||||||||||||||||||||
| 47 | 170 | my @old = grep { $stay{$_} } @was; | |||||||||||||||||||||
| 48 | 170 | ▶ | next if join("\0", @old) eq join("\0", @new); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 49 | 28 | ▶ | if (@new) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 21 | ▶ | push @ops, $class->new( | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
Condition100%2/2
$self->with_columns // []
MC/DC100%1/1
$self->with_columns // []
$self->with_columns
Truth table100%2/2
A
A= $self->with_columns
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||