| 1 | package DBIO::MySQL::Diff::Table; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MySQL/MariaDB tables | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 57 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 57 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 57 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 57 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 57 | ▶ | use DBIO::MySQL::Identifier qw(quote_ident quote_table_in); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 57 | ▶ | use DBIO::MySQL::DefaultValue qw(default_sql on_update_sql); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 57 | ▶ | use DBIO::MySQL::Diff::PrimaryKey 'primary_key_columns'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 57 | ▶ | use DBIO::MySQL::Diff::ForeignKey (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 57 | ▶ | use DBIO::MySQL::Diff::Column (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 57 | ▶ | use DBIO::MySQL::DDL (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 57 | ▶ | use DBIO::MySQL::Diff::View (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 57 | ▶ | use DBIO::MySQL::IndexType (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 57 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 57 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||||||||||||||||
| 22 | table_name table_info old_info columns foreign_keys indexes database | ||||||||||||||||||||||||||||||||||||||||||
| 23 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 183 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 183 | my ($class, $source, $target, $target_columns, $target_fks, $target_indexes, $source_columns, $source_fks) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 183 | ▶ | $source_columns //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_columns //= {}
MC/DC100%1/1
$source_columns //= {}
$source_columns
Truth table100%2/2
A
A= $source_columns
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 183 | ▶ | $source_fks //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_fks //= {}
MC/DC100%1/1
$source_fks //= {}
$source_fks
Truth table100%2/2
A
A= $source_fks
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 183 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns //= {}
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 183 | ▶ | $target_fks //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_fks //= {}
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 183 | ▶ | $target_indexes //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_indexes //= {}
MC/DC100%1/1
$target_indexes //= {}
$target_indexes
Truth table100%2/2
A
A= $target_indexes
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # a view is no table (karr k79): DBIO::MySQL::Diff::View | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 183 | ($source) = DBIO::MySQL::Diff::View::without_views($source); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 183 | ($target) = DBIO::MySQL::Diff::View::without_views($target); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 183 | my @converts; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 183 | for my $name (sort keys %$target) { | |||||||||||||||||||||||||||||||||||||||||
| 42 | 216 | my ($old, $new) = ($source->{$name}, $target->{$name}); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 216 | ▶ | next unless $old; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 168 | ▶ | next unless defined $old->{table_collation} && defined $new->{table_collation}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $old->{"table_collation"} and defined $new->{"table_collation"}
MC/DC100%2/2
defined $old->{"table_collation"} and defined $new->{"table_collation"}
defined $old->{"table_collation"}
defined $new->{"table_collation"}
Truth table100%3/3
A and B
A= defined $old->{"table_collation"}B= defined $new->{"table_collation"}
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 21 | ▶ | next if lc $old->{table_collation} eq lc $new->{table_collation}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | refuse_unprovable_conversion($name, $old->{table_collation}, $new->{table_collation}, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | $source_columns->{$name} // [], $source_fks); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source_columns->{$name} // []
MC/DC0%0/1
$source_columns->{$name} // []
$source_columns->{$name}
Truth table50%1/2
A
A= $source_columns->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 7 | push @converts, $class->new( | |||||||||||||||||||||||||||||||||||||||||
| 49 | action => 'convert', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | table_info => $new, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | old_info => $old, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | return (@converts, $class->diff_toplevel( | ||||||||||||||||||||||||||||||||||||||||||
| 57 | $source, $target, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 48 | ▶ | create => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 48 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 61 | action => 'create', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | table_info => $target->{$name}, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||||||||||||||||||||||
| 65 | foreign_keys => $target_fks->{$name} // [], | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 48 | ▶ | indexes => $target_indexes->{$name} // [], | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$target_columns->{$name} // []
$target_fks->{$name} // []
$target_indexes->{$name} // []
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
MC/DC100%1/1
$target_indexes->{$name} // []
$target_indexes->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Truth table100%2/2
A
A= $target_fks->{$name}
Truth table100%2/2
A
A= $target_indexes->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 14 | ▶ | drop => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 14 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 72 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||||||
| 73 | table_name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 14 | table_info => $source->{$name}, | |||||||||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 177 | )); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # MariaDB before 10.6 reports utf8, MySQL 8 and MariaDB 11 utf8mb3. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 33 | ▶ | sub _charset { my $c = lc($_[0] // ''); $c eq 'utf8' ? 'utf8mb3' : $c } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_charset: called CC=3 SCAR=12.0
Branch: 1 branch
Condition50%1/2
$_[0] // ""
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 13 | ▶ | sub refuse_unprovable_conversion { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
refuse_unprovable_conversion: called CC=22 SCAR=31.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 13 | my ($table, $old, $new, $columns, $fks) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 6 | ▶ | my $refuse = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | DBIO::Exception->throw(sprintf | |||||||||||||||||||||||||||||||||||||||||
| 88 | "DBIO::MySQL::Diff: table %s has the collation %s and the target gives it %s, " | ||||||||||||||||||||||||||||||||||||||||||
| 89 | . "and ALTER TABLE ... CONVERT TO is refused because it would not keep the data: %s. " | ||||||||||||||||||||||||||||||||||||||||||
| 90 | . "Convert the table by hand, or leave its collation as it is", | ||||||||||||||||||||||||||||||||||||||||||
| 91 | quote_ident($table), $old, $new, $_[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 13 | }; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 13 | ▶ | my ($to) = $new =~ /^([^_]+)_/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | or $refuse->("the character set of the collation $new cannot be told"); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 13 | $to = _charset($to); | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 13 | my %text; # the columns CONVERT TO rewrites | |||||||||||||||||||||||||||||||||||||||||
| 99 | 13 | for my $col (@$columns) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 21 | ▶ | next unless defined $col->{character_set}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 20 | $text{ $col->{column_name} } = 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 20 | ▶ | next if _charset($col->{character_set}) eq $to; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | my $name = quote_ident($col->{column_name}); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 8 | ▶ | $refuse->("column $name is $col->{character_set} and not $to, and a conversion into a character set other than utf8mb4 may lose characters") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | unless $to eq 'utf8mb4'; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | my ($base) = lc($col->{column_type} // $col->{data_type} // '') =~ /^(\w+)/; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($col->{"column_type"} // $col->{"data_type"}) // ""
$col->{"column_type"} // $col->{"data_type"}
MC/DC0%0/2
($col->{"column_type"} // $col->{"data_type"}) // ""
$col->{"column_type"}
$col->{"data_type"}
Truth table33%1/3
A // B
A= $col->{"column_type"}B= $col->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | ▶ | $base //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$base //= ""
MC/DC0%0/1
$base //= ""
$base
Truth table50%1/2
A
A= $base
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 7 | ▶ | $refuse->("column $name is a $base and the server changes its type (tinytext, text and mediumtext become text, mediumtext and longtext) while converting it to $to") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | if $base =~ /^(?:tinytext|text|mediumtext)$/; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | ▶ | $refuse->("column $name is an $base, and the server drops its members or the data while converting it to $to") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | if $base =~ /^(?:enum|set)$/; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 9 | for my $from (sort keys %$fks) { | |||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | for my $fk (@{ $fks->{$from} }) { | |||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | my $role = $from eq $table && grep({ $text{$_} } @{ $fk->{from_columns} // [] }) ? 'a column of the table references' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition57%4/7
$fk->{"from_columns"} // []
($fk->{"to_table"} // "") eq $table and grep {$text{$_};} @{$fk->{"to_columns"} // [];}
$fk->{"to_table"} // ""
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/2
($fk->{"to_table"} // "") eq $table and grep {$text{$_};} @{$fk->{"to_columns"} // [];}
$fk->{"to_table"}
grep {$text{$_};} @{$fk->{"to_columns"} // [];}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Truth table33%1/3
A and B
A= $fk->{"to_table"}B= grep {$text{$_};} @{$fk->{"to_columns"} // [];}
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | ▶ | : ($fk->{to_table} // '') eq $table && grep({ $text{$_} } @{ $fk->{to_columns} // [] }) ? 'a column of the table is referenced by' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$from eq $table and grep {$text{$_};} @{$fk->{"from_columns"} // [];}
$fk->{"to_columns"} // []
MC/DC100%2/2
$from eq $table and grep {$text{$_};} @{$fk->{"from_columns"} // [];}
$from eq $table
grep {$text{$_};} @{$fk->{"from_columns"} // [];}
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table100%3/3
A and B
A= $from eq $tableB= grep {$text{$_};} @{$fk->{"from_columns"} // [];}
Truth table50%1/2
A
A= $fk->{"to_columns"}
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | : next; | ||||||||||||||||||||||||||||||||||||||||||
| 119 | $refuse->(sprintf "%s %s (constraint %s), and the server converts neither side of such a pair on its own", | ||||||||||||||||||||||||||||||||||||||||||
| 120 | $role, $from eq $table ? quote_ident($fk->{to_table}) : quote_ident($from), | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | defined $fk->{constraint_name} ? quote_ident($fk->{constraint_name}) : '(unnamed)'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 90 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=26 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 90 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 90 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 14 | return sprintf 'DROP TABLE %s;', quote_table_in($self->database, $self->table_name); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 76 | ▶ | if ($self->action eq 'convert') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 6 | my $collate = $self->table_info->{table_collation}; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | my ($charset) = $collate =~ /^([^_]+)_/; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 6 | return sprintf 'ALTER TABLE %s CONVERT TO CHARACTER SET %s COLLATE %s;', | |||||||||||||||||||||||||||||||||||||||||
| 139 | quote_table_in($self->database, $self->table_name), $charset, $collate; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 70 | my @col_defs; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 70 | my @pk_cols = primary_key_columns($self->columns); | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 70 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||||||||||||||||||||||
| 146 | 119 | ▶ | my $type = $col->{column_type} || $col->{data_type} || 'text'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
$col->{'column_type'} || $col->{'data_type'} || 'text'
$col->{"column_type"} or $col->{"data_type"}
MC/DC0%0/2
$col->{'column_type'} || $col->{'data_type'} || 'text'
$col->{"column_type"}
$col->{"data_type"}
Truth table33%1/3
A or B
A= $col->{"column_type"}B= $col->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 119 | my $def = sprintf ' %s %s', quote_ident($col->{column_name}), $type; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 119 | ▶ | $def .= DBIO::MySQL::Diff::Column::collation_sql($col, ($self->table_info // {})->{table_collation}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->table_info // {}
MC/DC100%1/1
$self->table_info // {}
$self->table_info
Truth table100%2/2
A
A= $self->table_info
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 119 | ▶ | $def .= ' NOT NULL' if $col->{not_null}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 119 | ▶ | $def .= ' AUTO_INCREMENT' if $col->{is_auto_increment}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 119 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 11 | $def .= " DEFAULT " . default_sql($col->{default_value}, $col->{default_is_expression}); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 119 | $def .= on_update_sql($col->{on_update}); | |||||||||||||||||||||||||||||||||||||||||
| 155 | 119 | push @col_defs, $def; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 70 | ▶ | if (@pk_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 51 | join(', ', map { quote_ident($_) } @pk_cols); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | # The key of an AUTO_INCREMENT column outside the primary key (karr k107). | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 70 | ▶ | for my $ix (@{ $self->indexes // [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->indexes // []
MC/DC100%1/1
$self->indexes // []
$self->indexes
Truth table100%2/2
A
A= $self->indexes
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 3 | my $info = $ix->{index_info}; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | my (undef, $type) = DBIO::MySQL::IndexType::index_type_split($info->{index_type}); | |||||||||||||||||||||||||||||||||||||||||
| 167 | push @col_defs, sprintf ' %sKEY %s (%s)%s', | ||||||||||||||||||||||||||||||||||||||||||
| 168 | ($info->{is_unique} ? 'UNIQUE ' : ''), quote_ident($ix->{index_name}), | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | join(', ', map { quote_ident($_) } @{ $info->{columns} // [] }), $type // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$info->{"columns"} // []
$type // ""
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $info->{"columns"}
Truth table50%1/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 70 | for my $fk (@{ $self->foreign_keys }) { | |||||||||||||||||||||||||||||||||||||||||
| 173 | 31 | DBIO::MySQL::Diff::ForeignKey::refuse_set_default($self->table_name, $fk->{constraint_name}, $fk); | |||||||||||||||||||||||||||||||||||||||||
| 174 | # Named like install_ddl names it: the introspected name, or for a model | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # that carries none <table>_<columns>_fk (DBIO::MySQL::DDL::foreign_key_name), | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # never the server's <table>_ibfk_N (karr k106). | ||||||||||||||||||||||||||||||||||||||||||
| 177 | my $fk_name = $fk->{constraint_name} | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 27 | ▶ | // DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{ $fk->{from_columns} }); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$fk->{"constraint_name"} // DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$fk{"from_columns"};})
MC/DC0%0/2
$fk->{"constraint_name"} // DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$fk{"from_columns"};})
$fk->{"constraint_name"}
DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$fk{"from_columns"};})
Truth table66%2/3
A // B
A= $fk->{"constraint_name"}B= DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$fk{"from_columns"};})
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | my $def = sprintf ' CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||||||||||||||||||||||
| 180 | quote_ident($fk_name), | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 27 | join(', ', map { quote_ident($_) } @{ $fk->{from_columns} }), | |||||||||||||||||||||||||||||||||||||||||
| 182 | quote_ident($fk->{to_table}), | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 27 | join(', ', map { quote_ident($_) } @{ $fk->{to_columns} }); | |||||||||||||||||||||||||||||||||||||||||
| 184 | # The actions, as DBIO::MySQL::Diff::ForeignKey writes them (karr k106). | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 27 | ▶ | $def .= " ON UPDATE $fk->{on_update}" if $fk->{on_update} && $fk->{on_update} ne 'NO ACTION'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fk->{"on_update"} and $fk->{"on_update"} ne "NO ACTION"
MC/DC100%2/2
$fk->{"on_update"} and $fk->{"on_update"} ne "NO ACTION"
$fk->{"on_update"}
$fk->{"on_update"} ne "NO ACTION"
Truth table100%3/3
A and B
A= $fk->{"on_update"}B= $fk->{"on_update"} ne "NO ACTION"
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 27 | ▶ | $def .= " ON DELETE $fk->{on_delete}" if $fk->{on_delete} && $fk->{on_delete} ne 'NO ACTION'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fk->{"on_delete"} and $fk->{"on_delete"} ne "NO ACTION"
MC/DC100%2/2
$fk->{"on_delete"} and $fk->{"on_delete"} ne "NO ACTION"
$fk->{"on_delete"}
$fk->{"on_delete"} ne "NO ACTION"
Truth table100%3/3
A and B
A= $fk->{"on_delete"}B= $fk->{"on_delete"} ne "NO ACTION"
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 27 | push @col_defs, $def; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 66 | ▶ | my $info = $self->table_info // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->table_info // {}
MC/DC100%1/1
$self->table_info // {}
$self->table_info
Truth table100%2/2
A
A= $self->table_info
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 66 | ▶ | my $engine = $info->{engine} || 'InnoDB'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$info->{'engine'} || 'InnoDB'
MC/DC100%1/1
$info->{'engine'} || 'InnoDB'
$info->{'engine'}
Truth table100%2/2
A
A= $info->{'engine'}
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | # The character set and collation install_ddl gives a table that names none | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # (karr k82): without COLLATE the server fills in its own default, and the | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # text columns of the table differ from the schema in every later diff. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | my ($charset, $collate) = ($info->{table_collation} && $info->{table_collation} =~ /^([^_]+)_/) | ||||||||||||||||||||||||||||||||||||||||||
| 196 | ? ($1, $info->{table_collation}) | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 66 | ▶ | : (DBIO::MySQL::DDL::default_charset(), DBIO::MySQL::DDL::default_collation()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"table_collation"} and $info->{"table_collation"} =~ /^([^_]+)_/
MC/DC50%1/2
$info->{"table_collation"} and $info->{"table_collation"} =~ /^([^_]+)_/
$info->{"table_collation"}
$info->{"table_collation"} =~ /^([^_]+)_/
Truth table66%2/3
A and B
A= $info->{"table_collation"}B= $info->{"table_collation"} =~ /^([^_]+)_/
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 66 | return sprintf "CREATE TABLE %s (\n%s\n) ENGINE=%s DEFAULT CHARSET=%s COLLATE=%s;", | |||||||||||||||||||||||||||||||||||||||||
| 200 | quote_table_in($self->database, $self->table_name), join(",\n", @col_defs), $engine, $charset, $collate; | ||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | if ($self->action eq 'convert') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | return sprintf '~ table collation: %s (%s -> %s)', $self->table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | $self->old_info->{table_collation}, $self->table_info->{table_collation}; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | return sprintf '%s table: %s', $prefix, $self->table_name; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 57 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||||||||||||||||||||||||