| 1 | package DBIO::SQLite::Diff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Compare two introspected SQLite models | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 190 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 190 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 190 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 190 | ▶ | use base 'DBIO::Diff::Base'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 190 | ▶ | use DBIO::SQLite::Diff::Table; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 190 | ▶ | use DBIO::SQLite::Diff::Column; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 190 | ▶ | use DBIO::SQLite::Diff::Index; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 190 | ▶ | use DBIO::SQLite::Diff::Rebuild; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 190 | ▶ | use DBIO::SQLite::Diff::View; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 190 | ▶ | use DBIO::SQLite::Util qw( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | comparable_statement create_name_index foreign_key_action refuse_qualified_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | split_type_size sql_tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 190 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # The introspected spelling of a compiled default. A plain value becomes the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # literal install_ddl writes for it, by the same function and with the column's | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # type, which decides whether a value of digits is a string, so the two cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # drift apart. Literal SQL comes out of its scalar ref, without the parentheses | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # of DEFAULT (expr), which the PRAGMA does not report. Whatever follows DEFAULT | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # and starts with "(" is that form, so the closing one is the last character -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # no matching needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 75 | ▶ | sub _compiled_default { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compiled_default: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 75 | my ($default, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 75 | ▶ | return $default unless defined $default; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | ▶ | return DBIO::SQLite::Util::default_literal_sql($default, $type) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | unless ref $default eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | my $sql = $$default; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | $sql =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | $sql =~ s/\A\(\s*(.*?)\s*\)\z/$1/s; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | return $sql; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 14 | ▶ | sub target_from_compiled { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
target_from_compiled: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 14 | my ($class, $compiled) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 14 | my (%tables, %columns); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 14 | ▶ | for my $tname (keys %{ $compiled->{tables} // {} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$compiled->{"tables"} // {}
MC/DC0%0/1
$compiled->{"tables"} // {}
$compiled->{"tables"}
Truth table50%1/2
A
A= $compiled->{"tables"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 33 | my $t = $compiled->{tables}{$tname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 33 | refuse_qualified_name(table => $tname); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 32 | $tables{$tname} = { table_name => $tname }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # The key in declared order; the columns come in column order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 32 | ▶ | my @pk = @{ $t->{primary_key} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$t->{"primary_key"} // []
MC/DC0%0/1
$t->{"primary_key"} // []
$t->{"primary_key"}
Truth table50%1/2
A
A= $t->{"primary_key"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 32 | my %pk_position; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 32 | @pk_position{@pk} = 1 .. @pk; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 32 | my @cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 32 | ▶ | for my $c (@{ $t->{columns} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$t->{"columns"} // []
MC/DC0%0/1
$t->{"columns"} // []
$t->{"columns"}
Truth table50%1/2
A
A= $t->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # The type and its size apart, as the introspected model has them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 75 | my ($type, $size) = split_type_size($c->{native_type}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | push @cols, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | column_name => $c->{column_name}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | data_type => $type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | (defined $size ? (size => $size) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | not_null => ($c->{not_null} ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | default_value => _compiled_default($c->{default}, $c->{native_type}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | is_pk => ($c->{is_pk} ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 75 | ▶ | pk_position => $pk_position{ $c->{column_name} } // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%2/2
$pk_position{$c->{"column_name"}} // 0
MC/DC100%1/1
$pk_position{$c->{"column_name"}} // 0
$pk_position{$c->{"column_name"}}
Truth table100%2/2
A
A= $pk_position{$c->{"column_name"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 32 | $columns{$tname} = \@cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 13 | return { tables => \%tables, columns => \%columns, indexes => {}, foreign_keys => {} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 209 | ▶ | sub statements { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
statements: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 209 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 316 | ▶ | return map { $_->can('statements') ? $_->statements : $_->as_sql } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 209 | @{ $self->operations }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 251 | ▶ | sub _is_generated { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_generated: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 251 | my ($column) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 251 | ▶ | my $hidden = $column->{hidden} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$column->{"hidden"} // 0
MC/DC100%1/1
$column->{"hidden"} // 0
$column->{"hidden"}
Truth table100%2/2
A
A= $column->{"hidden"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 251 | ▶ | return $hidden == 2 || $hidden == 3; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$hidden == 2 || $hidden == 3
MC/DC100%2/2
$hidden == 2 || $hidden == 3
$hidden == 2
$hidden == 3
Truth table100%3/3
A || B
A= $hidden == 2B= $hidden == 3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # What an entry of a model's tables section is: 'view', 'virtual' or 'table'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # A view says so in its kind. A virtual table has kind 'table' -- that is what | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # sqlite_master calls it -- and is known by its statement. Everything else is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # a plain table, an entry without kind and without statement included: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # models built by hand and by target_from_compiled have nothing but plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # tables. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1320 | ▶ | sub _kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_kind: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1320 | my ($entry) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1320 | ▶ | return 'view' if ($entry->{kind} // '') eq 'view'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$entry->{"kind"} // ""
MC/DC100%1/1
$entry->{"kind"} // ""
$entry->{"kind"}
Truth table100%2/2
A
A= $entry->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1249 | ▶ | my @tokens = sql_tokens($entry->{sql} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$entry->{"sql"} // ""
MC/DC100%1/1
$entry->{"sql"} // ""
$entry->{"sql"}
Truth table100%2/2
A
A= $entry->{"sql"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1249 | ▶ | return defined create_name_index(\@tokens, 'VIRTUAL', 'TABLE') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ? 'virtual' : 'table'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # A tables section sorted by that: { view => {...}, virtual => {...}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # table => {...} }, each keyed by name like the section itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 732 | ▶ | sub _by_kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_by_kind: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 732 | my ($tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 732 | my %by = (view => {}, virtual => {}, table => {}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 732 | ▶ | $by{ _kind($tables->{$_}) }{$_} = $tables->{$_} for keys %{ $tables // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tables // {}
MC/DC0%0/1
$tables // {}
$tables
Truth table50%1/2
A
A= $tables
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 732 | return \%by; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # The refusals: a name that is a table on both sides, and a virtual one on at | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # least one of them, without being the same virtual table on both. SQLite | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # cannot alter a virtual table, and nothing may stand in for that -- a rebuild | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # copies what SELECT shows and loses the rest of what the module stores. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 366 | ▶ | sub _refused_tables { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refused_tables: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 366 | my ($src, $tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 366 | my @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | my %seen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 366 | for my $name (sort grep { !$seen{$_}++ } | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 366 | keys %{ $src->{virtual} }, keys %{ $tgt->{virtual} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 65 | my ($old, $new) = ($src->{virtual}{$name}, $tgt->{virtual}{$name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 65 | my $reason; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 65 | ▶ | if ($old && $new) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$old and $new
MC/DC100%2/2
$old and $new
$old
$new
Truth table100%3/3
A and B
A= $oldB= $new
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | next if comparable_statement($old->{sql}, 'VIRTUAL', 'TABLE') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | eq comparable_statement($new->{sql}, 'VIRTUAL', 'TABLE'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | $reason = "its definition as a virtual table differs from the target's, " | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | . 'and SQLite cannot alter a virtual table -- a rebuild would lose ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | . 'what its module stores'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | ▶ | elsif ($old && $tgt->{table}{$name}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$old and $tgt->{"table"}{$name}
MC/DC100%2/2
$old and $tgt->{"table"}{$name}
$old
$tgt->{"table"}{$name}
Truth table100%3/3
A and B
A= $oldB= $tgt->{"table"}{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | $reason = 'it is a virtual table in the source and a plain table in ' | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | . 'the target, and SQLite cannot alter a virtual table -- replacing ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | . 'it would lose what its module stores'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ▶ | elsif ($new && $src->{table}{$name}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$new and $src->{"table"}{$name}
MC/DC100%2/2
$new and $src->{"table"}{$name}
$new
$src->{"table"}{$name}
Truth table100%3/3
A and B
A= $newB= $src->{"table"}{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | $reason = 'it is a plain table in the source and a virtual table in ' | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | . 'the target, and no statement turns the one into the other -- ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | . 'replacing it would lose its rows'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 60 | next; # on one side only, or a view on the other: created or dropped | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 4 | push @ops, DBIO::SQLite::Diff::Table->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | action => 'refuse', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | table_info => $new, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | reason => $reason, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 366 | return @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # The primary key of a table as its columns in the order of the key: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # pk_position where the model has it, column order where it does not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 1024 | ▶ | sub _primary_key { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_primary_key: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1024 | my ($columns) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1024 | my @at = grep { $columns->[$_]{is_pk} } 0 .. $#$columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1024 | return map { $columns->[$_]{column_name} } sort { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1024 | ▶ | ($columns->[$a]{pk_position} // 0) <=> ($columns->[$b]{pk_position} // 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition42%3/7
($columns->[$a]{'pk_position'} // 0) <=> ($columns->[$b]{'pk_position'} // 0) || $a <=> $b
$columns->[$a]{"pk_position"} // 0
$columns->[$b]{"pk_position"} // 0
MC/DC0%0/2
($columns->[$a]{'pk_position'} // 0) <=> ($columns->[$b]{'pk_position'} // 0) || $a <=> $b
$columns->[$a]{"pk_position"}
$a <=> $b
MC/DC0%0/1
$columns->[$b]{"pk_position"} // 0
$columns->[$b]{"pk_position"}
Truth table33%1/3
A || B
A= $columns->[$a]{"pk_position"}B= $a <=> $b
Truth table50%1/2
A
A= $columns->[$b]{"pk_position"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | || $a <=> $b | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | } @at; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # The UNIQUE constraints of a table: the column lists of its automatic indexes | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # of origin "u", which is how PRAGMA index_list reports a constraint, written | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # on the column or on the table alike. A set -- SQLite keeps one index for a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # constraint declared twice. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 1024 | ▶ | sub _unique_constraints { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unique_constraints: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 1024 | my ($indexes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 79 | ▶ | return map { join(', ', @{ $_->{columns} // [] }) => 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"columns"} // []
MC/DC0%0/1
$_->{"columns"} // []
$_->{"columns"}
Truth table50%1/2
A
A= $_->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 1024 | ▶ | grep { ($_->{origin} // '') eq 'u' } values %{ $indexes // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$_->{"origin"} // ""
$indexes // {}
MC/DC0%0/1
$_->{"origin"} // ""
$_->{"origin"}
MC/DC100%1/1
$indexes // {}
$indexes
Truth table50%1/2
A
A= $_->{"origin"}
Truth table100%2/2
A
A= $indexes
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # One foreign key as what it does: its columns in order, the table and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # columns it points at, and its two actions. Names and actions are compared in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # any letter case, as SQLite reads them; an action nobody declared is NO | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # ACTION, which is what PRAGMA foreign_key_list reports for it. The value is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # the key as the summary shows it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 227 | ▶ | sub _foreign_key { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_foreign_key: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 227 | my ($fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 227 | ▶ | my @from = @{ $fk->{from_columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fk->{"from_columns"} // []
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 227 | ▶ | my @to = grep { defined } @{ $fk->{to_columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fk->{"to_columns"} // []
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | my %action = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 227 | $_ => foreign_key_action($fk->{$_}, 'foreign key (' . join(', ', @from) | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 453 | ▶ | . ') -> ' . ($fk->{to_table} // '?')) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
foreign_key_action($fk->{$_}, 'foreign key (' . join(", ", @from) . ') -> ' . ($fk->{"to_table"} // "?")) // "NO ACTION"
$fk->{"to_table"} // "?"
MC/DC100%1/1
foreign_key_action($fk->{$_}, 'foreign key (' . join(", ", @from) . ') -> ' . ($fk->{"to_table"} // "?")) // "NO ACTION"
foreign_key_action($fk->{$_}, 'foreign key (' . join(", ", @from) . ') -> ' . ($fk->{"to_table"} // "?"))
MC/DC0%0/1
$fk->{"to_table"} // "?"
$fk->{"to_table"}
Truth table100%2/2
A
A= foreign_key_action($fk->{$_}, 'foreign key (' . join(", ", @from) . ') -> ' . ($fk->{"to_table"} // "?"))
Truth table50%1/2
A
A= $fk->{"to_table"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | // 'NO ACTION' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } qw(on_update on_delete); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | my $key = lc join "\0", join(',', @from), $fk->{to_table} // '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 225 | ▶ | join(',', @to), @action{qw(on_update on_delete)}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fk->{"to_table"} // ""
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"to_table"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 225 | ▶ | my $shown = sprintf '(%s) -> %s%s', join(', ', @from), $fk->{to_table} // '?', | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$fk->{"to_table"} // "?"
MC/DC0%0/1
$fk->{"to_table"} // "?"
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"to_table"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | @to ? ' (' . join(', ', @to) . ')' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 225 | ▶ | $shown .= " ON UPDATE $action{on_update}" if $action{on_update} ne 'NO ACTION'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 225 | ▶ | $shown .= " ON DELETE $action{on_delete}" if $action{on_delete} ne 'NO ACTION'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 225 | return ($key => $shown); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # What only one of two sets has, as reasons: "$what $shown added" for the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # target's, "... removed" for the source's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1041 | ▶ | sub _set_changes { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_changes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 1041 | my ($what, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 25 | (map { "$what $new->{$_} added" } grep { !exists $old->{$_} } sort keys %$new), | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1041 | (map { "$what $old->{$_} removed" } grep { !exists $new->{$_} } sort keys %$old), | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # What two statements declare and no PRAGMA reports, compared: the CHECK, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # COLLATE and generated clause of every column both tables have, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # CHECK constraints of the table. Read by Diff::Column's reader. A statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # it cannot follow is not passed over: then the two statements are compared | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # as they stand, name aside, and any difference is a reason. That errs | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # towards the rebuild, and a rebuild ends it -- afterwards the live statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # is the target's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # No result class can declare any of these clauses, so a target deployed from | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # result classes never has one, and "the target has none" says "not stated", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # not "none wanted". They follow the desired-state contract core's | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # DBIO::Diff::Compare::changed_fields has for a column's fields (core ADR | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # 0019): what the target leaves unstated is not compared, what it states is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # compared with the source, and stated against missing is a change. A column | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # whose target has no CHECK, no COLLATE or is not generated is not compared in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # that clause, and neither are the table's CHECKs when the target has none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # A target that states CHECKs has stated all of them: the sets are compared, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # and one only the source has goes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 510 | ▶ | sub _declared_changes { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_declared_changes: called CC=17 SCAR=29.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 510 | my ($old_sql, $new_sql, $shared) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 510 | my ($old, $new) = map { DBIO::SQLite::Diff::Column::_declarations($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | $old_sql, $new_sql; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | my $readable = $old && $new | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 510 | ▶ | && !grep { !$old->{columns}{$_} || !$new->{columns}{$_} } @$shared; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%3/9
$old && $new && !grep({not $old->{'columns'}{$_} && $new->{'columns'}{$_};} @$shared)
$old and $new
$old->{'columns'}{$_} && $new->{'columns'}{$_}
MC/DC0%0/3
$old && $new && !grep({not $old->{'columns'}{$_} && $new->{'columns'}{$_};} @$shared)
$old
$new
!grep({not $old->{'columns'}{$_} && $new->{'columns'}{$_};} @$shared)
MC/DC0%0/2
$old->{'columns'}{$_} && $new->{'columns'}{$_}
$old->{'columns'}{$_}
$new->{'columns'}{$_}
Truth table25%1/4
A and B && not(C)
A= $oldB= $newC= !grep({not $old->{'columns'}{$_} && $new->{'columns'}{$_};} @$shared)
Truth table33%1/3
A && B
A= $old->{'columns'}{$_}B= $new->{'columns'}{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 510 | ▶ | unless ($readable) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | return if comparable_statement($old_sql, 'TABLE') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | eq comparable_statement($new_sql, 'TABLE'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | return 'the CREATE TABLE statements differ, and one of them cannot be ' | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | . 'read clause by clause'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # Each clause is compared only where the target states it -- see above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 510 | my @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 510 | for my $name (@$shared) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 1249 | my ($was, $is) = ($old->{columns}{$name}, $new->{columns}{$name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | push @reasons, _set_changes("check on column $name:", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 26 | map { +{ map { $_ => "($_)" } @{ $_->{checks} } } } $was, $is) | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 1249 | ▶ | if @{ $is->{checks} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | push @reasons, sprintf 'collation of column %s: %s -> %s', $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | map { $_->{collate} // 'none' } $was, $is | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_->{"collate"} // "none"
MC/DC100%1/1
$_->{"collate"} // "none"
$_->{"collate"}
Truth table100%2/2
A
A= $_->{"collate"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 1249 | ▶ | if defined $is->{collate} && ($was->{collate} // '') ne $is->{collate}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
defined $is->{"collate"} and ($was->{"collate"} // "") ne $is->{"collate"}
$was->{"collate"} // ""
MC/DC100%2/2
defined $is->{"collate"} and ($was->{"collate"} // "") ne $is->{"collate"}
defined $is->{"collate"}
$was->{"collate"}
Truth table100%3/3
A and B
A= defined $is->{"collate"}B= $was->{"collate"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | push @reasons, sprintf 'generated expression of column %s: %s -> %s', $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 6 | ▶ | map { defined $_->{generated} ? "($_->{generated})" : 'none' } $was, $is | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 1249 | ▶ | if defined $is->{generated} && ($was->{generated} // '') ne $is->{generated}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
defined $is->{"generated"} and ($was->{"generated"} // "") ne $is->{"generated"}
$was->{"generated"} // ""
MC/DC100%2/2
defined $is->{"generated"} and ($was->{"generated"} // "") ne $is->{"generated"}
defined $is->{"generated"}
$was->{"generated"}
Truth table100%3/3
A and B
A= defined $is->{"generated"}B= $was->{"generated"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | push @reasons, _set_changes('table check', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 12 | map { +{ map { $_ => "($_)" } @{ $_->{checks} } } } $old, $new) | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 510 | ▶ | if @{ $new->{checks} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 510 | return @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | # Everything a plain table declares beyond the fields of its columns, compared | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # between the two models: why the table has to be rebuilt, as a list of | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # reasons, empty when nothing differs (karr k72). See "When a table is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # rebuilt" for each arm and for what is left out on purpose. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 512 | ▶ | sub _table_changes { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table_changes: called CC=14 SCAR=26.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 512 | my ($source, $target, $table) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 512 | my ($old, $new) = map { $_->{tables}{$table} } $source, $target; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | my ($old_columns, $new_columns) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 512 | ▶ | map { $_->{columns}{$table} // [] } $source, $target; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"columns"}{$table} // []
MC/DC0%0/1
$_->{"columns"}{$table} // []
$_->{"columns"}{$table}
Truth table50%1/2
A
A= $_->{"columns"}{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 512 | my @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # a. The primary key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | my ($old_pk, $new_pk) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 512 | map { join ', ', _primary_key($_) } $old_columns, $new_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 512 | ▶ | push @reasons, sprintf 'primary key (%s) -> (%s)', $old_pk, $new_pk | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | if $old_pk ne $new_pk; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | # b. The UNIQUE constraints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | push @reasons, _set_changes('unique', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 512 | ▶ | map { my %set = _unique_constraints(($_->{indexes} // {})->{$table}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"indexes"} // {}
MC/DC0%0/1
$_->{"indexes"} // {}
$_->{"indexes"}
Truth table50%1/2
A
A= $_->{"indexes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 1024 | +{ map { $_ => "($_)" } keys %set } } $source, $target); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # c. The foreign keys, as a set: a key the database holds twice is one, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # the order of the lines is none of it (karr k74). A key of the target over | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # a column the source has not is left out -- it comes with the column, by | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # ADD COLUMN or by the rebuild that column causes (karr k64). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 512 | my %has = map { lc $_->{column_name} => 1 } @$old_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 114 | my %old_fk = map { _foreign_key($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 512 | ▶ | @{ ($source->{foreign_keys} // {})->{$table} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
+($source->{"foreign_keys"} // {})->{$table} // []
$source->{"foreign_keys"} // {}
MC/DC0%0/1
+($source->{"foreign_keys"} // {})->{$table} // []
$source->{"foreign_keys"}
Truth table50%1/2
A
A= $source->{"foreign_keys"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 113 | my %new_fk = map { _foreign_key($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 118 | ▶ | grep { !grep { !$has{ lc $_ } } @{ $_->{from_columns} // [] } } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"from_columns"} // []
MC/DC0%0/1
$_->{"from_columns"} // []
$_->{"from_columns"}
Truth table50%1/2
A
A= $_->{"from_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 512 | ▶ | @{ ($target->{foreign_keys} // {})->{$table} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
+($target->{"foreign_keys"} // {})->{$table} // []
$target->{"foreign_keys"} // {}
MC/DC0%0/1
+($target->{"foreign_keys"} // {})->{$table} // []
$target->{"foreign_keys"}
Truth table50%1/2
A
A= $target->{"foreign_keys"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 510 | push @reasons, _set_changes('foreign key', \%old_fk, \%new_fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # d. What only the statements say. A source without one states nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 510 | ▶ | if (length($old->{sql} // '')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$old->{"sql"} // ""
MC/DC0%0/1
$old->{"sql"} // ""
$old->{"sql"}
Truth table50%1/2
A
A= $old->{"sql"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 510 | my %in_new = map { $_->{column_name} => 1 } @$new_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 510 | my @shared = grep { $in_new{$_} } map { $_->{column_name} } @$old_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 510 | push @reasons, _declared_changes($old->{sql}, $new->{sql}, \@shared); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # e. The two table options, under the same contract as d: an option the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # target has and the source has not is a change, one only the source has is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # not stated by the target and is left alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 510 | for my $option ([ without_rowid => 'WITHOUT ROWID' ], [ strict => 'STRICT' ]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 1020 | my ($field, $shown) = @$option; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 1020 | ▶ | push @reasons, "$shown added" if $new->{$field} && !$old->{$field}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$new->{$field} and not $old->{$field}
MC/DC100%2/2
$new->{$field} and not $old->{$field}
$new->{$field}
not $old->{$field}
Truth table100%3/3
A and not(B)
A= $new->{$field}B= not $old->{$field}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 510 | return @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # A column that takes a NOT NULL value from nowhere when it is added to a table | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # that holds rows: NOT NULL, no default (or DEFAULT NULL), and not one SQLite | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | # fills itself -- the rowid alias, a generated column. Used here and by | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # DBIO::SQLite::Deploy for a plain ADD COLUMN (karr k91). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 37 | ▶ | sub _added_not_null_without_default { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_added_not_null_without_default: called CC=9 SCAR=23.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 37 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 37 | ▶ | return 0 unless $info->{not_null}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 6 | ▶ | return 0 if defined $info->{default_value} && uc $info->{default_value} ne 'NULL'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"default_value"} and uc $info->{"default_value"} ne "NULL"
MC/DC50%1/2
defined $info->{"default_value"} and uc $info->{"default_value"} ne "NULL"
defined $info->{"default_value"}
uc $info->{"default_value"} ne "NULL"
Truth table66%2/3
A and B
A= defined $info->{"default_value"}B= uc $info->{"default_value"} ne "NULL"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 4 | ▶ | return 0 if $info->{is_auto_increment} || _is_generated($info); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"is_auto_increment"} or _is_generated($info)
MC/DC50%1/2
$info->{"is_auto_increment"} or _is_generated($info)
$info->{"is_auto_increment"}
_is_generated($info)
Truth table66%2/3
A or B
A= $info->{"is_auto_increment"}B= _is_generated($info)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 3 | ▶ | return 0 if $info->{is_pk} && uc($info->{data_type} // '') eq 'INTEGER'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$info->{"is_pk"} and uc($info->{"data_type"} // "") eq "INTEGER"
$info->{"data_type"} // ""
MC/DC0%0/2
$info->{"is_pk"} and uc($info->{"data_type"} // "") eq "INTEGER"
$info->{"is_pk"}
$info->{"data_type"}
Truth table33%1/3
A and B
A= $info->{"is_pk"}B= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 366 | ▶ | sub _build_operations { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_operations: called CC=34 SCAR=35.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 366 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 366 | my ($source, $target) = ($self->source, $self->target); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # What each name is, before anything is walked (karr k67). The walks below | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # are written for tables, and each of them does to a view or a virtual | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # table something SQLite refuses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 366 | my $src = _by_kind($source->{tables}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 366 | my $tgt = _by_kind($target->{tables}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # Views: dropped and created as views, replaced when the definition differs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | my @view_ops = DBIO::SQLite::Diff::View->diff( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | $src->{view}, $tgt->{view}, $source->{triggers}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 366 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | # Tables come and go the same way whether plain or virtual: the stored | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # statement creates either, DROP TABLE removes either. A name that is a view | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | # on the other side is not on that side as far as this walk goes, so a table | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # that becomes a view is dropped here and a view that becomes a table is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # created here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 366 | my %src_tables = (%{ $src->{table} }, %{ $src->{virtual} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 366 | my %tgt_tables = (%{ $tgt->{table} }, %{ $tgt->{virtual} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | my @table_ops = DBIO::SQLite::Diff::Table->diff( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | \%src_tables, \%tgt_tables, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | $target->{columns}, $target->{foreign_keys}, $target->{indexes}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 366 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 366 | my @refused = _refused_tables($src, $tgt); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 366 | push @table_ops, @refused; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # From here on only plain tables: the column walk takes the tables that are | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # plain on both sides. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | my @col_ops = DBIO::SQLite::Diff::Column->diff( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | $source->{columns}, $target->{columns}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | $src->{table}, $tgt->{table}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 366 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # SQLite cannot ALTER a column's type / nullability / default in place. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | # table carrying any such change must be rebuilt wholesale (create-new / | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | # copy / drop / rename), which also subsumes that table's add / drop column | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | # ops. We can only rebuild faithfully when the target table's original | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # CREATE statement is available (the introspect path provides it; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # compiled-model path does not) -- otherwise the per-column ops stand, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # an alter among them refuses to render: as_sql of the diff throws (karr | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # k73). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # The same goes for a column that is added with a declaration ADD COLUMN | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # cannot express -- UNIQUE, PRIMARY KEY, a part in a table constraint. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # Diff::Column marks those, and only where there is a statement (karr k64). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # Every reason is kept, in the order found: the rebuild says why it is there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 366 | my %reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 366 | ▶ | for my $op (grep { $_->action eq 'alter' || $_->needs_rebuild } @col_ops) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_->action eq 'alter' || $_->needs_rebuild
MC/DC100%2/2
$_->action eq 'alter' || $_->needs_rebuild
$_->action eq 'alter'
$_->needs_rebuild
Truth table100%3/3
A || B
A= $_->action eq 'alter'B= $_->needs_rebuild
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 67 | my $table = $op->table_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 67 | ▶ | next unless ($tgt->{table}{$table} // {})->{sql}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$tgt->{"table"}{$table} // {}
MC/DC0%0/1
$tgt->{"table"}{$table} // {}
$tgt->{"table"}{$table}
Truth table50%1/2
A
A= $tgt->{"table"}{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 65 | ▶ | push @{ $reasons{$table} }, $op->action eq 'alter' | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | ? sprintf('column %s: %s', $op->column_name, $op->changes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | : sprintf('column %s is added with a declaration ADD COLUMN cannot carry ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | . '(UNIQUE, PRIMARY KEY, a table constraint or a non-constant DEFAULT)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | $op->column_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | # And for what a table declares beyond its columns' fields: key, UNIQUE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | # foreign keys, CHECK, COLLATE, generated expressions, WITHOUT ROWID and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | # STRICT. None of it can be altered in place, so any difference rebuilds the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # table -- under the same condition, a target that has the statement. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # model without one holds no indexes and no foreign keys by construction, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # which says "not stated", not "none" (karr k72). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 366 | for my $table (sort keys %{ $tgt->{table} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 594 | ▶ | next unless $src->{table}{$table} && $tgt->{table}{$table}{sql}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$src->{"table"}{$table} and $tgt->{"table"}{$table}{"sql"}
MC/DC100%2/2
$src->{"table"}{$table} and $tgt->{"table"}{$table}{"sql"}
$src->{"table"}{$table}
$tgt->{"table"}{$table}{"sql"}
Truth table100%3/3
A and B
A= $src->{"table"}{$table}B= $tgt->{"table"}{$table}{"sql"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 512 | ▶ | my @changes = _table_changes($source, $target, $table) or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 40 | push @{ $reasons{$table} }, @changes; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 364 | my %needs_rebuild = map { $_ => 1 } keys %reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | # What a rebuild asks of the rows the table holds (karr k91): the column ops | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | # that go into the rebuild still know which column becomes NOT NULL and which | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | # NOT NULL column has no default to fill the copied rows with. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 364 | my %data_checks; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 364 | for my $op (grep { $needs_rebuild{ $_->table_name } } @col_ops) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 74 | ▶ | my $new = $op->new_info or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 70 | ▶ | if ($op->action eq 'alter') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 26 | push @{ $data_checks{ $op->table_name } }, | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | { column => $op->column_name, kind => 'nulls' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 46 | ▶ | if $new->{not_null} && !$op->old_info->{not_null}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$new->{"not_null"} and not $op->old_info->{"not_null"}
MC/DC100%2/2
$new->{"not_null"} and not $op->old_info->{"not_null"}
$new->{"not_null"}
not $op->old_info->{"not_null"}
Truth table100%3/3
A and not(B)
A= $new->{"not_null"}B= not $op->old_info->{"not_null"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | elsif ($op->action eq 'add') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | ▶ | push @{ $data_checks{ $op->table_name } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | { column => $op->column_name, kind => 'added' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | if _added_not_null_without_default($new); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # The column ops that go into the rebuild leave with it, and so does what | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # they say about stored values: a dropped column is gone, a lossy change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | # may alter what the copy writes. The rebuild reports it (karr core k399). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 364 | my %lossy_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 364 | for my $op (grep { $needs_rebuild{ $_->table_name } } @col_ops) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 74 | ▶ | push @{ $lossy_columns{ $op->table_name } }, $op->column_name | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op->action eq "drop" or $op->lossy
MC/DC100%2/2
$op->action eq "drop" or $op->lossy
$op->action eq "drop"
$op->lossy
Truth table100%3/3
A or B
A= $op->action eq "drop"B= $op->lossy
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | if $op->action eq 'drop' || $op->lossy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 364 | @col_ops = grep { !$needs_rebuild{ $_->table_name } } @col_ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 364 | my @rebuild_ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 364 | for my $table (sort keys %needs_rebuild) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 252 | my %src_names = map { $_->{column_name} => 1 } | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 93 | ▶ | @{ $source->{columns}{$table} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{"columns"}{$table} // []
MC/DC0%0/1
$source->{"columns"}{$table} // []
$source->{"columns"}{$table}
Truth table50%1/2
A
A= $source->{"columns"}{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # A column the new table generates takes no data: SQLite refuses the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # INSERT (karr k70). PRAGMA table_xinfo marks it, hidden 2 for a VIRTUAL | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # and 3 for a STORED one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 243 | my @copy = map { $_->{column_name} } | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 272 | ▶ | grep { $src_names{ $_->{column_name} } && !_is_generated($_) } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$src_names{$_->{'column_name'}} && !_is_generated($_)
MC/DC100%2/2
$src_names{$_->{'column_name'}} && !_is_generated($_)
$src_names{$_->{'column_name'}}
!_is_generated($_)
Truth table100%3/3
A && not(B)
A= $src_names{$_->{'column_name'}}B= !_is_generated($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 93 | ▶ | @{ $target->{columns}{$table} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target->{"columns"}{$table} // []
MC/DC0%0/1
$target->{"columns"}{$table} // []
$target->{"columns"}{$table}
Truth table50%1/2
A
A= $target->{"columns"}{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # The triggers of the live table go with the DROP; the rebuild creates | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | # them again from their statements (karr k69). A model that has no | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # triggers section has none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 93 | ▶ | my $triggers = ($source->{triggers} // {})->{$table} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
+($source->{"triggers"} // {})->{$table} // {}
$source->{"triggers"} // {}
MC/DC100%1/1
+($source->{"triggers"} // {})->{$table} // {}
$source->{"triggers"}
Truth table100%2/2
A
A= $source->{"triggers"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | push @rebuild_ops, DBIO::SQLite::Diff::Rebuild->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | table_sql => $tgt->{table}{$table}{sql}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | copy_columns => \@copy, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 7 | ▶ | triggers => [ map { $triggers->{$_}{sql} } sort keys %$triggers ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
$data_checks{$table} // []
$lossy_columns{$table} // []
MC/DC100%1/1
$data_checks{$table} // []
$data_checks{$table}
MC/DC100%1/1
$lossy_columns{$table} // []
$lossy_columns{$table}
Truth table100%2/2
A
A= $data_checks{$table}
Truth table100%2/2
A
A= $lossy_columns{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | reasons => $reasons{$table}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | data_checks => $data_checks{$table} // [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 93 | lossy_columns => $lossy_columns{$table} // [], | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | # The rebuild drops the table and with it every index; re-create the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # table's explicit (non-auto) target indexes afterwards. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 93 | ▶ | my $tgt_idx = $target->{indexes}{$table} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target->{"indexes"}{$table} // {}
MC/DC100%1/1
$target->{"indexes"}{$table} // {}
$target->{"indexes"}{$table}
Truth table100%2/2
A
A= $target->{"indexes"}{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 93 | for my $name (sort keys %$tgt_idx) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 29 | my $info = $tgt_idx->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 29 | ▶ | next if DBIO::SQLite::Diff::Index::_is_auto($info); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 11 | push @rebuild_ops, DBIO::SQLite::Diff::Index->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | action => 'create', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | table_name => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | index_name => $name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | index_info => $info, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # Normal index diff for every table that is NOT being rebuilt (rebuilt | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # tables' indexes are handled above). The plain tables of the two sides are | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | # threaded through so Diff::Index can suppress a standalone DROP INDEX for | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # any index whose owning table is itself being dropped this pass -- SQLite's | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | # DROP TABLE already removes those indexes (karr k14). A view and a virtual | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | # table have no index of their own, and a refused table gets no op but its | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | # refusal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 364 | ▶ | my %src_idx = %{ $source->{indexes} // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{"indexes"} // {}
MC/DC0%0/1
$source->{"indexes"} // {}
$source->{"indexes"}
Truth table50%1/2
A
A= $source->{"indexes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 364 | ▶ | my %tgt_idx = %{ $target->{indexes} // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target->{"indexes"} // {}
MC/DC0%0/1
$target->{"indexes"} // {}
$target->{"indexes"}
Truth table50%1/2
A
A= $target->{"indexes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 364 | delete @src_idx{ keys %needs_rebuild, keys %{ $src->{view} }, keys %{ $src->{virtual} } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 364 | delete @tgt_idx{ keys %needs_rebuild, keys %{ $tgt->{view} }, keys %{ $tgt->{virtual} } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 364 | for my $op (@refused) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 4 | delete $src_idx{ $op->table_name }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 4 | delete $tgt_idx{ $op->table_name }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | my @index_ops = DBIO::SQLite::Diff::Index->diff( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | \%src_idx, \%tgt_idx, $src->{table}, $tgt->{table}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 364 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | # The order -- see "The order of the operations". Each step is where it is | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # because of something SQLite checks, measured on libsqlite 3.51.1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | # 1 DROP VIEW first. ALTER TABLE ... DROP COLUMN reads the whole schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | # and fails on any view that names something that is gone (`error in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | # view v: no such column`), on whichever table the column is; and a view | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | # that becomes a table has to be out of the way of the CREATE TABLE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # 2 DROP INDEX before DROP COLUMN: SQLite refuses to drop a column an | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | # index covers (`error in index ... after drop column`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | # 3 Tables come and go. Neither CREATE TABLE nor DROP TABLE reads the rest | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # of the schema. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | # 4 ADD COLUMN does not read it either, and wants only the table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # 5 The rebuilds, each followed by its indexes. They rename under | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | # legacy_alter_table and so read nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | # 6 CREATE VIEW, now that every table and column the target's views name | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # is there -- CREATE VIEW itself would not notice, but | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | # 7 DROP COLUMN does, and by now no view in the database names anything | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | # that is about to go: what is left are the target's views. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | # 8 CREATE INDEX last, over columns that all exist. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 364 | ▶ | my $is = sub { my $action = shift; grep { $_->action eq $action } @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | return [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | $is->(drop => @view_ops), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | $is->(drop => @index_ops), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | @table_ops, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 364 | (grep { $_->action ne 'drop' } @col_ops), | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | @rebuild_ops, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | $is->(create => @view_ops), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | $is->(drop => @col_ops), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | $is->(create => @index_ops), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 190 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||