| 1 | package DBIO::MSSQL::Diff::Index; | ||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL indexes | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 3 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 3 | ▶ | use DBIO::SQL::Util qw(_quote_ident); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 3 | ▶ | use DBIO::Diff::Compare qw(changed_fields); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | 3 | __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/); | |||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | 11 | ▶ | sub diff { | ||||||||||||
|
Subroutine
diff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 17 | 11 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||
| 18 | |||||||||||||||
| 19 | # Tables present in the source model but absent from the target are being | ||||||||||||||
| 20 | # dropped in this same pass. MSSQL's DROP TABLE also removes the table's own | ||||||||||||||
| 21 | # indexes, so a later standalone DROP INDEX ... ON <table> would fail (the | ||||||||||||||
| 22 | # table no longer exists). Suppress those drops (karr #15). Absent table | ||||||||||||||
| 23 | # sections yield an empty set, preserving the original two-argument | ||||||||||||||
| 24 | # behaviour used by direct unit-test calls. | ||||||||||||||
| 25 | 11 | ▶ | $source_tables //= {}; | ||||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||||
| 26 | 11 | ▶ | $target_tables //= {}; | ||||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||||
| 27 | 3 | my %dropped_table = map { $_ => 1 } | |||||||||||||
| 28 | 11 | grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 4 | ▶ | my $create = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 4 | my ($table, $name, $info) = @_; | |||||||||||||
| 32 | 4 | $class->new(action => 'create', table_name => $table, | |||||||||||||
| 33 | index_name => $name, index_info => $info); | ||||||||||||||
| 34 | 11 | }; | |||||||||||||
| 35 | 5 | ▶ | my $drop = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 5 | my ($table, $name, $info) = @_; | |||||||||||||
| 37 | 5 | $class->new(action => 'drop', table_name => $table, | |||||||||||||
| 38 | index_name => $name, index_info => $info); | ||||||||||||||
| 39 | 11 | }; | |||||||||||||
| 40 | |||||||||||||||
| 41 | # scope 'all': indexes are diffed for every target table (including | ||||||||||||||
| 42 | # brand-new ones, whose standalone indexes are not created inline by the | ||||||||||||||
| 43 | # table op) plus a trailing drop pass for source-only tables. | ||||||||||||||
| 44 | return $class->diff_nested($source, $target, | ||||||||||||||
| 45 | scope => 'all', | ||||||||||||||
| 46 | 2 | ▶ | changed_when => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 2 | scalar changed_fields($_[0], $_[1], bool => ['is_unique'], dim => ['columns']); | |||||||||||||
| 48 | }, | ||||||||||||||
| 49 | 2 | ▶ | on_new => sub { $create->(@_) }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | 2 | ▶ | on_changed => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 51 | 2 | my ($table, $name, $old, $new) = @_; | |||||||||||||
| 52 | 2 | ($drop->($table, $name, $old), $create->($table, $name, $new)); | |||||||||||||
| 53 | }, | ||||||||||||||
| 54 | 5 | ▶ | on_gone => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 55 | 5 | my ($table, $name, $info) = @_; | |||||||||||||
| 56 | # Skip when the owning table is itself being dropped this pass; the | ||||||||||||||
| 57 | # DROP TABLE already removes this index (karr #15). | ||||||||||||||
| 58 | 5 | ▶ | return () if $dropped_table{$table}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 3 | return $drop->($table, $name, $info); | |||||||||||||
| 60 | }, | ||||||||||||||
| 61 | 11 | ); | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | |||||||||||||||
| 65 | 6 | ▶ | sub as_sql { | ||||||||||||
|
Subroutine
as_sql: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 66 | 6 | my ($self) = @_; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 6 | ▶ | if ($self->action eq 'create') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 3 | ▶ | my $unique = $self->index_info->{is_unique} ? 'UNIQUE ' : ''; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 3 | ▶ | 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'}
| |||||||||||||||
| 71 | 3 | ▶ | my $kind_sql = $kind eq 'clustered' ? 'CLUSTERED' : $kind eq 'nonclustered' ? 'NONCLUSTERED' : ''; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 72 | my $cols = join ', ', | ||||||||||||||
| 73 | 3 | ▶ | 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"}
| |||||||||||||||
| 74 | 3 | ▶ | my $sql = sprintf 'CREATE %sINDEX %s ON %s %s(%s)', | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | $unique, | ||||||||||||||
| 76 | _quote_ident($self->index_name), | ||||||||||||||
| 77 | _quote_ident($self->table_name), | ||||||||||||||
| 78 | $kind_sql ? "$kind_sql " : '', | ||||||||||||||
| 79 | $cols; | ||||||||||||||
| 80 | 3 | return "$sql;"; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | 3 | return sprintf 'DROP INDEX %s ON %s;', _quote_ident($self->index_name), _quote_ident($self->table_name); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | |||||||||||||||
| 86 | 2 | ▶ | sub summary { | ||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 87 | 2 | my ($self) = @_; | |||||||||||||
| 88 | 2 | return sprintf ' %sindex: %s on %s', | |||||||||||||
| 89 | $self->summary_prefix, $self->index_name, $self->table_name; | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 3 | 1; | |||||||||||||
| 93 | |||||||||||||||
| 94 | __END__ | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =pod | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =encoding UTF-8 | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head1 NAME | ||||||||||||||
| 101 | |||||||||||||||
| 102 | DBIO::MSSQL::Diff::Index - Diff operations for MSSQL indexes | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =head1 VERSION | ||||||||||||||
| 105 | |||||||||||||||
| 106 | version 0.900001 | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Index-level diff operations for MSSQL. MSSQL supports C<CREATE INDEX>, | ||||||||||||||
| 111 | C<DROP INDEX>, and C<CREATE INDEX ... DROP_EXISTING>. Built on | ||||||||||||||
| 112 | L<DBIO::Diff::Op> (the nested walk). A definition change is a drop-then- | ||||||||||||||
| 113 | create pair. Index column I<order> is significant, so the column list is | ||||||||||||||
| 114 | compared as an order-preserving C<dim> field (not the order-independent | ||||||||||||||
| 115 | default of L<DBIO::Diff::Compare/changed_index_fields>). | ||||||||||||||
| 116 | |||||||||||||||
| 117 | =head1 METHODS | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head2 diff | ||||||||||||||
| 120 | |||||||||||||||
| 121 | my @ops = DBIO::MSSQL::Diff::Index->diff( | ||||||||||||||
| 122 | $source, $target, $source_tables, $target_tables); | ||||||||||||||
| 123 | |||||||||||||||
| 124 | The optional C<$source_tables> / C<$target_tables> hashrefs are the C<tables> | ||||||||||||||
| 125 | sections of the two models (threaded in by L<DBIO::MSSQL::Diff>). They detect | ||||||||||||||
| 126 | tables that are being dropped in this same diff pass: MSSQL's C<DROP TABLE> | ||||||||||||||
| 127 | already removes that table's own indexes, so emitting a standalone | ||||||||||||||
| 128 | C<DROP INDEX ... ON <table>> for one of them would fail once the table is gone | ||||||||||||||
| 129 | and abort the deploy (karr #15). When these arguments are absent (e.g. direct | ||||||||||||||
| 130 | unit-test calls) the set is empty and no drops are suppressed, preserving the | ||||||||||||||
| 131 | original two-argument behaviour. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =head2 as_sql | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head2 summary | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head1 AUTHOR | ||||||||||||||
| 138 | |||||||||||||||
| 139 | DBIO & DBIx::Class Authors | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 142 | |||||||||||||||
| 143 | Copyright (C) 2026 DBIO Authors | ||||||||||||||
| 144 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||
| 145 | Based on DBIx::Class, heavily modified. | ||||||||||||||
| 146 | |||||||||||||||
| 147 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 148 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =cut | ||||||||||||||