| 1 | package DBIO::ClickHouse::Diff::View; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for ClickHouse views | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 38 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 38 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 38 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 38 | ▶ | use DBIO::Diff::Op (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 38 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 38 | ▶ | use DBIO::ClickHouse::DDL (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 38 | ▶ | use DBIO::ClickHouse::DDL::Emit qw(create_view drop_view); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 38 | __PACKAGE__->mk_diff_accessors(qw/view_name definition storage/); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 102 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=18 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 102 | my ($class, $source_tables, $declared_views, $storage, $target_tables) = @_; | |||||||||||||||||||||||||||
| 21 | 102 | ▶ | return () unless ref $declared_views eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 19 | ▶ | $source_tables ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables ||= {}
MC/DC0%0/1
$source_tables ||= {}
$source_tables
Truth table50%1/2
A
A= $source_tables
| |||||||||||||||||||||||||||||
| 23 | 19 | ▶ | $target_tables ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target_tables ||= {}
MC/DC0%0/1
$target_tables ||= {}
$target_tables
Truth table50%1/2
A
A= $target_tables
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 19 | ▶ | my @live_views = grep { (($source_tables->{$_} || {})->{kind} // '') eq 'view' } | ||||||||||||||||||||||||||
|
Condition50%2/4
+($source_tables->{$_} || {})->{"kind"} // ""
$source_tables->{$_} || {}
MC/DC0%0/1
+($source_tables->{$_} || {})->{"kind"} // ""
$source_tables->{$_}
Truth table50%1/2
A
A= $source_tables->{$_}
| |||||||||||||||||||||||||||||
| 26 | sort keys %$source_tables; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # data first: a refusal is thrown before an operation exists | ||||||||||||||||||||||||||||
| 29 | 19 | ▶ | my @held = grep { ($source_tables->{$_}{engine} // '') =~ /\A(?:Materialized|Live|Window)View\z/ } | ||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables->{$_}{"engine"} // ""
MC/DC0%0/1
$source_tables->{$_}{"engine"} // ""
$source_tables->{$_}{"engine"}
Truth table50%1/2
A
A= $source_tables->{$_}{"engine"}
| |||||||||||||||||||||||||||||
| 30 | @live_views; | ||||||||||||||||||||||||||||
| 31 | 19 | ▶ | if (@held) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | DBIO::Exception->throw(join "\n", | ||||||||||||||||||||||||||||
| 33 | 'upgrade refused, no statement was executed:', | ||||||||||||||||||||||||||||
| 34 | (map { | ||||||||||||||||||||||||||||
| 35 | 3 | my $engine = $source_tables->{$_}{engine}; | |||||||||||||||||||||||||||
| 36 | 3 | ▶ | exists $declared_views->{$_} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | ? "view `$_` is a $engine and holds data, but the schema declares a plain view of that name; " | ||||||||||||||||||||||||||||
| 38 | . "the upgrade will not replace it" | ||||||||||||||||||||||||||||
| 39 | : "view `$_` is a $engine and holds data, but the schema does not declare it; " | ||||||||||||||||||||||||||||
| 40 | . "the upgrade will not drop it" | ||||||||||||||||||||||||||||
| 41 | } @held), | ||||||||||||||||||||||||||||
| 42 | 'Handle it by hand (DROP VIEW ... after saving what it holds), or declare it, then run the upgrade again', | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my %held = map { $_ => 1 } @held; | |||||||||||||||||||||||||||
| 47 | 16 | my @ops; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # A reader goes before what it reads: the reverse of the order they are | ||||||||||||||||||||||||||||
| 50 | # written in, read from the live definitions (karr k43). | ||||||||||||||||||||||||||||
| 51 | 5 | ▶ | my %dropped = map { $_ => $source_tables->{$_}{definition} // '' } | ||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables->{$_}{"definition"} // ""
MC/DC0%0/1
$source_tables->{$_}{"definition"} // ""
$source_tables->{$_}{"definition"}
Truth table50%1/2
A
A= $source_tables->{$_}{"definition"}
| |||||||||||||||||||||||||||||
| 52 | 16 | grep { !exists $declared_views->{$_} } @live_views; | |||||||||||||||||||||||||||
| 53 | push @ops, map { | ||||||||||||||||||||||||||||
| 54 | 16 | $class->new(action => 'drop', view_name => $_, storage => $storage) | |||||||||||||||||||||||||||
| 55 | } reverse DBIO::ClickHouse::DDL->view_order(\%dropped); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Written after the views they read (a cycle ends the diff here, before an | ||||||||||||||||||||||||||||
| 58 | # operation is rendered). | ||||||||||||||||||||||||||||
| 59 | 16 | for my $name (DBIO::ClickHouse::DDL->view_order($declared_views)) { | |||||||||||||||||||||||||||
| 60 | 15 | my $live = $source_tables->{$name}; | |||||||||||||||||||||||||||
| 61 | 15 | ▶ | if (!$live) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 63 | action => 'create', view_name => $name, | ||||||||||||||||||||||||||||
| 64 | 5 | definition => $declared_views->{$name}, storage => $storage, | |||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | 5 | next; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 10 | ▶ | next unless ($live->{kind} // '') eq 'view'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$live->{"kind"} // ""
MC/DC0%0/1
$live->{"kind"} // ""
$live->{"kind"}
Truth table50%1/2
A
A= $live->{"kind"}
| |||||||||||||||||||||||||||||
| 69 | 10 | ▶ | next unless defined $live->{definition}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | ▶ | my $target = $target_tables->{$name} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_tables->{$name} || {}
MC/DC100%1/1
$target_tables->{$name} || {}
$target_tables->{$name}
Truth table100%2/2
A
A= $target_tables->{$name}
| |||||||||||||||||||||||||||||
| 72 | my $want = defined $target->{definition} | ||||||||||||||||||||||||||||
| 73 | ? _normalise($target->{definition}, $target->{database}) | ||||||||||||||||||||||||||||
| 74 | 8 | ▶ | : _normalise($declared_views->{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | next if _normalise($live->{definition}, $live->{database}) eq $want; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | push @ops, $class->new( | ||||||||||||||||||||||||||||
| 78 | action => 'alter', view_name => $name, | ||||||||||||||||||||||||||||
| 79 | 6 | definition => $declared_views->{$name}, storage => $storage, | |||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 15 | return @ops; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # The text the server holds, without what differs between two databases that | ||||||||||||||||||||||||||||
| 87 | # hold the same view: the database in front of a name, the quoting marks, the | ||||||||||||||||||||||||||||
| 88 | # white space. | ||||||||||||||||||||||||||||
| 89 | 16 | ▶ | sub _normalise { | ||||||||||||||||||||||||||
|
Subroutine
_normalise: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 90 | 16 | my ($sql, $database) = @_; | |||||||||||||||||||||||||||
| 91 | 16 | $sql =~ s/`//g; | |||||||||||||||||||||||||||
| 92 | 16 | ▶ | $sql =~ s/(?<![\w.])\Q$database\E\.(?=\w)//g if defined $database && length $database; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $database and length $database
MC/DC50%1/2
defined $database and length $database
defined $database
length $database
Truth table66%2/3
A and B
A= defined $databaseB= length $database
| |||||||||||||||||||||||||||||
| 93 | 16 | $sql =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 94 | 16 | $sql =~ s/\A\s+//; | |||||||||||||||||||||||||||
| 95 | 16 | $sql =~ s/\s*;?\s*\z//; | |||||||||||||||||||||||||||
| 96 | 16 | return $sql; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 15 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||
| 102 | 15 | ▶ | my $guard = DBIO::Diff::Op::should_emit_if_exists($self->storage) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 15 | ▶ | return drop_view(name => $self->view_name, if_exists => $guard) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||
| 105 | 10 | ▶ | return create_view( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | name => $self->view_name, | ||||||||||||||||||||||||||||
| 107 | definition => $self->definition, | ||||||||||||||||||||||||||||
| 108 | or_replace => ($self->action eq 'alter' ? 1 : 0), | ||||||||||||||||||||||||||||
| 109 | if_not_exists => $guard, | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 116 | 3 | return sprintf '%s view: %s', $self->summary_prefix, $self->view_name; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 38 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||