| 1 | package DBIO::SQLite::Diff::Table; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for SQLite tables | ||||||||||||||||||||||||||||||||||
| 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::Op'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 190 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 190 | ▶ | use DBIO::SQLite::Util qw(default_value_sql quote_identifier type_with_size); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 190 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 190 | __PACKAGE__->mk_diff_accessors(qw/ | |||||||||||||||||||||||||||||||||
| 17 | table_name table_info columns foreign_keys unique_constraints reason | ||||||||||||||||||||||||||||||||||
| 18 | /); | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 370 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 24 | 370 | my ($class, $source, $target, $target_columns, $target_fks, $target_indexes) = @_; | |||||||||||||||||||||||||||||||||
| 25 | 370 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns //= {}
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||||
| 26 | 370 | ▶ | $target_fks //= {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_fks //= {}
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
| |||||||||||||||||||||||||||||||||||
| 27 | 370 | ▶ | $target_indexes //= {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_indexes //= {}
MC/DC100%1/1
$target_indexes //= {}
$target_indexes
Truth table100%2/2
A
A= $target_indexes
| |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | # The generic create/drop walk lives in DBIO::Diff::Op->diff_toplevel; we | ||||||||||||||||||||||||||||||||||
| 30 | # only supply the SQLite-shaped ops (create captures the target table entry, | ||||||||||||||||||||||||||||||||||
| 31 | # whose sql as_sql emits, plus columns + FKs for a model that has none). | ||||||||||||||||||||||||||||||||||
| 32 | return $class->diff_toplevel($source, $target, | ||||||||||||||||||||||||||||||||||
| 33 | 46 | ▶ | create => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||
| 34 | 46 | my ($name) = @_; | |||||||||||||||||||||||||||||||||
| 35 | $class->new( | ||||||||||||||||||||||||||||||||||
| 36 | action => 'create', | ||||||||||||||||||||||||||||||||||
| 37 | table_name => $name, | ||||||||||||||||||||||||||||||||||
| 38 | table_info => $target->{$name}, | ||||||||||||||||||||||||||||||||||
| 39 | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||||||||||||||
| 40 | foreign_keys => $target_fks->{$name} // [], | ||||||||||||||||||||||||||||||||||
| 41 | 46 | ▶ | unique_constraints => _unique_column_lists($target_indexes->{$name}), | ||||||||||||||||||||||||||||||||
|
Condition75%3/4
$target_columns->{$name} // []
$target_fks->{$name} // []
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Truth table100%2/2
A
A= $target_fks->{$name}
| |||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||||||||
| 44 | 95 | ▶ | drop => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 45 | 95 | my ($name) = @_; | |||||||||||||||||||||||||||||||||
| 46 | $class->new( | ||||||||||||||||||||||||||||||||||
| 47 | action => 'drop', | ||||||||||||||||||||||||||||||||||
| 48 | table_name => $name, | ||||||||||||||||||||||||||||||||||
| 49 | 95 | table_info => $source->{$name}, | |||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||||
| 52 | 370 | ); | |||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | # The column lists of the UNIQUE constraints in one table's index hash: the | ||||||||||||||||||||||||||||||||||
| 56 | # indexes of origin 'u', as a set (two constraints on the same columns are one), | ||||||||||||||||||||||||||||||||||
| 57 | # sorted so the text is the same every time. | ||||||||||||||||||||||||||||||||||
| 58 | 46 | ▶ | sub _unique_column_lists { | ||||||||||||||||||||||||||||||||
|
Subroutine
_unique_column_lists: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||
| 59 | 46 | my ($indexes) = @_; | |||||||||||||||||||||||||||||||||
| 60 | 46 | my %seen; | |||||||||||||||||||||||||||||||||
| 61 | 46 | ▶ | for my $idx (values %{ $indexes // {} }) { | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$indexes // {}
MC/DC100%1/1
$indexes // {}
$indexes
Truth table100%2/2
A
A= $indexes
| |||||||||||||||||||||||||||||||||||
| 62 | 10 | ▶ | next unless ($idx->{origin} // '') eq 'u'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$idx->{"origin"} // ""
MC/DC0%0/1
$idx->{"origin"} // ""
$idx->{"origin"}
Truth table50%1/2
A
A= $idx->{"origin"}
| |||||||||||||||||||||||||||||||||||
| 63 | 9 | ▶ | my @cols = @{ $idx->{columns} // [] }; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$idx->{"columns"} // []
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table50%1/2
A
A= $idx->{"columns"}
| |||||||||||||||||||||||||||||||||||
| 64 | 9 | ▶ | next unless @cols; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 9 | $seen{ join "\0", @cols } = \@cols; | |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||
| 67 | 46 | return [ map { $seen{$_} } sort keys %seen ]; | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 147 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=20 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 72 | 147 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 147 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 73 | return sprintf 'DROP TABLE %s;', quote_identifier($self->table_name); | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 74 | ▶ | Carp::croak(sprintf 'Cannot change table %s: %s', | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | $self->table_name, $self->reason) | ||||||||||||||||||||||||||||||||||
| 80 | if $self->action eq 'refuse'; | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | # The statement the target was created with, when the model has it. Nothing | ||||||||||||||||||||||||||||||||||
| 83 | # below can reproduce it from the columns (karr k56). | ||||||||||||||||||||||||||||||||||
| 84 | 70 | ▶ | my $sql = ($self->table_info // {})->{sql}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->table_info // {}
MC/DC0%0/1
$self->table_info // {}
$self->table_info
Truth table50%1/2
A
A= $self->table_info
| |||||||||||||||||||||||||||||||||||
| 85 | 70 | ▶ | if (defined $sql && length $sql) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sql and length $sql
MC/DC50%1/2
defined $sql and length $sql
defined $sql
length $sql
Truth table66%2/3
A and B
A= defined $sqlB= length $sql
| |||||||||||||||||||||||||||||||||||
| 86 | 47 | ▶ | $sql .= ';' unless $sql =~ /;\s*$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | 47 | return $sql; | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | # The key columns in the order of the key, which pk_position holds and which | ||||||||||||||||||||||||||||||||||
| 91 | # need not be the order of the columns (karr k68). A model that gives no | ||||||||||||||||||||||||||||||||||
| 92 | # position keeps the column order. | ||||||||||||||||||||||||||||||||||
| 93 | 23 | my @columns = @{ $self->columns }; | |||||||||||||||||||||||||||||||||
| 94 | 29 | my @pk_cols = map { $columns[$_]{column_name} } | |||||||||||||||||||||||||||||||||
| 95 | sort { | ||||||||||||||||||||||||||||||||||
| 96 | 7 | ▶ | ($columns[$a]{pk_position} // 0) <=> ($columns[$b]{pk_position} // 0) | ||||||||||||||||||||||||||||||||
|
Condition85%6/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/DC100%1/1
$columns[$b]{"pk_position"} // 0
$columns[$b]{"pk_position"}
Truth table66%2/3
A || B
A= $columns[$a]{"pk_position"}B= $a <=> $b
Truth table100%2/2
A
A= $columns[$b]{"pk_position"}
| |||||||||||||||||||||||||||||||||||
| 97 | || $a <=> $b | ||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | 23 | grep { $columns[$_]{is_pk} } 0 .. $#columns; | |||||||||||||||||||||||||||||||||
| 100 | 23 | my @col_defs; | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 23 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||||||||||||||
| 103 | 50 | ▶ | my $type = type_with_size($col->{data_type} // 'TEXT', $col->{size}); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$col->{"data_type"} // "TEXT"
MC/DC0%0/1
$col->{"data_type"} // "TEXT"
$col->{"data_type"}
Truth table50%1/2
A
A= $col->{"data_type"}
| |||||||||||||||||||||||||||||||||||
| 104 | 50 | my $def = sprintf ' %s %s', quote_identifier($col->{column_name}), $type; | |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | # A single-column key goes on the column, which for the type INTEGER is | ||||||||||||||||||||||||||||||||||
| 107 | # what makes it the rowid alias. PRAGMA table_xinfo reports that type as | ||||||||||||||||||||||||||||||||||
| 108 | # plain INTEGER, so the key is not "already in the type" -- leaving it out | ||||||||||||||||||||||||||||||||||
| 109 | # here left the table without one. | ||||||||||||||||||||||||||||||||||
| 110 | 50 | ▶ | $def .= ' PRIMARY KEY' if @pk_cols == 1 && $col->{is_pk}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@pk_cols == 1 and $col->{"is_pk"}
MC/DC100%2/2
@pk_cols == 1 and $col->{"is_pk"}
@pk_cols == 1
$col->{"is_pk"}
Truth table100%3/3
A and B
A= @pk_cols == 1B= $col->{"is_pk"}
| |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 50 | ▶ | $def .= ' NOT NULL' if $col->{not_null}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | 50 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 114 | 12 | $def .= ' DEFAULT ' . default_value_sql($col->{default_value}); | |||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | 50 | push @col_defs, $def; | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | # A multi-column key can only be a table-level constraint. | ||||||||||||||||||||||||||||||||||
| 120 | 23 | ▶ | if (@pk_cols > 1) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||||||||
| 122 | 5 | join(', ', map { quote_identifier($_) } @pk_cols); | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 23 | ▶ | for my $cols (@{ $self->unique_constraints // [] }) { | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->unique_constraints // []
MC/DC0%0/1
$self->unique_constraints // []
$self->unique_constraints
Truth table50%1/2
A
A= $self->unique_constraints
| |||||||||||||||||||||||||||||||||||
| 126 | push @col_defs, sprintf ' UNIQUE (%s)', | ||||||||||||||||||||||||||||||||||
| 127 | 5 | join(', ', map { quote_identifier($_) } @$cols); | |||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | 23 | for my $fk (@{ $self->foreign_keys }) { | |||||||||||||||||||||||||||||||||
| 131 | push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||||||||||||||
| 132 | 2 | join(', ', map { quote_identifier($_) } @{ $fk->{from_columns} }), | |||||||||||||||||||||||||||||||||
| 133 | quote_identifier($fk->{to_table}), | ||||||||||||||||||||||||||||||||||
| 134 | 2 | join(', ', map { quote_identifier($_) } @{ $fk->{to_columns} }); | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 23 | return sprintf "CREATE TABLE %s (\n%s\n);", | |||||||||||||||||||||||||||||||||
| 138 | quote_identifier($self->table_name), join(",\n", @col_defs); | ||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 19 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 143 | 19 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 144 | 19 | ▶ | return sprintf '! table: %s (refused: %s)', $self->table_name, $self->reason | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 145 | if $self->action eq 'refuse'; | ||||||||||||||||||||||||||||||||||
| 146 | 11 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 11 | return sprintf '%s table: %s', $prefix, $self->table_name; | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | 190 | 1; | |||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||||||||