| 1 | package DBIO::DuckDB::Diff::Index; | ||||||||||||
| 2 | # ABSTRACT: Diff operations for DuckDB indexes | ||||||||||||
| 3 | |||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 4 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | use DBIO::SQL::Util qw(_quote_ident); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 4 | ▶ | use DBIO::DuckDB::DDL::Emit qw(create_index); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use DBIO::Diff::Compare qw(changed_index_fields); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | __PACKAGE__->mk_diff_accessors(qw/table_name index_name index_info/); | |||||||||||
| 14 | |||||||||||||
| 15 | |||||||||||||
| 16 | |||||||||||||
| 17 | 5 | ▶ | sub diff { | ||||||||||
|
Subroutine
diff: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||
| 18 | 5 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||
| 19 | |||||||||||||
| 20 | # Tables present in the source model but absent from the target are being | ||||||||||||
| 21 | # dropped in this same pass. DuckDB's DROP TABLE removes the table's own | ||||||||||||
| 22 | # indexes, so a later standalone DROP INDEX for one of them must be | ||||||||||||
| 23 | # suppressed (karr #8). | ||||||||||||
| 24 | 5 | ▶ | $source_tables //= {}; | ||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||
| 25 | 5 | ▶ | $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 | 5 | grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||
| 28 | |||||||||||||
| 29 | return $class->diff_nested($source, $target, | ||||||||||||
| 30 | scope => 'all', | ||||||||||||
| 31 | 0 | ▶ | changed_when => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 32 | 0 | my ($old, $new) = @_; | |||||||||||
| 33 | 0 | scalar changed_index_fields($old, $new); | |||||||||||
| 34 | }, | ||||||||||||
| 35 | 0 | ▶ | on_new => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 36 | 0 | my ($table_name, $name, $tgt) = @_; | |||||||||||
| 37 | 0 | $class->new( | |||||||||||
| 38 | action => 'create', | ||||||||||||
| 39 | table_name => $table_name, | ||||||||||||
| 40 | index_name => $name, | ||||||||||||
| 41 | index_info => $tgt, | ||||||||||||
| 42 | ); | ||||||||||||
| 43 | }, | ||||||||||||
| 44 | 0 | ▶ | on_changed => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 45 | 0 | my ($table_name, $name, $old, $new) = @_; | |||||||||||
| 46 | return ( | ||||||||||||
| 47 | 0 | $class->new( | |||||||||||
| 48 | action => 'drop', table_name => $table_name, | ||||||||||||
| 49 | index_name => $name, index_info => $old, | ||||||||||||
| 50 | ), | ||||||||||||
| 51 | $class->new( | ||||||||||||
| 52 | action => 'create', table_name => $table_name, | ||||||||||||
| 53 | index_name => $name, index_info => $new, | ||||||||||||
| 54 | ), | ||||||||||||
| 55 | ); | ||||||||||||
| 56 | }, | ||||||||||||
| 57 | 5 | ▶ | on_gone => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 58 | 5 | my ($table_name, $name, $old) = @_; | |||||||||||
| 59 | # Skip when the owning table is itself being dropped this pass; DuckDB's | ||||||||||||
| 60 | # DROP TABLE already removes this index (karr #8). | ||||||||||||
| 61 | 5 | ▶ | return () if $dropped_table{$table_name}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 3 | $class->new( | |||||||||||
| 63 | action => 'drop', | ||||||||||||
| 64 | table_name => $table_name, | ||||||||||||
| 65 | index_name => $name, | ||||||||||||
| 66 | index_info => $old, | ||||||||||||
| 67 | ); | ||||||||||||
| 68 | }, | ||||||||||||
| 69 | 5 | ); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | |||||||||||||
| 73 | 2 | ▶ | sub as_sql { | ||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=27.0
Pod
documented
| |||||||||||||
| 74 | 2 | my ($self) = @_; | |||||||||||
| 75 | |||||||||||||
| 76 | 2 | ▶ | if ($self->action eq 'create') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 0 | ▶ | if (my $sql = $self->index_info->{sql}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 0 | ▶ | $sql .= ';' unless $sql =~ /;\s*$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | return $sql; | |||||||||||
| 80 | } | ||||||||||||
| 81 | return create_index( | ||||||||||||
| 82 | name => $self->index_name, | ||||||||||||
| 83 | table => $self->table_name, | ||||||||||||
| 84 | columns => $self->index_info->{columns} // [], | ||||||||||||
| 85 | unique => $self->index_info->{is_unique}, | ||||||||||||
| 86 | 0 | ▶ | ); | ||||||||||
|
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"}
| |||||||||||||
| 87 | } | ||||||||||||
| 88 | 2 | return sprintf 'DROP INDEX %s;', _quote_ident($self->index_name); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | sub summary { | ||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||
| 94 | 0 | return sprintf ' %sindex: %s on %s', | |||||||||||
| 95 | $self->summary_prefix, $self->index_name, $self->table_name; | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 4 | 1; | |||||||||||
| 99 | |||||||||||||
| 100 | __END__ | ||||||||||||
| 101 | |||||||||||||
| 102 | =pod | ||||||||||||
| 103 | |||||||||||||
| 104 | =encoding UTF-8 | ||||||||||||
| 105 | |||||||||||||
| 106 | =head1 NAME | ||||||||||||
| 107 | |||||||||||||
| 108 | DBIO::DuckDB::Diff::Index - Diff operations for DuckDB indexes | ||||||||||||
| 109 | |||||||||||||
| 110 | =head1 VERSION | ||||||||||||
| 111 | |||||||||||||
| 112 | version 0.900001 | ||||||||||||
| 113 | |||||||||||||
| 114 | =head1 DESCRIPTION | ||||||||||||
| 115 | |||||||||||||
| 116 | Index-level diff operations for DuckDB. DuckDB has no C<ALTER INDEX>, | ||||||||||||
| 117 | so changed definitions become a drop-then-create pair. Indexes backing | ||||||||||||
| 118 | PRIMARY KEY / UNIQUE constraints are filtered out upstream by the | ||||||||||||
| 119 | introspect layer -- they belong to the table, not to explicit CREATE | ||||||||||||
| 120 | INDEX. | ||||||||||||
| 121 | |||||||||||||
| 122 | =head1 METHODS | ||||||||||||
| 123 | |||||||||||||
| 124 | =head2 diff | ||||||||||||
| 125 | |||||||||||||
| 126 | my @ops = DBIO::DuckDB::Diff::Index->diff( | ||||||||||||
| 127 | $source, $target, $source_tables, $target_tables); | ||||||||||||
| 128 | |||||||||||||
| 129 | Compares index sets across all tables. Index identity is by name; changed | ||||||||||||
| 130 | definitions become a drop-then-create pair. | ||||||||||||
| 131 | |||||||||||||
| 132 | The optional C<$source_tables> / C<$target_tables> hashrefs are the C<tables> | ||||||||||||
| 133 | sections of the two models (threaded in by L<DBIO::DuckDB::Diff>). They detect | ||||||||||||
| 134 | tables being dropped in this same pass: DuckDB's C<DROP TABLE> already removes | ||||||||||||
| 135 | that table's own indexes, so emitting a later standalone C<DROP INDEX> for one | ||||||||||||
| 136 | of them would fail once the table is gone (the diff order is | ||||||||||||
| 137 | tables-then-columns-then-indexes). Such drops are suppressed. When these | ||||||||||||
| 138 | arguments are absent (e.g. direct unit-test calls) the dropped-table set is | ||||||||||||
| 139 | empty and no drops are suppressed, preserving the original two-argument | ||||||||||||
| 140 | behaviour. | ||||||||||||
| 141 | |||||||||||||
| 142 | =head2 as_sql | ||||||||||||
| 143 | |||||||||||||
| 144 | =head2 summary | ||||||||||||
| 145 | |||||||||||||
| 146 | =head1 AUTHOR | ||||||||||||
| 147 | |||||||||||||
| 148 | DBIO Authors | ||||||||||||
| 149 | |||||||||||||
| 150 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 151 | |||||||||||||
| 152 | Copyright (C) 2026 DBIO Authors | ||||||||||||
| 153 | |||||||||||||
| 154 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 155 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 156 | |||||||||||||
| 157 | =cut | ||||||||||||