| 1 | package DBIO::Sybase::Diff::Index; | ||||||||||||
| 2 | # ABSTRACT: Diff Sybase ASE indexes | ||||||||||||
| 3 | |||||||||||||
| 4 | 32 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 32 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 32 | our $VERSION = '0.900007'; | |||||||||||
| 8 | |||||||||||||
| 9 | 32 | ▶ | use DBIO::Diff::Compare qw(changed_index_fields); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 32 | ▶ | use DBIO::Sybase::Quote (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 32 | my $q = \&DBIO::Sybase::Quote::quote_ident; | |||||||||||
| 13 | |||||||||||||
| 14 | |||||||||||||
| 15 | 56 | ▶ | sub diff { | ||||||||||
|
Subroutine
diff: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||
| 16 | 56 | my ($class, $source, $target) = @_; | |||||||||||
| 17 | 56 | ▶ | $source //= {}; | ||||||||||
|
Condition100%2/2
$source //= {}
MC/DC100%1/1
$source //= {}
$source
Truth table100%2/2
A
A= $source
| |||||||||||||
| 18 | 56 | ▶ | $target //= {}; | ||||||||||
|
Condition100%2/2
$target //= {}
MC/DC100%1/1
$target //= {}
$target
Truth table100%2/2
A
A= $target
| |||||||||||||
| 19 | 56 | my @ops; | |||||||||||
| 20 | |||||||||||||
| 21 | 56 | for my $table (sort keys %$target) { | |||||||||||
| 22 | 20 | my $s_idx = _without_pk($source->{$table}); | |||||||||||
| 23 | 20 | my $t_idx = _without_pk($target->{$table}); | |||||||||||
| 24 | |||||||||||||
| 25 | 20 | for my $name (sort keys %$t_idx) { | |||||||||||
| 26 | 12 | ▶ | if (exists $s_idx->{$name}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | push @ops, DBIO::Sybase::Diff::Index::Alter->new( | ||||||||||||
| 28 | action => 'alter', | ||||||||||||
| 29 | table => $table, | ||||||||||||
| 30 | from => $s_idx->{$name}, | ||||||||||||
| 31 | to => $t_idx->{$name}, | ||||||||||||
| 32 | 7 | ▶ | ) if changed_index_fields($s_idx->{$name}, $t_idx->{$name}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | } | ||||||||||||
| 34 | else { | ||||||||||||
| 35 | push @ops, DBIO::Sybase::Diff::Index::Create->new( | ||||||||||||
| 36 | action => 'create', | ||||||||||||
| 37 | table => $table, | ||||||||||||
| 38 | 5 | idx => $t_idx->{$name}, | |||||||||||
| 39 | ); | ||||||||||||
| 40 | } | ||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 20 | for my $name (sort keys %$s_idx) { | |||||||||||
| 44 | push @ops, DBIO::Sybase::Diff::Index::Drop->new( | ||||||||||||
| 45 | action => 'drop', | ||||||||||||
| 46 | table => $table, | ||||||||||||
| 47 | idx => $s_idx->{$name}, | ||||||||||||
| 48 | 11 | ▶ | ) unless exists $t_idx->{$name}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | } | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 56 | return @ops; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 40 | ▶ | sub _without_pk { | ||||||||||
|
Subroutine
_without_pk: called CC=3 SCAR=11.0
| |||||||||||||
| 56 | 40 | my ($set) = @_; | |||||||||||
| 57 | 40 | ▶ | $set //= {}; | ||||||||||
|
Condition100%2/2
$set //= {}
MC/DC100%1/1
$set //= {}
$set
Truth table100%2/2
A
A= $set
| |||||||||||||
| 58 | 23 | return { map { $_ => $set->{$_} } | |||||||||||
| 59 | 40 | ▶ | grep { ($set->{$_}{origin} // '') ne 'pk' } keys %$set }; | ||||||||||
|
Condition100%2/2
$set->{$_}{"origin"} // ""
MC/DC100%1/1
$set->{$_}{"origin"} // ""
$set->{$_}{"origin"}
Truth table100%2/2
A
A= $set->{$_}{"origin"}
| |||||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | package DBIO::Sybase::Diff::Index::Create; | ||||||||||||
| 63 | 32 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | |||||||||||||
| 65 | 32 | __PACKAGE__->mk_diff_accessors(qw(table idx)); | |||||||||||
| 66 | |||||||||||||
| 67 | 5 | ▶ | sub as_sql { | ||||||||||
|
Subroutine
as_sql: called CC=2 SCAR=6.9
| |||||||||||||
| 68 | 5 | my $self = shift; | |||||||||||
| 69 | 5 | my $cols = join(', ', map { $q->($_) } @{$self->idx->{columns}}); | |||||||||||
| 70 | 5 | ▶ | my $unique = $self->idx->{is_unique} ? 'UNIQUE ' : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | sprintf 'CREATE %sINDEX %s ON %s (%s);', | ||||||||||||
| 72 | 5 | $unique, $q->($self->idx->{index_name}), $q->($self->table), $cols; | |||||||||||
| 73 | } | ||||||||||||
| 74 | 1 | ▶ | sub summary { | ||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
| |||||||||||||
| 75 | 1 | my $self = shift; | |||||||||||
| 76 | 1 | "CREATE INDEX $self->{idx}{index_name} ON $self->{table}"; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | package DBIO::Sybase::Diff::Index::Alter; | ||||||||||||
| 80 | 32 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 81 | |||||||||||||
| 82 | 32 | __PACKAGE__->mk_diff_accessors(qw(table from to)); | |||||||||||
| 83 | |||||||||||||
| 84 | 6 | ▶ | sub as_sql { | ||||||||||
|
Subroutine
as_sql: called CC=2 SCAR=7.0
| |||||||||||||
| 85 | 6 | my $self = shift; | |||||||||||
| 86 | 6 | my $tbl = $q->($self->table); | |||||||||||
| 87 | 6 | my $cols = join(', ', map { $q->($_) } @{$self->to->{columns}}); | |||||||||||
| 88 | 6 | ▶ | my $unique = $self->to->{is_unique} ? 'UNIQUE ' : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | # DROP and CREATE since Sybase ASE does not support ALTER INDEX | ||||||||||||
| 90 | sprintf 'DROP INDEX %s.%s; CREATE %sINDEX %s ON %s (%s);', | ||||||||||||
| 91 | 6 | $tbl, $q->($self->from->{index_name}), $unique, $q->($self->to->{index_name}), $tbl, $cols; | |||||||||||
| 92 | } | ||||||||||||
| 93 | 0 | ▶ | sub summary { | ||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
| |||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||
| 95 | 0 | "ALTER INDEX $self->{to}{index_name} ON $self->{table}"; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | package DBIO::Sybase::Diff::Index::Drop; | ||||||||||||
| 99 | 32 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 100 | |||||||||||||
| 101 | 32 | __PACKAGE__->mk_diff_accessors(qw(table idx)); | |||||||||||
| 102 | |||||||||||||
| 103 | 5 | ▶ | sub as_sql { | ||||||||||
|
Subroutine
as_sql: called CC=1 SCAR=0.0
| |||||||||||||
| 104 | 5 | my $self = shift; | |||||||||||
| 105 | 5 | sprintf 'DROP INDEX %s.%s;', $q->($self->table), $q->($self->idx->{index_name}); | |||||||||||
| 106 | } | ||||||||||||
| 107 | 0 | ▶ | sub summary { | ||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
| |||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||
| 109 | 0 | "DROP INDEX $self->{table}.$self->{idx}{index_name}"; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 32 | 1; | |||||||||||
| 113 | |||||||||||||
| 114 | __END__ | ||||||||||||