| 1 | package DBIO::PostgreSQL::Diff::Index; | ||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL indexes | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 9 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 9 | __PACKAGE__->mk_diff_accessors(qw(table_key index_name index_info)); | |||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | |||||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | 6 | ▶ | sub diff { | ||||||||||||
|
Subroutine
diff: called CC=15 SCAR=29.8
Pod
documented
| |||||||||||||||
| 17 | 6 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||
| 18 | 6 | my @ops; | |||||||||||||
| 19 | |||||||||||||||
| 20 | # Tables present in the source model but absent from the target are being | ||||||||||||||
| 21 | # dropped in this same pass. Their indexes go with the table via | ||||||||||||||
| 22 | # DROP TABLE ... CASCADE, so a later standalone DROP INDEX for one of them | ||||||||||||||
| 23 | # must be suppressed (karr #32). | ||||||||||||||
| 24 | 6 | ▶ | $source_tables //= {}; | ||||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||||
| 25 | 6 | ▶ | $target_tables //= {}; | ||||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||||
| 26 | 2 | my %dropped_table = map { $_ => 1 } | |||||||||||||
| 27 | 6 | grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||||
| 28 | |||||||||||||||
| 29 | # Collect all index names across all tables | ||||||||||||||
| 30 | 6 | my %src_indexes; | |||||||||||||
| 31 | 6 | for my $table_key (keys %$source) { | |||||||||||||
| 32 | 5 | for my $idx_name (keys %{ $source->{$table_key} }) { | |||||||||||||
| 33 | $src_indexes{$idx_name} = { | ||||||||||||||
| 34 | table_key => $table_key, | ||||||||||||||
| 35 | 5 | index_info => $source->{$table_key}{$idx_name}, | |||||||||||||
| 36 | }; | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 6 | my %tgt_indexes; | |||||||||||||
| 41 | 6 | for my $table_key (keys %$target) { | |||||||||||||
| 42 | 2 | for my $idx_name (keys %{ $target->{$table_key} }) { | |||||||||||||
| 43 | $tgt_indexes{$idx_name} = { | ||||||||||||||
| 44 | table_key => $table_key, | ||||||||||||||
| 45 | 1 | index_info => $target->{$table_key}{$idx_name}, | |||||||||||||
| 46 | }; | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | # New indexes | ||||||||||||||
| 51 | 6 | for my $name (sort keys %tgt_indexes) { | |||||||||||||
| 52 | 1 | ▶ | if (!exists $src_indexes{$name}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | push @ops, $class->new( | ||||||||||||||
| 54 | action => 'create', | ||||||||||||||
| 55 | table_key => $tgt_indexes{$name}{table_key}, | ||||||||||||||
| 56 | index_name => $name, | ||||||||||||||
| 57 | index_info => $tgt_indexes{$name}{index_info}, | ||||||||||||||
| 58 | 1 | ); | |||||||||||||
| 59 | 1 | next; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | # Changed indexes: compare definitions | ||||||||||||||
| 62 | 0 | ▶ | my $src_def = $src_indexes{$name}{index_info}{definition} // ''; | ||||||||||||
|
Condition0%0/2
$src_indexes{$name}{"index_info"}{"definition"} // ""
MC/DC0%0/1
$src_indexes{$name}{"index_info"}{"definition"} // ""
$src_indexes{$name}{"index_info"}{"definition"}
Truth table0%0/2
A
A= $src_indexes{$name}{"index_info"}{"definition"}
| |||||||||||||||
| 63 | 0 | ▶ | my $tgt_def = $tgt_indexes{$name}{index_info}{definition} // ''; | ||||||||||||
|
Condition0%0/2
$tgt_indexes{$name}{"index_info"}{"definition"} // ""
MC/DC0%0/1
$tgt_indexes{$name}{"index_info"}{"definition"} // ""
$tgt_indexes{$name}{"index_info"}{"definition"}
Truth table0%0/2
A
A= $tgt_indexes{$name}{"index_info"}{"definition"}
| |||||||||||||||
| 64 | 0 | ▶ | if ($src_def ne $tgt_def) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | push @ops, $class->new( | ||||||||||||||
| 66 | action => 'drop', | ||||||||||||||
| 67 | table_key => $src_indexes{$name}{table_key}, | ||||||||||||||
| 68 | index_name => $name, | ||||||||||||||
| 69 | index_info => $src_indexes{$name}{index_info}, | ||||||||||||||
| 70 | 0 | ); | |||||||||||||
| 71 | push @ops, $class->new( | ||||||||||||||
| 72 | action => 'create', | ||||||||||||||
| 73 | table_key => $tgt_indexes{$name}{table_key}, | ||||||||||||||
| 74 | index_name => $name, | ||||||||||||||
| 75 | index_info => $tgt_indexes{$name}{index_info}, | ||||||||||||||
| 76 | 0 | ); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | # Dropped indexes | ||||||||||||||
| 81 | 6 | for my $name (sort keys %src_indexes) { | |||||||||||||
| 82 | 5 | ▶ | next if exists $tgt_indexes{$name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | # Skip when the owning table is itself being dropped this pass; the | ||||||||||||||
| 84 | # DROP TABLE ... CASCADE already removes this index (karr #32). | ||||||||||||||
| 85 | 5 | ▶ | next if $dropped_table{ $src_indexes{$name}{table_key} }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | push @ops, $class->new( | ||||||||||||||
| 87 | action => 'drop', | ||||||||||||||
| 88 | table_key => $src_indexes{$name}{table_key}, | ||||||||||||||
| 89 | index_name => $name, | ||||||||||||||
| 90 | index_info => $src_indexes{$name}{index_info}, | ||||||||||||||
| 91 | 3 | ); | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 6 | return @ops; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | |||||||||||||||
| 98 | 4 | ▶ | sub as_sql { | ||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||
| 99 | 4 | my ($self) = @_; | |||||||||||||
| 100 | 4 | ▶ | if ($self->action eq 'create') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 101 | # Use the full definition from introspection if available | ||||||||||||||
| 102 | 1 | ▶ | if ($self->index_info->{definition}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 1 | return $self->index_info->{definition} . ';'; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | return sprintf 'CREATE INDEX %s ON %s (%s);', | ||||||||||||||
| 106 | $self->index_name, $self->table_key, | ||||||||||||||
| 107 | 0 | ▶ | join(', ', @{ $self->index_info->{columns} // ['?'] }); | ||||||||||||
|
Condition0%0/2
$self->index_info->{"columns"} // ["?"]
MC/DC0%0/1
$self->index_info->{"columns"} // ["?"]
$self->index_info->{"columns"}
Truth table0%0/2
A
A= $self->index_info->{"columns"}
| |||||||||||||||
| 108 | } | ||||||||||||||
| 109 | elsif ($self->action eq 'drop') { | ||||||||||||||
| 110 | 3 | return sprintf 'DROP INDEX %s;', $self->index_name; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | ▶ | sub summary { | ||||||||||||
|
Subroutine
summary: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 116 | 0 | my ($self) = @_; | |||||||||||||
| 117 | 0 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 0 | return sprintf ' %sindex: %s (on %s)', $prefix, $self->index_name, $self->table_key; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 9 | 1; | |||||||||||||
| 122 | |||||||||||||||
| 123 | __END__ | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =pod | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =encoding UTF-8 | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head1 NAME | ||||||||||||||
| 130 | |||||||||||||||
| 131 | DBIO::PostgreSQL::Diff::Index - Diff operations for PostgreSQL indexes | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =head1 VERSION | ||||||||||||||
| 134 | |||||||||||||||
| 135 | version 0.900001 | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head1 DESCRIPTION | ||||||||||||||
| 138 | |||||||||||||||
| 139 | Represents an index-level diff operation: C<CREATE INDEX> or C<DROP INDEX>. | ||||||||||||||
| 140 | When an index definition changes, it generates a C<DROP> followed by a | ||||||||||||||
| 141 | C<CREATE>. The full definition string from C<pg_get_indexdef> is used for | ||||||||||||||
| 142 | comparison, so partial indexes, expression indexes, and storage parameters are | ||||||||||||||
| 143 | all detected correctly. | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =head1 ATTRIBUTES | ||||||||||||||
| 146 | |||||||||||||||
| 147 | =head2 table_key | ||||||||||||||
| 148 | |||||||||||||||
| 149 | The C<schema.table> key for the table this index belongs to. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =head2 index_name | ||||||||||||||
| 152 | |||||||||||||||
| 153 | The index name. | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head2 index_info | ||||||||||||||
| 156 | |||||||||||||||
| 157 | Index metadata hashref (C<definition>, C<access_method>, C<columns>, etc.). | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =head1 METHODS | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =head2 diff | ||||||||||||||
| 162 | |||||||||||||||
| 163 | my @ops = DBIO::PostgreSQL::Diff::Index->diff( | ||||||||||||||
| 164 | $source, $target, $source_tables, $target_tables); | ||||||||||||||
| 165 | |||||||||||||||
| 166 | Compares index sets across all tables. Index identity is by name; definition | ||||||||||||||
| 167 | changes produce a drop-then-create pair. | ||||||||||||||
| 168 | |||||||||||||||
| 169 | The optional C<$source_tables> / C<$target_tables> hashrefs are the C<tables> | ||||||||||||||
| 170 | sections of the two models (threaded in by L<DBIO::PostgreSQL::Diff> via its | ||||||||||||||
| 171 | aux-section wiring). They are used to detect tables that are being dropped in | ||||||||||||||
| 172 | this same diff pass: a C<DROP TABLE ... CASCADE> already removes that table's | ||||||||||||||
| 173 | own indexes, so emitting a standalone C<DROP INDEX> for one of them would fail | ||||||||||||||
| 174 | with C<index ... does not exist> and abort the deploy (karr #32). When these | ||||||||||||||
| 175 | arguments are absent (e.g. direct unit-test calls) the set is empty and no | ||||||||||||||
| 176 | drops are suppressed, preserving the original two-argument behaviour. | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =head2 as_sql | ||||||||||||||
| 179 | |||||||||||||||
| 180 | Returns the SQL for this operation. For C<create>, uses the full | ||||||||||||||
| 181 | C<pg_get_indexdef> definition string when available, otherwise generates a | ||||||||||||||
| 182 | basic C<CREATE INDEX> statement. For C<drop>, returns C<DROP INDEX name;>. | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =head2 summary | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Returns a one-line description such as C<+index: idx_users_tags (on auth.users)>. | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||
| 189 | |||||||||||||||
| 190 | DBIO & DBIx::Class Authors | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 193 | |||||||||||||||
| 194 | Copyright (C) 2026 DBIO Authors | ||||||||||||||
| 195 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||
| 196 | Based on DBIx::Class, heavily modified. | ||||||||||||||
| 197 | |||||||||||||||
| 198 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 199 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =cut | ||||||||||||||