| 1 | package DBIO::Informix::Deploy; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Deploy and upgrade Informix schemas via test-deploy-and-compare | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 15 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use base 'DBIO::Deploy::Base::TempDatabase'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use DBI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use DBIO::Util (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use DBIO::Informix::DDL; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use DBIO::Informix::Introspect; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 15 | ▶ | use DBIO::Informix::Diff; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 15 | ▶ | use DBIO::Informix::Util (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | # --- class-name hooks for DBIO::Deploy::Base ------------------------------- | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub _ddl_class { 'DBIO::Informix::DDL' } | ||||||||||||||||||||||||||||||
|
Subroutine
_ddl_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub _introspect_class { 'DBIO::Informix::Introspect' } | ||||||||||||||||||||||||||||||
|
Subroutine
_introspect_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub _diff_class { 'DBIO::Informix::Diff' } | ||||||||||||||||||||||||||||||
|
Subroutine
_diff_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # --- Informix-specific throwaway-database dialect -------------------------- | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | # dbi:Informix:database[@server] -> (prefix, database, '@server' or '') | ||||||||||||||||||||||||||||||||
| 29 | 7 | ▶ | sub _split_dsn { | ||||||||||||||||||||||||||||||
|
Subroutine
_split_dsn: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||
| 30 | 7 | my ($self, $dsn) = @_; | |||||||||||||||||||||||||||||||
| 31 | 7 | ▶ | $dsn =~ /\A(dbi:Informix:)([^@;=]+)(\@[^;=]+)?\z/i | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | or die ref($self) . ": cannot derive a throwaway database from the DSN: " | ||||||||||||||||||||||||||||||||
| 33 | . "expected dbi:Informix:database[\@server]\n"; | ||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | return ($1, $2, $3 // ''); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$3 // ""
MC/DC0%0/1
$3 // ""
$3
Truth table50%1/2
A
A= $3
| |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | sub _temp_dsn { | ||||||||||||||||||||||||||||||
|
Subroutine
_temp_dsn: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | 5 | my ($self, $dsn, $temp_db) = @_; | |||||||||||||||||||||||||||||||
| 40 | 5 | my ($prefix, undef, $server) = $self->_split_dsn($dsn); | |||||||||||||||||||||||||||||||
| 41 | 4 | return "$prefix$temp_db$server"; | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | sub _admin_dbh { | ||||||||||||||||||||||||||||||
|
Subroutine
_admin_dbh: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||
| 46 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 47 | 2 | my ($dsn, $user, $pass) = @{ $self->schema->storage->_dbi_connect_info }; | |||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | die ref($self) . " does not support a coderef DSN for a throwaway database\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | if ref $dsn eq 'CODE'; | ||||||||||||||||||||||||||||||||
| 50 | 2 | my ($prefix, undef, $server) = $self->_split_dsn($dsn); | |||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | my $dbh = eval { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | DBI->connect("$prefix$server", $user, $pass, { | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || "Cannot connect to the server: $DBI::errstr"
MC/DC0%0/1
$@ || "Cannot connect to the server: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||
| 53 | RaiseError => 1, PrintError => 0, AutoCommit => 1, | ||||||||||||||||||||||||||||||||
| 54 | }) | ||||||||||||||||||||||||||||||||
| 55 | } or die DBIO::Util::mask_connect_secrets( | ||||||||||||||||||||||||||||||||
| 56 | $@ || "Cannot connect to the server: $DBI::errstr", [ $dsn, $user, $pass ], | ||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||
| 58 | 2 | $self->{_admin_dbh} = $dbh; | |||||||||||||||||||||||||||||||
| 59 | 2 | return $dbh; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub _create_temp_db { | ||||||||||||||||||||||||||||||
|
Subroutine
_create_temp_db: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 64 | 2 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 65 | 2 | my $name = $self->temp_db_prefix . $$ . '_' . time(); | |||||||||||||||||||||||||||||||
| 66 | 2 | $dbh->do("CREATE DATABASE $name WITH LOG"); | |||||||||||||||||||||||||||||||
| 67 | 2 | return $name; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub _drop_temp_db { | ||||||||||||||||||||||||||||||
|
Subroutine
_drop_temp_db: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 72 | 2 | my ($self, $dbh, $name) = @_; | |||||||||||||||||||||||||||||||
| 73 | 2 | $dbh->do('CLOSE DATABASE'); | |||||||||||||||||||||||||||||||
| 74 | 2 | $dbh->do("DROP DATABASE $name"); | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub _deploy_and_introspect_temp { | ||||||||||||||||||||||||||||||
|
Subroutine
_deploy_and_introspect_temp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 79 | 2 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||
| 80 | 2 | local $ENV{DELIMIDENT} = 'y'; | |||||||||||||||||||||||||||||||
| 81 | 2 | return $self->next::method(@args); | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | sub _check_before_apply { | ||||||||||||||||||||||||||||||
|
Subroutine
_check_before_apply: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 86 | 6 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | $self->_refuse_unfillable($diff, $self->_dbh) if $diff->has_changes; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 4 | return $self->next::method($diff); | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub _refuse_unfillable { | ||||||||||||||||||||||||||||||
|
Subroutine
_refuse_unfillable: called CC=16 SCAR=29.4
| |||||||||||||||||||||||||||||||||
| 93 | 4 | my ($self, $diff, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 4 | my $q = \&DBIO::Informix::Util::_quote_ident; | |||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | my $has_rows = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 97 | 3 | my ($table, $where) = @_; | |||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | return scalar $dbh->selectrow_array(sprintf 'SELECT FIRST 1 1 FROM %s%s', | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$where // ""
MC/DC100%1/1
$where // ""
$where
Truth table100%2/2
A
A= $where
| |||||||||||||||||||||||||||||||||
| 99 | $q->($table), $where // ''); | ||||||||||||||||||||||||||||||||
| 100 | 4 | }; | |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 4 | my @found; | |||||||||||||||||||||||||||||||
| 103 | 4 | for my $op (@{ $diff->operations }) { | |||||||||||||||||||||||||||||||
| 104 | 5 | ▶ | next unless $op->isa('DBIO::Informix::Diff::Column'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 3 | my ($table, $column) = ($op->table_name, $op->column_info->{column_name}); | |||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | my $new = $op->column_info // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$op->column_info // {}
MC/DC0%0/1
$op->column_info // {}
$op->column_info
Truth table50%1/2
A
A= $op->column_info
| |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 3 | ▶ | if ($op->action eq 'add') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | next unless $new->{not_null} && !$new->{is_auto_increment}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$new->{"not_null"} and not $new->{"is_auto_increment"}
MC/DC0%0/2
$new->{"not_null"} and not $new->{"is_auto_increment"}
$new->{"not_null"}
not $new->{"is_auto_increment"}
Truth table33%1/3
A and not(B)
A= $new->{"not_null"}B= not $new->{"is_auto_increment"}
| |||||||||||||||||||||||||||||||||
| 110 | 2 | ▶ | next if defined $new->{default_value}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | next unless $has_rows->($table); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 1 | push @found, sprintf | |||||||||||||||||||||||||||||||
| 113 | ' %s.%s: added NOT NULL without a default, and the table holds rows', | ||||||||||||||||||||||||||||||||
| 114 | $q->($table), $q->($column); | ||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | elsif ($op->action eq 'alter') { | ||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | next unless $new->{not_null} && !($op->from_info // {})->{not_null}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
$new->{"not_null"} and not +($op->from_info // {})->{"not_null"}
$op->from_info // {}
MC/DC0%0/2
$new->{"not_null"} and not +($op->from_info // {})->{"not_null"}
$new->{"not_null"}
$op->from_info
Truth table33%1/3
A and not(B)
A= $new->{"not_null"}B= $op->from_info
| |||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | next unless $has_rows->($table, ' WHERE ' . $q->($column) . ' IS NULL'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 1 | push @found, sprintf | |||||||||||||||||||||||||||||||
| 120 | ' %s.%s: becomes NOT NULL, and the table holds rows where it is NULL', | ||||||||||||||||||||||||||||||||
| 121 | $q->($table), $q->($column); | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 4 | ▶ | DBIO::Exception->throw(join "\n", | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 126 | sprintf('refusing to apply the diff: the Informix server would refuse %s half way, with its own error', | ||||||||||||||||||||||||||||||||
| 127 | @found == 1 ? 'this change' : 'these changes'), | ||||||||||||||||||||||||||||||||
| 128 | '', | ||||||||||||||||||||||||||||||||
| 129 | @found, | ||||||||||||||||||||||||||||||||
| 130 | '', | ||||||||||||||||||||||||||||||||
| 131 | 'No statement was run. Fill the column (or give it a default) first, or empty the table.', | ||||||||||||||||||||||||||||||||
| 132 | '', | ||||||||||||||||||||||||||||||||
| 133 | ) if @found; | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 2 | return; | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 15 | 1; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||||||