| 1 | package DBIO::MSSQL::Diff::Index; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL indexes | ||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 38 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 38 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | 38 | ▶ | use DBIO::MSSQL::DDL qw(_quote_ident _quote_table); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 38 | ▶ | use DBIO::Diff::Compare qw(changed_fields); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 38 | ▶ | use DBIO::MSSQL::Diff::Table (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 38 | __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/); | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 101 | ▶ | sub diff { | ||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 20 | 101 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | # Tables present in the source model but absent from the target are being | ||||||||||||||||||||||||||||||
| 23 | # dropped in this same pass. MSSQL's DROP TABLE also removes the table's own | ||||||||||||||||||||||||||||||
| 24 | # indexes, so a later standalone DROP INDEX ... ON <table> would fail (the | ||||||||||||||||||||||||||||||
| 25 | # table no longer exists). Suppress those drops (karr k15). Absent table | ||||||||||||||||||||||||||||||
| 26 | # sections yield an empty set, preserving the original two-argument | ||||||||||||||||||||||||||||||
| 27 | # behaviour used by direct unit-test calls. | ||||||||||||||||||||||||||||||
| 28 | 101 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||||||||||||||||||||
| 29 | 101 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | # indexes of a view are not diffed: a view is Diff::View's (karr k82) | ||||||||||||||||||||||||||||||
| 32 | 101 | my %is_view = map { $_ => 1 } | |||||||||||||||||||||||||||||
| 33 | DBIO::MSSQL::Diff::Table->view_names($source_tables, $target_tables); | ||||||||||||||||||||||||||||||
| 34 | 101 | ▶ | if (%is_view) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $source = { map { $_ => $source->{$_} } grep { !$is_view{$_} } keys %{ $source // {} } }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$source // {}
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | $target = { map { $_ => $target->{$_} } grep { !$is_view{$_} } keys %{ $target // {} } }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$target // {}
MC/DC0%0/1
$target // {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||
| 38 | 20 | my %dropped_table = map { $_ => 1 } | |||||||||||||||||||||||||||||
| 39 | 101 | grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 27 | ▶ | my $create = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 42 | 27 | my ($table, $name, $info) = @_; | |||||||||||||||||||||||||||||
| 43 | 27 | $class->new(action => 'create', table_name => $table, | |||||||||||||||||||||||||||||
| 44 | index_name => $name, index_info => $info); | ||||||||||||||||||||||||||||||
| 45 | 101 | }; | |||||||||||||||||||||||||||||
| 46 | 22 | ▶ | my $drop = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 47 | 22 | my ($table, $name, $info) = @_; | |||||||||||||||||||||||||||||
| 48 | 22 | $class->new(action => 'drop', table_name => $table, | |||||||||||||||||||||||||||||
| 49 | index_name => $name, index_info => $info); | ||||||||||||||||||||||||||||||
| 50 | 101 | }; | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | # scope 'all': indexes are diffed for every target table (including | ||||||||||||||||||||||||||||||
| 53 | # brand-new ones, whose standalone indexes are not created inline by the | ||||||||||||||||||||||||||||||
| 54 | # table op) plus a trailing drop pass for source-only tables. | ||||||||||||||||||||||||||||||
| 55 | return $class->diff_nested($source, $target, | ||||||||||||||||||||||||||||||
| 56 | scope => 'all', | ||||||||||||||||||||||||||||||
| 57 | 35 | ▶ | changed_when => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 58 | scalar changed_fields($_[0], $_[1], | ||||||||||||||||||||||||||||||
| 59 | bool => ['is_unique', 'is_unique_constraint'], | ||||||||||||||||||||||||||||||
| 60 | dim => ['columns'], | ||||||||||||||||||||||||||||||
| 61 | scalar => ['kind']) | ||||||||||||||||||||||||||||||
| 62 | or $class->normalize_filter($_[0]{filter}) | ||||||||||||||||||||||||||||||
| 63 | 35 | ▶ | ne $class->normalize_filter($_[1]{filter}); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||||
| 65 | 16 | ▶ | on_new => sub { $create->(@_) }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 67 | 11 | my ($table, $name, $old, $new) = @_; | |||||||||||||||||||||||||||||
| 68 | 11 | ($drop->($table, $name, $old), $create->($table, $name, $new)); | |||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||||
| 70 | 15 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 71 | 15 | my ($table, $name, $info) = @_; | |||||||||||||||||||||||||||||
| 72 | # Skip when the owning table is itself being dropped this pass; the | ||||||||||||||||||||||||||||||
| 73 | # DROP TABLE already removes this index (karr k15). | ||||||||||||||||||||||||||||||
| 74 | 15 | ▶ | return () if $dropped_table{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 75 | 11 | return $drop->($table, $name, $info); | |||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||
| 77 | 101 | ); | |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 75 | ▶ | sub normalize_filter { | ||||||||||||||||||||||||||||
|
Subroutine
normalize_filter: called CC=15 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||
| 82 | 75 | my ($class, $f) = @_; | |||||||||||||||||||||||||||||
| 83 | 75 | ▶ | return '' unless defined $f && $f =~ /\S/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $f and $f =~ /\S/
MC/DC50%1/2
defined $f and $f =~ /\S/
defined $f
$f =~ /\S/
Truth table66%2/3
A and B
A= defined $fB= $f =~ /\S/
| |||||||||||||||||||||||||||||||
| 84 | # literal and non-literal parts alternate; '' inside a literal is an escape | ||||||||||||||||||||||||||||||
| 85 | 26 | my @parts = split /('(?:[^']|'')*')/, $f, -1; | |||||||||||||||||||||||||||||
| 86 | 26 | my $out = ''; | |||||||||||||||||||||||||||||
| 87 | 26 | for my $i (0 .. $#parts) { | |||||||||||||||||||||||||||||
| 88 | 38 | ▶ | if ($i % 2) { $out .= $parts[$i]; next } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 89 | 32 | my $p = lc $parts[$i]; | |||||||||||||||||||||||||||||
| 90 | 32 | $p =~ s/\[(\w+)\]/$1/g; | |||||||||||||||||||||||||||||
| 91 | 32 | $p =~ s/\s+/ /g; | |||||||||||||||||||||||||||||
| 92 | 32 | $p =~ s/ ?([=<>!(,]) ?/$1/g; | |||||||||||||||||||||||||||||
| 93 | 32 | $p =~ s/ \)/)/g; | |||||||||||||||||||||||||||||
| 94 | # parentheses around a number: the server writes (1) where a declaration has 1 | ||||||||||||||||||||||||||||||
| 95 | 32 | ▶ | 1 while $p =~ s/(?<=[=<>(,])\((-?[\d.]+)\)/$1/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 96 | 32 | $out .= $p; | |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||
| 98 | 26 | $out =~ s/^ +| +$//g; | |||||||||||||||||||||||||||||
| 99 | # parentheses around the whole predicate | ||||||||||||||||||||||||||||||
| 100 | 26 | ▶ | while ($out =~ /^\((.*)\)$/s) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 101 | 9 | my ($inner, $depth, $wraps) = ($1, 0, 1); | |||||||||||||||||||||||||||||
| 102 | 9 | my @in = split /('(?:[^']|'')*')/, $inner, -1; | |||||||||||||||||||||||||||||
| 103 | 9 | OUTER: for my $i (0 .. $#in) { | |||||||||||||||||||||||||||||
| 104 | 13 | ▶ | next if $i % 2; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | 11 | for my $c (split //, $in[$i]) { | |||||||||||||||||||||||||||||
| 106 | 58 | ▶ | $depth++ if $c eq '('; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ($c eq ')' && --$depth < 0) { $wraps = 0; last OUTER } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c eq ")" and --$depth < 0
MC/DC0%0/2
$c eq ")" and --$depth < 0
$c eq ")"
--$depth < 0
Truth table66%2/3
A and B
A= $c eq ")"B= --$depth < 0
| |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | last unless $wraps && $depth == 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$wraps and $depth == 0
MC/DC0%0/2
$wraps and $depth == 0
$wraps
$depth == 0
Truth table33%1/3
A and B
A= $wrapsB= $depth == 0
| |||||||||||||||||||||||||||||||
| 111 | 9 | $out = $inner; | |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||
| 113 | 26 | return $out; | |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||
| 117 | 54 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||
| 118 | 54 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | 54 | my $constraint = $self->index_info->{is_unique_constraint}; | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 54 | ▶ | if ($self->action eq 'create' && $constraint) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->action eq "create" and $constraint
MC/DC100%2/2
$self->action eq "create" and $constraint
$self->action eq "create"
$constraint
Truth table100%3/3
A and B
A= $self->action eq "create"B= $constraint
| |||||||||||||||||||||||||||||||
| 123 | return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s UNIQUE %s(%s);', | ||||||||||||||||||||||||||||||
| 124 | _quote_table($self->table_name), _quote_ident($self->index_name), | ||||||||||||||||||||||||||||||
| 125 | (($self->index_info->{kind} || '') eq 'clustered' ? 'CLUSTERED ' : ''), | ||||||||||||||||||||||||||||||
| 126 | 11 | ▶ | join ', ', map { _quote_ident($_) } @{ $self->index_info->{columns} // [] }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$self->index_info->{'kind'} || ''
$self->index_info->{"columns"} // []
MC/DC100%1/1
$self->index_info->{'kind'} || ''
$self->index_info->{'kind'}
MC/DC0%0/1
$self->index_info->{"columns"} // []
$self->index_info->{"columns"}
Truth table100%2/2
A
A= $self->index_info->{'kind'}
Truth table50%1/2
A
A= $self->index_info->{"columns"}
| |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||
| 128 | 43 | ▶ | if ($self->action eq 'create') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 129 | 19 | ▶ | my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 130 | 19 | ▶ | my $kind = $self->index_info->{kind} || ''; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$self->index_info->{'kind'} || ''
MC/DC100%1/1
$self->index_info->{'kind'} || ''
$self->index_info->{'kind'}
Truth table100%2/2
A
A= $self->index_info->{'kind'}
| |||||||||||||||||||||||||||||||
| 131 | 19 | ▶ | my $kind_sql = $kind eq 'clustered' ? 'CLUSTERED ' : $kind eq 'nonclustered' ? 'NONCLUSTERED ' : ''; | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 132 | my $cols = join ', ', | ||||||||||||||||||||||||||||||
| 133 | 19 | ▶ | map { _quote_ident($_) } @{ $self->index_info->{columns} // [] }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->index_info->{"columns"} // []
MC/DC0%0/1
$self->index_info->{"columns"} // []
$self->index_info->{"columns"}
Truth table50%1/2
A
A= $self->index_info->{"columns"}
| |||||||||||||||||||||||||||||||
| 134 | 19 | my $sql = sprintf 'CREATE %s%sINDEX %s ON %s (%s)', | |||||||||||||||||||||||||||||
| 135 | $unique, | ||||||||||||||||||||||||||||||
| 136 | $kind_sql, | ||||||||||||||||||||||||||||||
| 137 | _quote_ident($self->index_name), | ||||||||||||||||||||||||||||||
| 138 | _quote_table($self->table_name), | ||||||||||||||||||||||||||||||
| 139 | $cols; | ||||||||||||||||||||||||||||||
| 140 | 19 | my $filter = $self->index_info->{filter}; | |||||||||||||||||||||||||||||
| 141 | 19 | ▶ | $sql .= " WHERE $filter" if defined $filter && length $filter; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $filter and length $filter
MC/DC50%1/2
defined $filter and length $filter
defined $filter
length $filter
Truth table66%2/3
A and B
A= defined $filterB= length $filter
| |||||||||||||||||||||||||||||||
| 142 | 19 | return "$sql;"; | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | 24 | ▶ | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 145 | _quote_table($self->table_name), _quote_ident($self->index_name) | ||||||||||||||||||||||||||||||
| 146 | if $constraint; | ||||||||||||||||||||||||||||||
| 147 | 16 | return sprintf 'DROP INDEX %s ON %s;', _quote_ident($self->index_name), _quote_table($self->table_name); | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 152 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 153 | 2 | return sprintf ' %sindex: %s on %s', | |||||||||||||||||||||||||||||
| 154 | $self->summary_prefix, $self->index_name, $self->table_name; | ||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||
| 157 | 38 | 1; | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||||