| 1 | package DBIO::ClickHouse::Deploy; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Deploy and upgrade ClickHouse schemas via test-deploy-and-compare | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 32 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 32 | ▶ | use base 'DBIO::Deploy::Base::TempDatabase'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 32 | ▶ | use DBIO::ClickHouse::DDL (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 32 | ▶ | use DBIO::ClickHouse::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 32 | ▶ | use DBIO::ClickHouse::Diff (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 32 | ▶ | use DBIO::ClickHouse::DDL::Emit qw(create_database drop_database); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | sub _ddl_class { 'DBIO::ClickHouse::DDL' } | ||||||||||||||||||||||||||
|
Subroutine
_ddl_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _introspect_class { 'DBIO::ClickHouse::Introspect' } | ||||||||||||||||||||||||||
|
Subroutine
_introspect_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub _diff_class { 'DBIO::ClickHouse::Diff' } | ||||||||||||||||||||||||||
|
Subroutine
_diff_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub temp_db_prefix { $_[0]->{temp_db_prefix} // 'dbio_deploy_tmp_' } | ||||||||||||||||||||||||||
|
Subroutine
temp_db_prefix: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$_[0]{"temp_db_prefix"} // "dbio_deploy_tmp_"
MC/DC0%0/1
$_[0]{"temp_db_prefix"} // "dbio_deploy_tmp_"
$_[0]{"temp_db_prefix"}
Truth table0%0/2
A
A= $_[0]{"temp_db_prefix"}
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _create_temp_db { | ||||||||||||||||||||||||||
|
Subroutine
_create_temp_db: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 0 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||
| 27 | 0 | my $name = $self->temp_db_prefix . $$ . '_' . time(); | |||||||||||||||||||||||||||
| 28 | 0 | $dbh->do(create_database(name => $name)); | |||||||||||||||||||||||||||
| 29 | 0 | return $name; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _drop_temp_db { | ||||||||||||||||||||||||||
|
Subroutine
_drop_temp_db: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | 0 | my ($self, $dbh, $name) = @_; | |||||||||||||||||||||||||||
| 35 | 0 | $dbh->do(drop_database(name => $name, if_exists => 1)); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # The databases of the qualified tables, asked of the server before the first | ||||||||||||||||||||||||||||
| 40 | # statement; the driver creates none. | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | sub _refuse_missing_databases { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_missing_databases: called CC=3 SCAR=17.3
| |||||||||||||||||||||||||||||
| 42 | 2 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||
| 43 | 2 | for my $pair ($self->_ddl_class->qualified_tables($self->schema)) { | |||||||||||||||||||||||||||
| 44 | 0 | my ($table, $database) = @$pair; | |||||||||||||||||||||||||||
| 45 | 0 | my ($found) = $dbh->selectrow_array( | |||||||||||||||||||||||||||
| 46 | 'SELECT count() FROM system.databases WHERE name = ?', undef, $database); | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | next if $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $self->schema->storage->throw_exception(sprintf | |||||||||||||||||||||||||||
| 49 | 'DBIO::ClickHouse refuses to create the table or view %s: the database %s does not ' | ||||||||||||||||||||||||||||
| 50 | . 'exist, and the driver does not create databases', $table, $database); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 2 | return; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # What the install writes into the live database is the whole schema; the | ||||||||||||||||||||||||||||
| 56 | # scratch database of a diff gets the tables of the current database only. | ||||||||||||||||||||||||||||
| 57 | # The refusal of a missing database is raised here, where the install asks for | ||||||||||||||||||||||||||||
| 58 | # its statements: the base install executes nothing before it has them. | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub _install_ddl { | ||||||||||||||||||||||||||
|
Subroutine
_install_ddl: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 60 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | $self->_refuse_missing_databases($self->_dbh) unless $self->{_local_only}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | return $self->_ddl_class->install_ddl($self->schema, | ||||||||||||||||||||||||||||
| 63 | 2 | ▶ | $self->{_local_only} ? { local_only => 1 } : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub _deploy_and_introspect_temp { | ||||||||||||||||||||||||||
|
Subroutine
_deploy_and_introspect_temp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 0 | local $self->{_local_only} = 1; | |||||||||||||||||||||||||||
| 69 | 0 | return $self->SUPER::_deploy_and_introspect_temp(@_); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 75 | 1 | my $source_model = $self->_introspect_current; | |||||||||||||||||||||||||||
| 76 | 1 | my $target_model = $self->_build_target_model; | |||||||||||||||||||||||||||
| 77 | 1 | return $self->_diff_class->new( | |||||||||||||||||||||||||||
| 78 | source => $source_model, | ||||||||||||||||||||||||||||
| 79 | target => $target_model, | ||||||||||||||||||||||||||||
| 80 | storage => $self->schema->storage, | ||||||||||||||||||||||||||||
| 81 | declared_settings => $self->_ddl_class->declared_settings($self->schema), | ||||||||||||||||||||||||||||
| 82 | declared_views => $self->_ddl_class->declared_views($self->schema, { local_only => 1 }), | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 10 | ▶ | sub _check_before_apply { | ||||||||||||||||||||||||||
|
Subroutine
_check_before_apply: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 10 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||
| 89 | 10 | $self->_refuse_nulls_in_narrowed_columns($diff); | |||||||||||||||||||||||||||
| 90 | 9 | $self->_refuse_not_null_added_over_rows($diff); | |||||||||||||||||||||||||||
| 91 | 8 | return $self->SUPER::_check_before_apply($diff); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # The dry-run paths never get here; this runs on the live handle, before the | ||||||||||||||||||||||||||||
| 96 | # first statement of the plan, and throws before anything has been executed. | ||||||||||||||||||||||||||||
| 97 | 10 | ▶ | sub _refuse_nulls_in_narrowed_columns { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_nulls_in_narrowed_columns: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 98 | 10 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||
| 99 | 10 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||
| 100 | 10 | my @found; | |||||||||||||||||||||||||||
| 101 | 10 | for my $op (@{ $diff->operations }) { | |||||||||||||||||||||||||||
| 102 | 10 | ▶ | next unless $op->can('needs_null_check') && $op->needs_null_check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$op->can("needs_null_check") and $op->needs_null_check
MC/DC50%1/2
$op->can("needs_null_check") and $op->needs_null_check
$op->can("needs_null_check")
$op->needs_null_check
Truth table66%2/3
A and B
A= $op->can("needs_null_check")B= $op->needs_null_check
| |||||||||||||||||||||||||||||
| 103 | 2 | my ($nulls) = $dbh->selectrow_array($op->null_check_sql); | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | push @found, [ $op->table_name, $op->column_name, $nulls ] if $nulls; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 10 | ▶ | return unless @found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $msg = join "\n", map { | ||||||||||||||||||||||||||||
| 109 | 1 | my ($t, $c, $n) = @$_; | |||||||||||||||||||||||||||
| 110 | 1 | "column $t.$c is Nullable and holds $n NULL row(s), but the schema makes it NOT NULL; " | |||||||||||||||||||||||||||
| 111 | . "MODIFY COLUMN would leave it unreadable and block the table's mutations. " | ||||||||||||||||||||||||||||
| 112 | . "Fill them by hand: ALTER TABLE `$t` UPDATE `$c` = <value> WHERE `$c` IS NULL " | ||||||||||||||||||||||||||||
| 113 | . "SETTINGS mutations_sync = 1" | ||||||||||||||||||||||||||||
| 114 | } @found; | ||||||||||||||||||||||||||||
| 115 | 1 | $self->schema->storage->throw_exception( | |||||||||||||||||||||||||||
| 116 | "upgrade refused, no statement was executed:\n$msg\nthen run the upgrade again" | ||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 9 | ▶ | sub _refuse_not_null_added_over_rows { | ||||||||||||||||||||||||||
|
Subroutine
_refuse_not_null_added_over_rows: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 122 | 9 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||
| 123 | 9 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||
| 124 | 9 | my @found; | |||||||||||||||||||||||||||
| 125 | 9 | for my $op (@{ $diff->operations }) { | |||||||||||||||||||||||||||
| 126 | 9 | ▶ | next unless $op->can('needs_row_check') && $op->needs_row_check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$op->can("needs_row_check") and $op->needs_row_check
MC/DC50%1/2
$op->can("needs_row_check") and $op->needs_row_check
$op->can("needs_row_check")
$op->needs_row_check
Truth table66%2/3
A and B
A= $op->can("needs_row_check")B= $op->needs_row_check
| |||||||||||||||||||||||||||||
| 127 | 2 | my ($has) = $dbh->selectrow_array($op->row_check_sql); | |||||||||||||||||||||||||||
| 128 | 2 | ▶ | push @found, [ $op->table_name, $op->column_name ] if $has; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 9 | ▶ | return unless @found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | my $msg = join "\n", map { | ||||||||||||||||||||||||||||
| 133 | 1 | my ($t, $c) = @$_; | |||||||||||||||||||||||||||
| 134 | 1 | "column $t.$c would be added NOT NULL without a default to a table that holds rows; " | |||||||||||||||||||||||||||
| 135 | . "ClickHouse fills existing rows with the type's zero value, which nobody wrote " | ||||||||||||||||||||||||||||
| 136 | . "(https://clickhouse.com/docs/sql-reference/statements/alter/column#add-column). " | ||||||||||||||||||||||||||||
| 137 | . "Give the column a default_value (or make it nullable) and run the upgrade again" | ||||||||||||||||||||||||||||
| 138 | } @found; | ||||||||||||||||||||||||||||
| 139 | 1 | $self->schema->storage->throw_exception( | |||||||||||||||||||||||||||
| 140 | "upgrade refused, no statement was executed:\n$msg" | ||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 32 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||