| 1 | package DBIO::ClickHouse::Diff; | ||||||||||||||||
| 2 | # ABSTRACT: Compare two introspected ClickHouse models | ||||||||||||||||
| 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::Base'; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 10 | |||||||||||||||||
| 11 | 38 | ▶ | use DBIO::ClickHouse::Diff::Table; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 12 | 38 | ▶ | use DBIO::ClickHouse::Diff::Column; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 13 | 38 | ▶ | use DBIO::ClickHouse::Diff::Index; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 14 | 38 | ▶ | use DBIO::ClickHouse::Diff::View; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 15 | |||||||||||||||||
| 16 | |||||||||||||||||
| 17 | 102 | ▶ | sub _build_operations { | ||||||||||||||
|
Subroutine
_build_operations: called CC=1 SCAR=0.0
| |||||||||||||||||
| 18 | 102 | my ($self) = @_; | |||||||||||||||
| 19 | |||||||||||||||||
| 20 | 102 | my $source_tables = _diffable_tables($self->source->{tables}); | |||||||||||||||
| 21 | 102 | my $target_tables = _diffable_tables($self->target->{tables}); | |||||||||||||||
| 22 | |||||||||||||||||
| 23 | # The storage is optional; when given it gates the F12 capability check | ||||||||||||||||
| 24 | # that decides whether diff ops emit IF [NOT] EXISTS guards in their as_sql. | ||||||||||||||||
| 25 | # Without it (or with one that doesn't declare the capability) the rendered | ||||||||||||||||
| 26 | # SQL stays bare -- the legacy shape t/40 and t/41 already pin. | ||||||||||||||||
| 27 | 102 | my $storage = $self->{storage}; | |||||||||||||||
| 28 | |||||||||||||||||
| 29 | 102 | my @ops; | |||||||||||||||
| 30 | |||||||||||||||||
| 31 | # A view is dropped before the tables go: it may select from one of them. | ||||||||||||||||
| 32 | # The views to create or replace come last, below. | ||||||||||||||||
| 33 | my @view_ops = DBIO::ClickHouse::Diff::View->diff( | ||||||||||||||||
| 34 | $self->source->{tables}, $self->{declared_views}, $storage, | ||||||||||||||||
| 35 | $self->target->{tables}, | ||||||||||||||||
| 36 | 102 | ); | |||||||||||||||
| 37 | 98 | push @ops, grep { $_->action eq 'drop' } @view_ops; | |||||||||||||||
| 38 | |||||||||||||||||
| 39 | push @ops, DBIO::ClickHouse::Diff::Table->diff( | ||||||||||||||||
| 40 | $source_tables, $target_tables, | ||||||||||||||||
| 41 | $self->target->{columns}, $self->target->{data_skipping_indexes}, | ||||||||||||||||
| 42 | $storage, $self->{declared_settings}, | ||||||||||||||||
| 43 | 98 | ); | |||||||||||||||
| 44 | # A skipping index is dropped before the column walk: the server refuses to | ||||||||||||||||
| 45 | # drop (or retype) a column an index still reads. An index added or | ||||||||||||||||
| 46 | # redefined follows the columns, because it may read a column added above. | ||||||||||||||||
| 47 | my @index_ops = DBIO::ClickHouse::Diff::Index->diff( | ||||||||||||||||
| 48 | $self->source->{data_skipping_indexes}, $self->target->{data_skipping_indexes}, | ||||||||||||||||
| 49 | 89 | $source_tables, $target_tables, | |||||||||||||||
| 50 | $storage, | ||||||||||||||||
| 51 | ); | ||||||||||||||||
| 52 | 89 | push @ops, grep { $_->action eq 'drop' } @index_ops; | |||||||||||||||
| 53 | push @ops, DBIO::ClickHouse::Diff::Column->diff( | ||||||||||||||||
| 54 | $self->source->{columns}, $self->target->{columns}, | ||||||||||||||||
| 55 | 89 | $source_tables, $target_tables, | |||||||||||||||
| 56 | $storage, | ||||||||||||||||
| 57 | ); | ||||||||||||||||
| 58 | 89 | push @ops, grep { $_->action ne 'drop' } @index_ops; | |||||||||||||||
| 59 | # Last: a view may select from a table or column created above. | ||||||||||||||||
| 60 | 89 | push @ops, grep { $_->action ne 'drop' } @view_ops; | |||||||||||||||
| 61 | |||||||||||||||||
| 62 | 89 | return \@ops; | |||||||||||||||
| 63 | } | ||||||||||||||||
| 64 | |||||||||||||||||
| 65 | # Views have operations of their own (Diff::View); see "Views follow the | ||||||||||||||||
| 66 | # schema's declaration" above. Filtering them here rather than in each op | ||||||||||||||||
| 67 | # class means every walk -- tables, columns, indexes -- agrees on which | ||||||||||||||||
| 68 | # sources are in scope, because they are all scoped by these two hashrefs. | ||||||||||||||||
| 69 | 204 | ▶ | sub _diffable_tables { | ||||||||||||||
|
Subroutine
_diffable_tables: called CC=4 SCAR=14.6
| |||||||||||||||||
| 70 | 204 | my ($tables) = @_; | |||||||||||||||
| 71 | 204 | ▶ | return {} unless ref $tables eq 'HASH'; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 72 | return { | ||||||||||||||||
| 73 | 194 | map { $_ => $tables->{$_} } | |||||||||||||||
| 74 | 204 | ▶ | grep { (($tables->{$_} || {})->{kind} // 'table') ne 'view' } | ||||||||||||||
|
Condition50%2/4
+($tables->{$_} || {})->{"kind"} // "table"
$tables->{$_} || {}
MC/DC0%0/1
+($tables->{$_} || {})->{"kind"} // "table"
$tables->{$_}
Truth table50%1/2
A
A= $tables->{$_}
| |||||||||||||||||
| 75 | keys %$tables | ||||||||||||||||
| 76 | }; | ||||||||||||||||
| 77 | } | ||||||||||||||||
| 78 | |||||||||||||||||
| 79 | |||||||||||||||||
| 80 | 38 | 1; | |||||||||||||||
| 81 | |||||||||||||||||
| 82 | __END__ | ||||||||||||||||