Informix/Deploy.pm

filter: Statementstmt 100% 84 / 84 Branchbran 67.8% 19 / 28 Conditioncond 43.7% 7 / 16 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 22 / 22 Podpod n/a totaltotal 84.1% 133 / 158 CC 23 SCAR 20.9
CC
23
Cov
86%
_refuse_unfillable
19.0
_admin_dbh
4.3
_split_dsn
3.0
CRAP 8.1
« Informix/Introspect/Columns.pm ↑ Summary Informix/Diff/PrimaryKey.pm »
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
BranchTrueFalseTotal
$dsn =~ /\A(dbi:Informix:)([^\@;=]+)(\@[^;=]+)?\z/i or die ref($self) . ': cannot derive a throwaway database from the DSN: ' . "expected dbi:Informix:database[\@server]\n" 6 1 7
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 // ""
l!l
60
MC/DC0%0/1
$3 // ""
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
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
BranchTrueFalseTotal
if ref $dsn eq "CODE" 0 2 2
49 if ref $dsn eq 'CODE';
50 2 my ($prefix, undef, $server) = $self->_split_dsn($dsn);
51 2 ▶ my $dbh = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = eval { do { "DBI"->connect("$prefix$server", $user, $pass, {"RaiseError", 1, "PrintError", 0, "AutoCommit", 1}) } } or die DBIO::Util::mask_connect_secrets($@ || "Cannot connect to the server: $DBI::errstr", [$dsn, $user, $pass]) 2 0 2
52 2 ▶ DBI->connect("$prefix$server", $user, $pass, {
Condition0%0/2
$@ || "Cannot connect to the server: $DBI::errstr"
l!l
00
MC/DC0%0/1
$@ || "Cannot connect to the server: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
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
BranchTrueFalseTotal
if $diff->has_changes 6 0 6
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 // ""
l!l
12
MC/DC100%1/1
$where // ""
$where
Truth table100%2/2
A
A= $where
Aresult
11
00
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
BranchTrueFalseTotal
unless $op->isa("DBIO::Informix::Diff::Column") 2 3 5
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 // {}
l!l
30
MC/DC0%0/1
$op->column_info // {}
$op->column_info
Truth table50%1/2
A
A= $op->column_info
Aresult
11
00
107  
108 3 ▶ if ($op->action eq 'add') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->action eq 'add') { } 2 1 3
elsif ($op->action eq 'alter') { } 1 0 1
109 2 ▶ next unless $new->{not_null} && !$new->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"not_null"} and not $new->{"is_auto_increment"} 0 2 2
Condition33%1/3
$new->{"not_null"} and not $new->{"is_auto_increment"}
!ll&&!rl&&r
002
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"}
ABresult
0X0
100
111
110 2 ▶ next if defined $new->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $new->{"default_value"} 0 2 2
111 2 ▶ next unless $has_rows->($table);
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table) 1 1 2
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
BranchTrueFalseTotal
unless $new->{"not_null"} and not +($op->from_info // {})->{"not_null"} 0 1 1
Condition40%2/5
$new->{"not_null"} and not +($op->from_info // {})->{"not_null"}
!ll&&!rl&&r
001
$op->from_info // {}
l!l
10
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
ABresult
0X0
110
101
118 1 ▶ next unless $has_rows->($table, ' WHERE ' . $q->($column) . ' IS NULL');
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table, ' WHERE ' . &$q($column) . ' IS NULL') 0 1 1
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
BranchTrueFalseTotal
if @found 2 2 4
@found == 1 ? : 2 0 2
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__
« Informix/Introspect/Columns.pm ↑ Summary Informix/Diff/PrimaryKey.pm »