Oracle/Deploy.pm

filter: Statementstmt 94.5% 275 / 291 Branchbran 80.5% 87 / 108 Conditioncond 52.3% 44 / 84 MC/DCmcdc 27.6% 13 / 47 Subroutinesub 94.7% 36 / 38 Podpod 100% 1 / 1 totaltotal 80.1% 456 / 569 CC 107 SCAR 31.3
CC
107
Cov
84%
_deploy_and_introspect_temp_schemas
61.1
_refuse_unfillable
24.1
_create_temp_db
10.3
CRAP 22.8
« Oracle/Diff.pm ↑ Summary Oracle/Storage/LOBSupport.pm »
1 package DBIO::Oracle::Deploy;
2 # ABSTRACT: Deploy, diff and upgrade a schema on Oracle, against a temporary user
3  
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 our $VERSION = '0.900007';
8  
9 21 ▶use base 'DBIO::Deploy::Base::TempDatabase';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 21 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use DBIO::Oracle::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use DBIO::Oracle::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use DBIO::Oracle::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use DBIO::Oracle::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 21 ▶use DBIO::Oracle::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21  
22 # --- class-name hooks for DBIO::Deploy::Base -------------------------------
23  
24 40 ▶sub _ddl_class { 'DBIO::Oracle::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
25 0 ▶sub _introspect_class { 'DBIO::Oracle::Introspect' }
Subroutine _introspect_class: not called CC=1 SCAR=6.9
26 1 ▶sub _diff_class { 'DBIO::Oracle::Diff' }
Subroutine _diff_class: called CC=1 SCAR=0.0
27  
28  
29 9 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=1 SCAR=0.0
30 9 my ($self) = @_;
31 9 $self->_require_schema_users;
32 8 return $self->SUPER::_install_ddl;
33 }
34  
35 9 ▶sub _require_schema_users {
Subroutine _require_schema_users: called CC=8 SCAR=21.8
36 9 my ($self) = @_;
37 9 ▶ my $objects = $self->_ddl_class->declared_objects($self->schema)->{schema} || {};
Condition100%2/2
$self->_ddl_class->declared_objects($self->schema)->{'schema'} || {}
l!l
27
MC/DC100%1/1
$self->_ddl_class->declared_objects($self->schema)->{'schema'} || {}
$self->_ddl_class->declared_objects($self->schema)->{'schema'}
Truth table100%2/2
A
A= $self->_ddl_class->declared_objects($self->schema)->{'schema'}
Aresult
11
00
38 9 my @wanted = sort keys %$objects;
39 9 ▶ return unless @wanted;
Branch: 1 branch
BranchTrueFalseTotal
unless @wanted 7 2 9
40  
41 2 my $dbh = $self->_dbh;
42 2 my %have = map { $_ => 1 } @{ $dbh->selectcol_arrayref(
43 'SELECT username FROM all_users WHERE username IN ('
44 . join(', ', ('?') x @wanted) . ')', undef, @wanted) };
45 2 my ($missing) = grep { !$have{$_} } @wanted;
46 2 ▶ return unless defined $missing;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $missing 1 1 2
47  
48 1 my $hint = '';
49 1 ▶ if ($missing ne uc $missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing ne uc $missing) 0 1 1
50 0 my ($upper) = $dbh->selectrow_array(
51 'SELECT username FROM all_users WHERE username = ?', undef, uc $missing);
52 0 ▶ $hint = " (a user '" . uc($missing) . "' exists: Oracle folds an unquoted name to upper case, "
Branch: 1 branch
BranchTrueFalseTotal
if defined $upper 0 0 0
53 . 'and the schema of a table name is taken exactly as spelled)' if defined $upper;
54 }
55 1 ▶ my @tables = map { "$missing.$_" } @{ $objects->{$missing}{table} || $objects->{$missing}{view} || [] };
Condition40%2/5
$objects->{$missing}{'table'} || $objects->{$missing}{'view'} || []
l!l
10
$objects->{$missing}{"table"} or $objects->{$missing}{"view"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$objects->{$missing}{'table'} || $objects->{$missing}{'view'} || []
$objects->{$missing}{"table"} $objects->{$missing}{"view"}
Truth table33%1/3
A or B
A= $objects->{$missing}{"table"}B= $objects->{$missing}{"view"}
ABresult
1X1
011
000
56 1 $self->schema->throw_exception(sprintf
57 "%s: the schema (user) '%s' does not exist on the server%s; the driver creates "
58 . "no user -- refused, nothing is installed",
59 join(', ', @tables), $missing, $hint);
60 }
61  
62  
63 5 ▶sub _check_before_apply {
Subroutine _check_before_apply: called CC=1 SCAR=0.0
64 5 my ($self, $diff) = @_;
65 5 $self->_refuse_unfillable($diff, $self->_dbh);
66 3 return $self->SUPER::_check_before_apply($diff);
67 }
68  
69 # What Oracle accepts when it changes the datatype of a column that holds
70 # values: the pairs (old base type => new base types) measured on Oracle
71 # Database 23ai Free (23.26.3) with one value in the column, karr k63. Any
72 # other change between the base types in %KNOWN_BASE is ORA-01439 (ORA-22858 and
73 # ORA-22859 for the LOB types); a type outside that list is left to the server.
74 21 my %KNOWN_BASE = map { $_ => 1 } (
75 'NUMBER', 'FLOAT', 'VARCHAR2', 'CHAR', 'NVARCHAR2', 'NCHAR', 'CLOB', 'DATE',
76 'TIMESTAMP', 'TIMESTAMP WITH TIME ZONE', 'BINARY_FLOAT', 'BINARY_DOUBLE',
77 'RAW', 'BLOB',
78 );
79 my %CONVERTS = (
80 63 'CHAR' => { map { $_ => 1 } 'VARCHAR2', 'NCHAR', 'NVARCHAR2' },
81 21 'VARCHAR2' => { map { $_ => 1 } 'CHAR', 'NCHAR', 'NVARCHAR2' },
82 'NCHAR' => { 'NVARCHAR2' => 1 },
83 'NVARCHAR2' => { 'NCHAR' => 1 },
84 'DATE' => { 'TIMESTAMP' => 1 },
85 'TIMESTAMP' => { 'DATE' => 1 },
86 'NUMBER' => { 'FLOAT' => 1 },
87 );
88  
89 # the type as Oracle names it, without size, scale or precision
90 28 ▶sub _base_type {
Subroutine _base_type: called CC=2 SCAR=6.9
91 28 my ($info) = @_;
92 28 my $type = uc DBIO::Oracle::Type::map_dbio_type_to_oracle($info->{data_type}, size => $info->{size});
93 28 $type =~ s/\([^)]*\)//g;
94 28 $type =~ s/\s+/ /g;
95 28 $type =~ s/^\s+|\s+\z//g;
96 28 ▶ return $type eq 'INTEGER' ? 'NUMBER' : $type;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'INTEGER' ? : 3 25 28
97 }
98  
99  
100 20 ▶sub _refuse_unfillable {
Subroutine _refuse_unfillable: called CC=22 SCAR=31.8
101 20 my ($self, $diff, $dbh) = @_;
102  
103 9 ▶ my $has_rows = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
104 9 my ($table, $where) = @_;
105 9 ▶ return scalar $dbh->selectrow_array(sprintf 'SELECT 1 FROM %s WHERE %sROWNUM = 1',
Branch: 1 branch
BranchTrueFalseTotal
$where ? : 5 4 9
106 DBIO::Oracle::Identifier::quote_table($table), $where ? "$where AND " : '');
107 20 };
108  
109 20 for my $op (@{ $diff->operations }) {
110 20 ▶ next unless $op->isa('DBIO::Oracle::Diff::Column');
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("DBIO::Oracle::Diff::Column") 0 20 20
111 20 my ($table, $column) = ($op->table_name, $op->column_name);
112 20 my $quoted = DBIO::Oracle::Identifier::quote($column);
113  
114 20 ▶ if ($op->action eq 'add') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->action eq 'add') { } 5 15 20
elsif ($op->action eq 'alter') { } 15 0 15
115 5 ▶ my $new = $op->new_info || {};
Condition50%1/2
$op->new_info || {}
l!l
50
MC/DC0%0/1
$op->new_info || {}
$op->new_info
Truth table50%1/2
A
A= $op->new_info
Aresult
11
00
116 5 ▶ next unless $new->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"not_null"} 0 5 5
117 next if defined $new->{default_value}
118 5 ▶ && (ref $new->{default_value} || $new->{default_value} ne 'null');
Branch: 1 branch
BranchTrueFalseTotal
if defined $new->{"default_value"} and ref $new->{"default_value"} || $new->{"default_value"} ne "null" 1 4 5
Condition50%3/6
defined $new->{"default_value"} and ref $new->{"default_value"} || $new->{"default_value"} ne "null"
!ll&&!rl&&r
401
ref $new->{"default_value"} or $new->{"default_value"} ne "null"
l!l&&r!l&&!r
010
MC/DC33%1/3
defined $new->{"default_value"} and ref $new->{"default_value"} || $new->{"default_value"} ne "null"
defined $new->{"default_value"} ref $new->{"default_value"} $new->{"default_value"} ne "null"
Truth table50%2/4
A and B or C
A= defined $new->{"default_value"}B= ref $new->{"default_value"}C= $new->{"default_value"} ne "null"
ABCresult
0XX0
1000
11X1
1011
119 4 ▶ next unless $has_rows->($table);
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table) 1 3 4
120 3 DBIO::Exception->throw(sprintf
121 'Oracle refuses to add the column %s.%s: it is NOT NULL without a '
122 . 'default, and table %s holds rows that would have no value for it '
123 . '(ORA-01758)', $table, $column, $table);
124 }
125 elsif ($op->action eq 'alter') {
126 15 ▶ my ($old, $new) = ($op->old_info || {}, $op->new_info || {});
Condition50%2/4
$op->old_info || {}
l!l
150
$op->new_info || {}
l!l
150
MC/DC0%0/1
$op->old_info || {}
$op->old_info
MC/DC0%0/1
$op->new_info || {}
$op->new_info
Truth table50%1/2
A
A= $op->old_info
Aresult
11
00
Truth table50%1/2
A
A= $op->new_info
Aresult
11
00
127  
128 15 ▶ if ($new->{not_null} && !$old->{not_null}
Branch: 1 branch
BranchTrueFalseTotal
if ($new->{"not_null"} and not $old->{"not_null"} and &$has_rows($table, "$quoted IS NULL")) 1 14 15
Condition83%5/6
$new->{"not_null"} and not $old->{"not_null"} and &$has_rows($table, "$quoted IS NULL")
!ll&&!rl&&r
1311
$new->{"not_null"} and not $old->{"not_null"}
!ll&&!rl&&r
1302
MC/DC66%2/3
$new->{"not_null"} and not $old->{"not_null"} and &$has_rows($table, "$quoted IS NULL")
$new->{"not_null"} not $old->{"not_null"} &$has_rows($table, "$quoted IS NULL")
Truth table75%3/4
A and not(B) and C
A= $new->{"not_null"}B= not $old->{"not_null"}C= &$has_rows($table, "$quoted IS NULL")
ABCresult
0XX0
10X0
1100
1111
129 && $has_rows->($table, "$quoted IS NULL")) {
130 1 DBIO::Exception->throw(sprintf
131 'Oracle refuses to make the column %s.%s NOT NULL: table %s holds '
132 . 'rows where it is NULL (ORA-02296)', $table, $column, $table);
133 }
134  
135 14 my ($from, $to) = (_base_type($old), _base_type($new));
136 14 ▶ next if $from eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $to 4 10 14
137 10 ▶ next unless $KNOWN_BASE{$from} && $KNOWN_BASE{$to};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN_BASE{$from} and $KNOWN_BASE{$to} 1 9 10
Condition66%2/3
$KNOWN_BASE{$from} and $KNOWN_BASE{$to}
!ll&&!rl&&r
109
MC/DC50%1/2
$KNOWN_BASE{$from} and $KNOWN_BASE{$to}
$KNOWN_BASE{$from} $KNOWN_BASE{$to}
Truth table66%2/3
A and B
A= $KNOWN_BASE{$from}B= $KNOWN_BASE{$to}
ABresult
0X0
100
111
138 9 ▶ next if $CONVERTS{$from} && $CONVERTS{$from}{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $CONVERTS{$from} and $CONVERTS{$from}{$to} 6 3 9
Condition66%2/3
$CONVERTS{$from} and $CONVERTS{$from}{$to}
!ll&&!rl&&r
036
MC/DC50%1/2
$CONVERTS{$from} and $CONVERTS{$from}{$to}
$CONVERTS{$from} $CONVERTS{$from}{$to}
Truth table66%2/3
A and B
A= $CONVERTS{$from}B= $CONVERTS{$from}{$to}
ABresult
0X0
100
111
139 3 ▶ next unless $has_rows->($table, "$quoted IS NOT NULL");
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table, "$quoted IS NOT NULL") 1 2 3
140 2 DBIO::Exception->throw(sprintf
141 'Oracle refuses to change the type of the column %s.%s from %s to %s: '
142 . 'it holds values, and Oracle changes the datatype of a column only '
143 . 'while it is empty (ORA-01439)',
144 $table, $column, $from, $to);
145 }
146 }
147 14 return;
148 }
149  
150  
151 0 ▶sub _new_introspect {
Subroutine _new_introspect: not called CC=1 SCAR=6.9
152 0 my ($self, $dbh, @args) = @_;
153 0 return $self->_introspect_class->new(dbh => $dbh, @args);
154 }
155  
156  
157 1 ▶sub _introspect_current {
Subroutine _introspect_current: called CC=2 SCAR=7.1
158 1 my ($self) = @_;
159 1 my @foreign = $self->_ddl_class->foreign_schemas($self->schema);
160 1 ▶ return $self->_new_introspect($self->_dbh, (@foreign ? (schemas => \@foreign) : ()))->model;
Branch: 1 branch
BranchTrueFalseTotal
@foreign ? : 1 0 1
161 }
162  
163  
164 18 ▶sub _build_target_model {
Subroutine _build_target_model: called CC=10 SCAR=23.1
165 18 my $self = shift;
166 18 my @declared = $self->_ddl_class->foreign_schemas($self->schema);
167 18 ▶ return $self->SUPER::_build_target_model(@_) unless @declared;
Branch: 1 branch
BranchTrueFalseTotal
unless @declared 10 8 18
168  
169 8 my $dbh = $self->_dbh;
170 # as in the base class: the connect info is read before anything exists
171 8 $self->_temp_connect_info('probe');
172  
173 # A schema named like the connected user is its own schema: the live
174 # introspection reads those tables bare, so the main temp user stands for it.
175 6 my ($me) = eval { $self->_admin_dbh->selectrow_array('SELECT USER FROM DUAL') };
176 6 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 6 6
177 0 $self->_close_admin_dbh;
178 0 die $e;
179 }
180 6 ▶ my @foreign = grep { !defined $me || $_ ne $me } @declared;
Condition66%2/3
!defined($me) || $_ ne $me
l!l&&r!l&&!r
0101
MC/DC50%1/2
!defined($me) || $_ ne $me
!defined($me) $_ ne $me
Truth table66%2/3
not(A) || B
A= !defined($me)B= $_ ne $me
ABresult
1X1
011
000
181  
182 6 my (@made, %temp, $model);
183 6 my $built = eval {
184 6 my $main = $self->_create_temp_db($dbh);
185 5 push @made, $main;
186 5 my $k = 0;
187 5 for my $real (@foreign) {
188 8 my $name = $temp{$real} = $main . '_' . ++$k;
189 8 $self->_create_temp_db($dbh, $name);
190 7 push @made, $name;
191 }
192 4 ▶ $model = $self->_deploy_and_introspect_temp_schemas($main, \%temp,
Branch: 1 branch
BranchTrueFalseTotal
scalar @foreign < @declared ? : 1 3 4
193 scalar(@foreign) < @declared ? $me : undef);
194 3 1;
195 };
196 6 ▶ my $err = $built ? undef : $@;
Branch: 1 branch
BranchTrueFalseTotal
$built ? : 3 3 6
197  
198 # every user made is dropped, the last first; a drop that fails is warned
199 # about and does not change the outcome
200 6 for my $user (reverse @made) {
201 12 ▶ eval { $self->_drop_temp_db($dbh, $user); 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_drop_temp_db($dbh, $user); 1 } } or do { my $drop_err = "$@"; my $info = eval { do { [$self->_temp_connect_info($user)] } }; ($drop_err = DBIO::Util::mask_connect_secrets($drop_err, $info)) =~ s/\s+\z//; warn ref($self) . ": temporary database '${user}' could not be dropped and " . "is still on the server -- drop it by hand: $drop_err\n" } 11 1 12
202 1 my $drop_err = "$@";
203 1 my $info = eval { [ $self->_temp_connect_info($user) ] };
204 1 ($drop_err = DBIO::Util::mask_connect_secrets($drop_err, $info)) =~ s/\s+\z//;
205 1 warn ref($self) . ": temporary database '$user' could not be dropped and "
206 . "is still on the server -- drop it by hand: $drop_err\n";
207 };
208 }
209  
210 6 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 3 6
211 3 return $model;
212 }
213  
214 # Connects as one temp user (the passwords masked in what is raised).
215 10 ▶sub _connect_temp {
Subroutine _connect_temp: called CC=3 SCAR=12.0
216 10 my ($self, $user) = @_;
217 10 my ($dsn, $name, $pass) = $self->_temp_connect_info($user);
218 10 ▶ my $temp_dbh = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $temp_dbh = eval { do { "DBI"->connect($dsn, $name, $pass, {"RaiseError", 1, "PrintError", 0, "AutoCommit", 1}) } } or die DBIO::Util::mask_connect_secrets($@ || "Cannot connect to temp database: $DBI::errstr", [$dsn, $name, $pass]) 10 0 10
219 10 ▶ DBI->connect($dsn, $name, $pass, { RaiseError => 1, PrintError => 0, AutoCommit => 1 })
Condition0%0/2
$@ || "Cannot connect to temp database: $DBI::errstr"
l!l
00
MC/DC0%0/1
$@ || "Cannot connect to temp database: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
220 } or die DBIO::Util::mask_connect_secrets(
221 $@ || "Cannot connect to temp database: $DBI::errstr", [ $dsn, $name, $pass ],
222 );
223 10 return $temp_dbh;
224 }
225  
226 19 ▶sub _unquote_ident { my $i = $_[0]; $i =~ s/""/"/g; return $i }
Subroutine _unquote_ident: called CC=1 SCAR=0.0
227  
228 # The user whose schema a statement of the install DDL writes to: the schema
229 # of its first object when that is qualified ("U"."t"), else the main user.
230 20 ▶sub _stmt_schema {
Subroutine _stmt_schema: called CC=2 SCAR=6.9
231 20 my ($stmt) = @_;
232 20 ▶ return $stmt =~ /\A[^"]*"((?:[^"]|"")*)"\s*\./ ? _unquote_ident($1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$stmt =~ /\A[^"]*"((?:[^"]|"")*)"\s*\./ ? : 17 3 20
233 }
234  
235  
236 4 ▶sub _deploy_and_introspect_temp_schemas {
Subroutine _deploy_and_introspect_temp_schemas: called CC=38 SCAR=41.1
237 4 my ($self, $main, $temp, $own) = @_;
238 4 my %real = reverse %$temp;
239 4 my @users = ($main, map { $temp->{$_} } sort keys %$temp);
240 4 my (%conn, $model);
241  
242 4 my $ok = eval {
243 4 $conn{$_} = $self->_connect_temp($_) for @users;
244  
245 4 my $ddl = do {
246 4 ▶ local %DBIO::Oracle::Identifier::SCHEMA_MAP = (%$temp, defined $own ? ($own => $main) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $own ? : 1 3 4
247 4 $self->_ddl_class->install_ddl($self->schema);
248 };
249 4 ▶ my $storage = eval { $self->schema->storage } || undef;
Condition50%1/2
eval { do { $self->schema->storage } } || undef
l!l
40
MC/DC0%0/1
eval { do { $self->schema->storage } } || undef
eval { do { $self->schema->storage } }
Truth table50%1/2
A
A= eval { do { $self->schema->storage } }
Aresult
11
00
250 4 for my $stmt (DBIO::Deploy::Base::_split_ddl($storage, $ddl)) {
251 20 my $owner = _stmt_schema($stmt);
252 20 ▶ $owner = $main unless defined $owner && $conn{$owner};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owner and $conn{$owner} 3 17 20
Condition66%2/3
defined $owner and $conn{$owner}
!ll&&!rl&&r
3017
MC/DC50%1/2
defined $owner and $conn{$owner}
defined $owner $conn{$owner}
Truth table66%2/3
A and B
A= defined $ownerB= $conn{$owner}
ABresult
0X0
100
111
253  
254 # a foreign key into the table of another user: the sandbox's owner of
255 # the parent lets the child's user reference it (the install DDL writes
256 # no GRANT, see _build_target_model)
257 20 ▶ if ($stmt =~ /\A\s*ALTER\s+TABLE\b/i
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /\A\s*ALTER\s+TABLE\b/i and $stmt =~ /\bREFERENCES\s+((?:("(?:[^"]|"")*")\s*\.\s*)?"(?:[^"]|"")*")/i) 4 16 20
Condition66%2/3
$stmt =~ /\A\s*ALTER\s+TABLE\b/i and $stmt =~ /\bREFERENCES\s+((?:("(?:[^"]|"")*")\s*\.\s*)?"(?:[^"]|"")*")/i
!ll&&!rl&&r
1604
MC/DC50%1/2
$stmt =~ /\A\s*ALTER\s+TABLE\b/i and $stmt =~ /\bREFERENCES\s+((?:("(?:[^"]|"")*")\s*\.\s*)?"(?:[^"]|"")*")/i
$stmt =~ /\A\s*ALTER\s+TABLE\b/i $stmt =~ /\bREFERENCES\s+((?:("(?:[^"]|"")*")\s*\.\s*)?"(?:[^"]|"")*")/i
Truth table66%2/3
A and B
A= $stmt =~ /\A\s*ALTER\s+TABLE\b/iB= $stmt =~ /\bREFERENCES\s+((?:("(?:[^"]|"")*")\s*\.\s*)?"(?:[^"]|"")*")/i
ABresult
0X0
100
111
258 && $stmt =~ /\bREFERENCES\s+((?:("(?:[^"]|"")*")\s*\.\s*)?"(?:[^"]|"")*")/i) {
259 4 my ($parent, $pschema) = ($1, $2);
260 4 ▶ my $powner = defined $pschema ? _unquote_ident(substr $pschema, 1, -1) : $main;
Branch: 1 branch
BranchTrueFalseTotal
defined $pschema ? : 2 2 4
261 $conn{$powner}->do("GRANT REFERENCES ON $parent TO $owner")
262 4 ▶ if $conn{$powner} && $powner ne $owner;
Branch: 1 branch
BranchTrueFalseTotal
if $conn{$powner} and $powner ne $owner 4 0 4
Condition33%1/3
$conn{$powner} and $powner ne $owner
!ll&&!rl&&r
004
MC/DC0%0/2
$conn{$powner} and $powner ne $owner
$conn{$powner} $powner ne $owner
Truth table33%1/3
A and B
A= $conn{$powner}B= $powner ne $owner
ABresult
0X0
100
111
263 }
264 20 $self->_execute_ddl($conn{$owner}, $stmt);
265 }
266  
267 3 $model = $self->_new_introspect($conn{$main})->model;
268 4 ▶ my $rename = sub {
Subroutine __ANON__: called CC=5 SCAR=19.4
269 4 my ($name, $own_real) = @_;
270 4 ▶ if ($name =~ /\A([^.]+)\.(.+)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\A([^.]+)\.(.+)\z/s) 4 0 4
271 4 ▶ return $2 if $1 eq $main;
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq $main 2 2 4
272 2 ▶ return "$real{$1}.$2" if exists $real{$1};
Branch: 1 branch
BranchTrueFalseTotal
if exists $real{$1} 2 0 2
273 0 return $name;
274 }
275 0 ▶ return defined $own_real ? "$own_real.$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $own_real ? : 0 0 0
276 3 };
277 3 ▶ for my $fks (values %{ $model->{foreign_keys} || {} }) {
Condition50%1/2
$model->{'foreign_keys'} || {}
l!l
30
MC/DC0%0/1
$model->{'foreign_keys'} || {}
$model->{'foreign_keys'}
Truth table50%1/2
A
A= $model->{'foreign_keys'}
Aresult
11
00
278 3 $_->{to_table} = $rename->($_->{to_table}) for @$fks;
279 }
280 # a view names the views it rests on: the temp users are the real schemas again
281 3 ▶ for my $v (values %{ $model->{views} || {} }) {
Condition50%1/2
$model->{'views'} || {}
l!l
03
MC/DC0%0/1
$model->{'views'} || {}
$model->{'views'}
Truth table50%1/2
A
A= $model->{'views'}
Aresult
11
00
282 0 ▶ $_ = $rename->($_) for @{ $v->{depends_on} || [] };
Condition0%0/2
$v->{'depends_on'} || []
l!l
00
MC/DC0%0/1
$v->{'depends_on'} || []
$v->{'depends_on'}
Truth table0%0/2
A
A= $v->{'depends_on'}
Aresult
11
00
283 }
284 3 for my $real_schema (sort keys %$temp) {
285 4 my $user = $temp->{$real_schema};
286 4 my $m = $self->_new_introspect($conn{$user})->model;
287 4 ▶ for my $key (keys %{ $m->{tables} || {} }) {
Condition50%1/2
$m->{'tables'} || {}
l!l
40
MC/DC0%0/1
$m->{'tables'} || {}
$m->{'tables'}
Truth table50%1/2
A
A= $m->{'tables'}
Aresult
11
00
288 4 my $full = "$real_schema.$key";
289 4 for my $section (qw(tables columns indexes foreign_keys primary_keys unique_constraints views)) {
290 28 ▶ $model->{$section}{$full} = $m->{$section}{$key} if exists $m->{$section}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $m->{$section}{$key} 24 4 28
291 }
292 4 $model->{tables}{$full}{schema} = $real_schema;
293 4 ▶ for my $fk (@{ $model->{foreign_keys}{$full} || [] }) {
Condition50%1/2
$model->{'foreign_keys'}{$full} || []
l!l
40
MC/DC0%0/1
$model->{'foreign_keys'}{$full} || []
$model->{'foreign_keys'}{$full}
Truth table50%1/2
A
A= $model->{'foreign_keys'}{$full}
Aresult
11
00
294 4 $fk->{from_table} = $full;
295 4 $fk->{to_table} = $rename->($fk->{to_table}, $real_schema);
296 }
297 4 ▶ if (my $v = $model->{views}{$full}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $model->{"views"}{$full}) 0 4 4
298 0 ▶ $_ = $rename->($_, $real_schema) for @{ $v->{depends_on} || [] };
Condition0%0/2
$v->{'depends_on'} || []
l!l
00
MC/DC0%0/1
$v->{'depends_on'} || []
$v->{'depends_on'}
Truth table0%0/2
A
A= $v->{'depends_on'}
Aresult
11
00
299 }
300 4 ▶ for my $col (@{ $model->{columns}{$full} || [] }) {
Condition50%1/2
$model->{'columns'}{$full} || []
l!l
40
MC/DC0%0/1
$model->{'columns'}{$full} || []
$model->{'columns'}{$full}
Truth table50%1/2
A
A= $model->{'columns'}{$full}
Aresult
11
00
301 $col->{sequence} =~ s/\A\Q$user\E\./$real_schema./
302 4 ▶ if defined $col->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if defined $col->{"sequence"} 2 2 4
303 }
304 }
305 }
306 3 1;
307 };
308 4 ▶ my $err = $ok ? undef : $@;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 3 1 4
309  
310 # a disconnect that fails is raised only when nothing else was
311 4 for my $user (reverse @users) {
312 10 ▶ my $c = $conn{$user} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $c = $conn{$user} or next 10 0 10
313 10 ▶ eval { $c->disconnect; 1 } or $err ||= $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $c->disconnect; 1 } } or $err ||= $@ 10 0 10
Condition0%0/3
$err ||= $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$err ||= $@
$err $@
Truth table0%0/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
314 }
315  
316 4 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 3 4
317 3 return $model;
318 }
319  
320 # --- the desired state in a temporary user ---------------------------------
321  
322 # One counter for the process: DBIO_TMP_<pid>_<n>.
323 21 my $TEMP_COUNTER = 0;
324  
325 # What the install DDL needs of the temp user. DDL.pm writes tables (their
326 # primary, unique and foreign keys and indexes come with CREATE TABLE),
327 # sequences, BEFORE INSERT triggers and views -- nothing else.
328 21 my @TEMP_GRANTS = (
329 'CREATE SESSION', 'CREATE TABLE', 'CREATE SEQUENCE', 'CREATE TRIGGER',
330 'CREATE VIEW',
331 );
332  
333 # What the connected user needs to make and remove the temp user.
334 21 my @USER_PRIVS = ('CREATE USER', 'DROP USER');
335  
336  
337 15 ▶sub temp_db_prefix { $_[0]->{temp_db_prefix} // 'DBIO_TMP_' }
Subroutine temp_db_prefix: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$_[0]{"temp_db_prefix"} // "DBIO_TMP_"
l!l
015
MC/DC0%0/1
$_[0]{"temp_db_prefix"} // "DBIO_TMP_"
$_[0]{"temp_db_prefix"}
Truth table50%1/2
A
A= $_[0]{"temp_db_prefix"}
Aresult
11
00
338  
339 # The DSN is the live one, unchanged: the temp user is another login, not
340 # another database.
341 65 ▶sub _temp_dsn { $_[1] }
Subroutine _temp_dsn: called CC=1 SCAR=0.0
342  
343  
344 67 ▶sub _temp_connect_info {
Subroutine _temp_connect_info: called CC=2 SCAR=6.9
345 67 my ($self, $temp_db) = @_;
346 67 my ($dsn, $user, $pass) = $self->SUPER::_temp_connect_info($temp_db);
347 65 ▶ return ($dsn, $user, $pass) if $temp_db eq 'probe';
Branch: 1 branch
BranchTrueFalseTotal
if $temp_db eq "probe" 44 21 65
348 21 return ($dsn, $temp_db, $self->{_temp_password}{$temp_db});
349 }
350  
351 # Errors from a connection are masked for both passwords in play: the one of
352 # the connected user, and the one of the temp user, which a failed CREATE USER
353 # is quoted with by the driver.
354 4 ▶sub _masked {
Subroutine _masked: called CC=4 SCAR=14.5
355 4 my ($self, $error, $temp_db) = @_;
356 4 my @info = ([ $self->_temp_connect_info('probe') ]);
357 push @info, [ $self->_temp_connect_info($temp_db) ]
358 4 ▶ if defined $temp_db && $self->{_temp_password}{$temp_db};
Branch: 1 branch
BranchTrueFalseTotal
if defined $temp_db and $self->{"_temp_password"}{$temp_db} 4 0 4
Condition33%1/3
defined $temp_db and $self->{"_temp_password"}{$temp_db}
!ll&&!rl&&r
004
MC/DC0%0/2
defined $temp_db and $self->{"_temp_password"}{$temp_db}
defined $temp_db $self->{"_temp_password"}{$temp_db}
Truth table33%1/3
A and B
A= defined $temp_dbB= $self->{"_temp_password"}{$temp_db}
ABresult
0X0
100
111
359 4 $error = "$error";
360 4 $error = DBIO::Util::mask_connect_secrets($error, $_) for @info;
361 4 $error =~ s/\s+\z//;
362 4 return $error;
363 }
364  
365 # The connection that makes and removes the temp user: its own, with the
366 # credentials of the storage and AutoCommit, so that the implicit commits of
367 # CREATE USER, GRANT and DROP USER are none of the caller's transaction.
368 56 ▶sub _admin_dbh {
Subroutine _admin_dbh: called CC=4 SCAR=13.9
369 56 my ($self) = @_;
370 56 ▶ return $self->{_admin_dbh} if $self->{_admin_dbh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_admin_dbh"} 32 24 56
371  
372 24 my ($dsn, $user, $pass) = $self->_temp_connect_info('probe');
373 24 ▶ return $self->{_admin_dbh} = eval {
Condition100%2/2
eval { do { 'DBI'->connect($dsn, $user, $pass, {'RaiseError', 1, 'PrintError', 0, 'AutoCommit', 1}) } } || die(DBIO::Util::mask_connect_secrets($@ || "Cannot connect for the temporary user: $DBI::errstr", [$dsn, $user, $pass]))
l!l
231
MC/DC100%1/1
eval { do { 'DBI'->connect($dsn, $user, $pass, {'RaiseError', 1, 'PrintError', 0, 'AutoCommit', 1}) } } || die(DBIO::Util::mask_connect_secrets($@ || "Cannot connect for the temporary user: $DBI::errstr", [$dsn, $user, $pass]))
eval { do { 'DBI'->connect($dsn, $user, $pass, {'RaiseError', 1, 'PrintError', 0, 'AutoCommit', 1}) } }
Truth table100%2/2
A
A= eval { do { 'DBI'->connect($dsn, $user, $pass, {'RaiseError', 1, 'PrintError', 0, 'AutoCommit', 1}) } }
Aresult
11
00
374 24 ▶ DBI->connect($dsn, $user, $pass, {
Condition50%1/2
$@ || "Cannot connect for the temporary user: $DBI::errstr"
l!l
10
MC/DC0%0/1
$@ || "Cannot connect for the temporary user: $DBI::errstr"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
375 RaiseError => 1, PrintError => 0, AutoCommit => 1,
376 })
377 } || die DBIO::Util::mask_connect_secrets(
378 $@ || "Cannot connect for the temporary user: $DBI::errstr",
379 [ $dsn, $user, $pass ],
380 );
381 }
382  
383 23 ▶sub _close_admin_dbh {
Subroutine _close_admin_dbh: called CC=2 SCAR=7.0
384 23 my ($self) = @_;
385 23 ▶ my $admin = delete $self->{_admin_dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $admin = delete $self->{"_admin_dbh"} or return 23 0 23
386 23 eval { $admin->disconnect; 1 };
387 23 return;
388 }
389  
390 # The tablespace comes from the dictionary and goes into the SQL as a quoted
391 # identifier.
392 38 ▶sub _quote_ident { my $i = $_[0]; $i =~ s/"/""/g; return qq{"$i"} }
Subroutine _quote_ident: called CC=1 SCAR=0.0
393  
394 23 ▶sub _random_password {
Subroutine _random_password: called CC=1 SCAR=0.0
395 23 my @chars = ('a' .. 'z', 'A' .. 'Z', '0' .. '9');
396 23 return join '', 'Tp', map { $chars[ rand @chars ] } 1 .. 24;
397 }
398  
399  
400 23 ▶sub _create_temp_db {
Subroutine _create_temp_db: called CC=10 SCAR=23.3
401 23 my ($self, undef, $name) = @_;
402  
403 23 ▶ $name //= $self->temp_db_prefix . $$ . '_' . ++$TEMP_COUNTER;
Condition100%2/2
$name //= $self->temp_db_prefix . $$ . '_' . ++$TEMP_COUNTER
l!l
815
MC/DC100%1/1
$name //= $self->temp_db_prefix . $$ . '_' . ++$TEMP_COUNTER
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
404 23 my $pass = $self->{_temp_password}{$name} = _random_password();
405 23 my $created;
406  
407 23 ▶ my $admin = eval { $self->_admin_dbh } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $admin = eval { do { $self->_admin_dbh } } or do { my $err = $@; delete $self->{"_temp_password"}{$name}; die $err } 23 0 23
408 0 my $err = $@;
409 0 delete $self->{_temp_password}{$name};
410 0 die $err;
411 };
412  
413 23 my $ok = eval {
414 23 my %have = map { $_ => 1 } @{ $admin->selectcol_arrayref(
415 'SELECT privilege FROM session_privs WHERE privilege IN ('
416 23 . join(', ', map { "'$_'" } @USER_PRIVS) . ')'
417 ) };
418 23 my @missing = grep { !$have{$_} } @USER_PRIVS;
419 23 ▶ $self->schema->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if @missing 4 19 23
420 ref($self) . ': diff and upgrade build the desired state in a temporary '
421 . 'user, and the connected user lacks the system privilege '
422 . join(' and ', @missing) . ' for it (nothing was created)'
423 ) if @missing;
424  
425 19 my ($tablespace) = $admin->selectrow_array(
426 'SELECT default_tablespace FROM user_users'
427 );
428 19 ▶ my $quota = defined $tablespace && length $tablespace
Branch: 1 branch
BranchTrueFalseTotal
defined $tablespace && length $tablespace ? : 19 0 19
Condition33%1/3
defined $tablespace and length $tablespace
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $tablespace and length $tablespace
defined $tablespace length $tablespace
Truth table33%1/3
A and B
A= defined $tablespaceB= length $tablespace
ABresult
0X0
100
111
429 ? ' DEFAULT TABLESPACE ' . _quote_ident($tablespace)
430 . ' QUOTA UNLIMITED ON ' . _quote_ident($tablespace)
431 : '';
432  
433 19 $created = 1;
434 19 $admin->do(qq{CREATE USER $name IDENTIFIED BY "$pass"$quota});
435 19 $admin->do("GRANT $_ TO $name") for @TEMP_GRANTS;
436 17 1;
437 };
438 23 my $err = $@;
439  
440 23 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 6 17 23
441 # nothing may stay behind of a half-made user
442 6 ▶ eval { $admin->do("DROP USER $name CASCADE"); 1 } if $created;
Branch: 1 branch
BranchTrueFalseTotal
if $created 2 4 6
443 6 ▶ $err = ref $err ? $err : $self->_masked($err, $name) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $err ? : 4 2 6
444 6 delete $self->{_temp_password}{$name};
445 6 $self->_close_admin_dbh;
446 6 die $err;
447 }
448  
449 17 return $name;
450 }
451  
452  
453 17 ▶sub _drop_temp_db {
Subroutine _drop_temp_db: called CC=5 SCAR=16.1
454 17 my ($self, undef, $name) = @_;
455  
456 17 my $admin = eval { $self->_admin_dbh };
457 17 my $err = $@;
458 17 ▶ my $dropped = $admin && eval { $admin->do("DROP USER $name CASCADE"); 1 };
Condition66%2/3
$admin && eval { do { $admin->do("DROP USER $name CASCADE"); 1 } }
!ll&&!rl&&r
0215
MC/DC50%1/2
$admin && eval { do { $admin->do("DROP USER $name CASCADE"); 1 } }
$admin eval { do { $admin->do("DROP USER $name CASCADE"); 1 } }
Truth table66%2/3
A && B
A= $adminB= eval { do { $admin->do("DROP USER $name CASCADE"); 1 } }
ABresult
0X0
100
111
459 17 ▶ $err = $@ if $admin && !$dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $admin and not $dropped 2 15 17
Condition66%2/3
$admin and not $dropped
!ll&&!rl&&r
0152
MC/DC50%1/2
$admin and not $dropped
$admin not $dropped
Truth table66%2/3
A and not(B)
A= $adminB= not $dropped
ABresult
0X0
100
111
460  
461 17 $self->_close_admin_dbh;
462  
463 17 ▶ if ($dropped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dropped) 15 2 17
464 15 delete $self->{_temp_password}{$name};
465 15 return;
466 }
467 2 die $self->_masked($err, $name) . "\n";
468 }
469  
470  
471 21 1;
472  
473 __END__
« Oracle/Diff.pm ↑ Summary Oracle/Storage/LOBSupport.pm »