| 1 | package DBIO::MySQL::Diff::View; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MySQL/MariaDB views | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 58 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 58 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 58 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 58 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 58 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 58 | ▶ | use DBIO::MySQL::Identifier qw(quote_ident); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 58 | ▶ | use DBIO::MySQL::DDL (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 58 | __PACKAGE__->mk_diff_accessors(qw/view_name view_info replaced/); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2502 | ▶ | sub is_view { return (($_[0] // {})->{kind} // '') eq 'view' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_view: called CC=3 SCAR=11.0
Pod
documented
Condition100%4/4
+($_[0] // {})->{"kind"} // ""
$_[0] // {}
MC/DC100%1/1
+($_[0] // {})->{"kind"} // ""
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1442 | ▶ | sub without_views { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
without_views: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1442 | my ($tables, @sections) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 1442 | ▶ | $tables //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tables //= {}
MC/DC0%0/1
$tables //= {}
$tables
Truth table50%1/2
A
A= $tables
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1442 | my %view = map { $_ => 1 } grep { is_view($tables->{$_}) } keys %$tables; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 1442 | ▶ | return ($tables, @sections) unless %view; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 30 | { map { $_ => $tables->{$_} } grep { !$view{$_} } keys %$tables }, | |||||||||||||||||||||||||||||||||||||||||
| 30 | 37 | ▶ | map { my $s = $_; ref $s eq 'HASH' ? { map { $_ => $s->{$_} } grep { !$view{$_} } keys %$s } : $s } @sections, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 338 | ▶ | sub model_without_views { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model_without_views: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 338 | my ($model) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | my ($tables, @sections) = without_views( | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 338 | $model->{tables}, map { $model->{$_} } qw(columns indexes foreign_keys)); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 338 | return { %$model, tables => $tables, | |||||||||||||||||||||||||||||||||||||||||
| 40 | columns => $sections[0], indexes => $sections[1], foreign_keys => $sections[2] }; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 52 | ▶ | sub _comparable { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comparable: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 52 | my ($def) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 52 | ▶ | return undef unless defined $def; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 52 | $def =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 52 | $def =~ s/\A //; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 52 | $def =~ s/ \z//; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 52 | return $def; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 172 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=25 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 172 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 172 | ▶ | $source //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 172 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 172 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 172 | my (%drop, %create, %replaced); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 172 | for my $name (sort grep { !$seen{$_}++ } keys %$source, keys %$target) { | |||||||||||||||||||||||||||||||||||||||||
| 61 | 279 | ▶ | my $old = is_view($source->{$name}) ? $source->{$name} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 279 | ▶ | my $new = is_view($target->{$name}) ? $target->{$name} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 279 | ▶ | next unless $old || $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$old or $new
MC/DC100%2/2
$old or $new
$old
$new
Truth table100%3/3
A or B
A= $oldB= $new
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 60 | ▶ | if ($old && $new) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 26 | my ($was, $will) = map { _comparable($_->{view_definition}) } $old, $new; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 26 | ▶ | next if !defined $was || !defined $will || $was eq $will; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not defined $was && defined $will or $was eq $will
defined $was && defined $will
MC/DC33%1/3
not defined $was && defined $will or $was eq $will
defined $was
defined $will
$was eq $will
Truth table50%2/4
not(A && B) or C
A= defined $wasB= defined $willC= $was eq $will
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 46 | ▶ | $drop{$name} = $old if $old; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 46 | ▶ | $create{$name} = $new if $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 46 | ▶ | $replaced{$name} = 1 if $old && $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # A view that reads a view being replaced is replaced with it (karr k113), | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # transitively, although its own definition is the same: the server lets the | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # view it reads be dropped and answers with 1356 until a view of that name | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # is back, and if the new one lost a column the reader reads, the reader is | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # broken without a word. Created again, the server checks it against the new | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # definition and a reader that no longer fits fails in the apply. An | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # unchanged view has the same reads in both models. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 14 | my %stay = map { $_ => $target->{$_} } grep { | |||||||||||||||||||||||||||||||||||||||||
| 83 | 172 | ▶ | is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_} | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
is_view($source->{$_}) and is_view($target->{$_})
MC/DC100%3/3
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
is_view($source->{$_})
is_view($target->{$_})
!$replaced{$_}
Truth table100%4/4
A and B && not(C)
A= is_view($source->{$_})B= is_view($target->{$_})C= !$replaced{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } keys %$target; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 172 | my %fold; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 172 | push @{ $fold{ lc $_ } }, $_ for keys %stay, keys %replaced; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 18 | ▶ | my $resolve = sub { # as order_views reads a name: exact, else the one name of that spelling | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 18 | my ($r) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 18 | ▶ | return $r if exists $stay{$r} || exists $replaced{$r}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $stay{$r} or exists $replaced{$r}
MC/DC100%2/2
exists $stay{$r} or exists $replaced{$r}
exists $stay{$r}
exists $replaced{$r}
Truth table100%3/3
A or B
A= exists $stay{$r}B= exists $replaced{$r}
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | ▶ | return @{ $fold{ lc $r } // [] } == 1 ? $fold{ lc $r }[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$fold{lc $r} // []
MC/DC0%0/1
$fold{lc $r} // []
$fold{lc $r}
Truth table50%1/2
A
A= $fold{lc $r}
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 172 | }; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 172 | my @grow = keys %replaced; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 172 | ▶ | while (defined(my $changed = shift @grow)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 20 | for my $name (sort keys %stay) { | |||||||||||||||||||||||||||||||||||||||||
| 95 | 18 | ▶ | next unless grep { ($resolve->($_) // '') eq $changed } | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
&$resolve($_) // ""
MC/DC100%1/1
&$resolve($_) // ""
&$resolve($_)
Truth table100%2/2
A
A= &$resolve($_)
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 17 | ▶ | DBIO::MySQL::DDL::view_reads($stay{$name}{view_definition}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | $drop{$name} = $source->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 8 | $create{$name} = $target->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 8 | $replaced{$name} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 8 | delete $stay{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 8 | push @grow, $name; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # the order is worked out for the whole set before an op is made: a cycle | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # throws and leaves nothing half done | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 172 | my @drop_order = reverse DBIO::MySQL::DDL::order_views({ map { $_ => $drop{$_}{view_definition} } keys %drop }); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 171 | my @create_order = DBIO::MySQL::DDL::order_views({ map { $_ => $create{$_}{view_definition} } keys %create }); | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 111 | (map { $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 112 | action => 'drop', | ||||||||||||||||||||||||||||||||||||||||||
| 113 | view_name => $_, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | view_info => $drop{$_}, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 34 | ▶ | replaced => ($replaced{$_} ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | ) } @drop_order), | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 170 | (map { $class->new( | |||||||||||||||||||||||||||||||||||||||||
| 118 | action => 'create', | ||||||||||||||||||||||||||||||||||||||||||
| 119 | view_name => $_, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 32 | view_info => $create{$_}, | |||||||||||||||||||||||||||||||||||||||||
| 121 | ) } @create_order), | ||||||||||||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 67 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 67 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 67 | ▶ | return sprintf 'DROP VIEW %s;', quote_ident($self->view_name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 32 | ▶ | my $def = ($self->view_info // {})->{view_definition}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->view_info // {}
MC/DC0%0/1
$self->view_info // {}
$self->view_info
Truth table50%1/2
A
A= $self->view_info
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 32 | ▶ | Carp::croak(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $def and length $def
MC/DC50%1/2
defined $def and length $def
defined $def
length $def
Truth table66%2/3
A and B
A= defined $defB= length $def
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 'Cannot create view %s: the target model holds no view definition for it, ' | ||||||||||||||||||||||||||||||||||||||||||
| 135 | . 'and a view cannot be written from its columns', $self->view_name) | ||||||||||||||||||||||||||||||||||||||||||
| 136 | unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 31 | return sprintf 'CREATE VIEW %s AS %s;', quote_ident($self->view_name), $def; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | return sprintf '%s view: %s', $self->summary_prefix, $self->view_name; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 58 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||||||||