blib/lib/DBIO/SQLite/Deploy.pm

filter: Statementstmt 100% 125 / 125 Branchbran 89.4% 34 / 38 Conditioncond 77.7% 14 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 24 / 24 Podpod 100% 1 / 1 totaltotal 93.9% 203 / 216 CC 35 SCAR 19.4
CC
35
Cov
96%
_refuse_unfillable
12.0
_execute_diff
11.1
_apply_extension_claims
10.1
CRAP 6.9
« blib/lib/DBIO/SQLite/Introspect/Tables.pm ↑ Summary blib/lib/DBIO/SQLite/Test.pm »
1 package DBIO::SQLite::Deploy;
2 # ABSTRACT: Deploy and upgrade SQLite schemas via test-deploy-and-compare
3  
4 185 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 185 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 185 our $VERSION = '0.900007';
8  
9 185 ▶use base 'DBIO::Deploy::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 185 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 185 ▶use DBIO::SQLite::DDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 185 ▶use DBIO::SQLite::Introspect;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 185 ▶use DBIO::SQLite::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 185 ▶use DBIO::SQLite::Extension ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 185 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 185 ▶use DBIO::SQLite::Util qw(quote_identifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 # --- hooks for Deploy::Base -------------------------------------------------
21  
22 666 ▶sub _ddl_class { 'DBIO::SQLite::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
23 389 ▶sub _introspect_class { 'DBIO::SQLite::Introspect' }
Subroutine _introspect_class: called CC=1 SCAR=0.0
24 212 ▶sub _diff_class { 'DBIO::SQLite::Diff' }
Subroutine _diff_class: called CC=1 SCAR=0.0
25  
26  
27 196 ▶sub _build_target_model {
Subroutine _build_target_model: called CC=1 SCAR=0.0
28 196 my ($self) = @_;
29  
30 196 my $temp_dbh = DBI->connect('dbi:SQLite::memory:', '', '', {
31 RaiseError => 1, PrintError => 0, AutoCommit => 1,
32 });
33 196 $temp_dbh->do('PRAGMA foreign_keys = ON');
34  
35 196 $self->_execute_ddl($temp_dbh, $self->_install_ddl);
36  
37 194 my $model = $self->_new_introspect($temp_dbh)->model;
38 192 $temp_dbh->disconnect;
39 192 return $model;
40 }
41  
42 # --- database extensions (ADR 0066) -------------------------------------------
43  
44  
45 473 ▶sub install {
Subroutine install: called CC=1 SCAR=0.0
Pod documented
46 473 my ($self) = @_;
47 473 $self->_resolve_extension_list;
48 473 return $self->SUPER::install;
49 }
50  
51  
52 674 ▶sub _resolve_extension_list {
Subroutine _resolve_extension_list: called CC=1 SCAR=0.0
53 674 my ($self) = @_;
54 674 return DBIO::SQLite::Extension->resolve_list($self->schema);
55 }
56  
57  
58 192 ▶sub _extension_list {
Subroutine _extension_list: called CC=2 SCAR=6.9
59 192 my ($self) = @_;
60 192 ▶ my $list = $self->_resolve_extension_list or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $list = $self->_resolve_extension_list or return [] 7 185 192
61 7 return [ $list->entries ];
62 }
63  
64  
65 33 ▶sub _extension_driver_helpers { DBIO::SQLite::Extension->driver_helpers }
Subroutine _extension_driver_helpers: called CC=1 SCAR=0.0
66  
67  
68 185 my %_CLAIM_SECTION = map { $_ => 1 } qw( tables columns indexes foreign_keys triggers );
69  
70 72 ▶sub _apply_extension_claims {
Subroutine _apply_extension_claims: called CC=10 SCAR=23.2
71 72 my ($self, $model, $addresses) = @_;
72 72 for my $address (@$addresses) {
73 454 my ($kind, @args) = @$address;
74  
75 454 ▶ if (($kind // '') eq 'table') {
Branch: 2 branches
BranchTrueFalseTotal
if (($kind // '') eq 'table') { } 449 5 454
elsif (($kind // '') eq 'column') { } 4 1 5
Condition50%1/2
$kind // ""
l!l
4540
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
76 449 delete $model->{$_}{ $args[0] } for grep { $model->{$_} } keys %_CLAIM_SECTION;
77 }
78 ▶ elsif (($kind // '') eq 'column') {
Condition50%1/2
$kind // ""
l!l
50
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
79 4 my ($table, $column) = @args;
80 4 ▶ my $columns = ($model->{columns} || {})->{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $columns = +($model->{"columns"} || {})->{$table} or next 2 2 4
Condition100%2/2
$model->{'columns'} || {}
l!l
31
MC/DC100%1/1
$model->{'columns'} || {}
$model->{'columns'}
Truth table100%2/2
A
A= $model->{'columns'}
Aresult
11
00
81 2 @$columns = grep { $_->{column_name} ne $column } @$columns;
82 }
83 else {
84 DBIO::Exception->throw(sprintf
85 "extension claim: the address [%s] is of a kind this deploy does not know (table, column)",
86 1 ▶ join(', ', map { defined $_ ? $_ : 'undef' } @$address));
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 0 2
87 }
88 }
89 71 return;
90 }
91  
92  
93  
94 102 ▶sub _refuse_unfillable {
Subroutine _refuse_unfillable: called CC=12 SCAR=24.8
95 102 my ($self, $diff, $dbh) = @_;
96  
97 28 ▶ my $has_rows = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
98 28 my ($table, $where) = @_;
99 28 ▶ return scalar $dbh->selectrow_array(sprintf 'SELECT 1 FROM %s%s LIMIT 1',
Condition100%2/2
$where // ""
l!l
253
MC/DC100%1/1
$where // ""
$where
Truth table100%2/2
A
A= $where
Aresult
11
00
100 quote_identifier($table), $where // '');
101 102 };
102  
103 102 my @asks;
104 102 for my $op (@{ $diff->operations }) {
105 149 ▶ if ($op->isa('DBIO::SQLite::Diff::Column')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->isa('DBIO::SQLite::Diff::Column')) { } 16 133 149
elsif ($op->isa('DBIO::SQLite::Diff::Rebuild')) { } 69 64 133
106 16 ▶ next unless $op->action eq 'add'
Branch: 1 branch
BranchTrueFalseTotal
unless $op->action eq "add" and DBIO::SQLite::Diff::_added_not_null_without_default($op->new_info) 13 3 16
Condition100%3/3
$op->action eq "add" and DBIO::SQLite::Diff::_added_not_null_without_default($op->new_info)
!ll&&!rl&&r
3103
MC/DC100%2/2
$op->action eq "add" and DBIO::SQLite::Diff::_added_not_null_without_default($op->new_info)
$op->action eq "add" DBIO::SQLite::Diff::_added_not_null_without_default($op->new_info)
Truth table100%3/3
A and B
A= $op->action eq "add"B= DBIO::SQLite::Diff::_added_not_null_without_default($op->new_info)
ABresult
0X0
100
111
107 && DBIO::SQLite::Diff::_added_not_null_without_default($op->new_info);
108 3 push @asks, [ $op->table_name, $op->column_name, 'added' ];
109 }
110 elsif ($op->isa('DBIO::SQLite::Diff::Rebuild')) {
111 push @asks, [ $op->table_name, $_->{column}, $_->{kind} ]
112 69 ▶ for @{ $op->data_checks // [] };
Condition100%2/2
$op->data_checks // []
l!l
672
MC/DC100%1/1
$op->data_checks // []
$op->data_checks
Truth table100%2/2
A
A= $op->data_checks
Aresult
11
00
113 }
114 }
115  
116 102 for my $ask (@asks) {
117 28 my ($table, $column, $kind) = @$ask;
118 28 ▶ if ($kind eq 'added') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "added") 3 25 28
119 3 ▶ next unless $has_rows->($table);
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table) 1 2 3
120 2 DBIO::Exception->throw(sprintf
121 'SQLite 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 $table, $column, $table);
124 }
125 25 ▶ next unless $has_rows->($table, ' WHERE ' . quote_identifier($column) . ' IS NULL');
Branch: 1 branch
BranchTrueFalseTotal
unless &$has_rows($table, ' WHERE ' . quote_identifier($column) . ' IS NULL') 22 3 25
126 3 DBIO::Exception->throw(sprintf
127 'SQLite refuses to make the column %s.%s NOT NULL: table %s holds rows '
128 . 'where it is NULL',
129 $table, $column, $table);
130 }
131  
132 97 return;
133 }
134  
135  
136 104 ▶sub _check_before_apply {
Subroutine _check_before_apply: called CC=1 SCAR=0.0
137 104 my ($self, $diff) = @_;
138  
139 # Rendered in full before anything runs: an op that refuses (karr k73)
140 # throws here.
141 104 $diff->statements;
142  
143 102 $self->_refuse_unfillable($diff, $self->_dbh);
144 97 return;
145 }
146  
147 97 ▶sub _execute_diff {
Subroutine _execute_diff: called CC=11 SCAR=24.1
148 97 my ($self, $diff) = @_;
149  
150 97 my @statements = $diff->statements;
151 97 my $dbh = $self->_dbh;
152  
153 97 ▶ $self->{_extension_protocol}->assert_no_refusals if $self->{_extension_protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_extension_protocol"} 0 97 97
154  
155 97 ▶ unless (grep { $_->isa('DBIO::SQLite::Diff::Rebuild') } @{ $diff->operations }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->isa("DBIO::SQLite::Diff::Rebuild");} @{$diff->operations;}) 34 63 97
156 34 $self->_execute_ddl($dbh, join "\n", @statements);
157 34 return 1;
158 }
159  
160 63 my @pragmas = qw(foreign_keys legacy_alter_table);
161 63 my %before = map { $_ => scalar $dbh->selectrow_array("PRAGMA $_") } @pragmas;
162  
163 63 ▶ my $restore = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
164 63 ▶ $dbh->do(sprintf 'PRAGMA %s=%s', $_, $before{$_} ? 'ON' : 'OFF') for @pragmas;
Branch: 1 branch
BranchTrueFalseTotal
$before{$_} ? : 47 79 126
165 63 };
166  
167 63 my $ok = eval { $self->_execute_ddl($dbh, join "\n", @statements); 1 };
168 63 my $error = $@;
169  
170 # The error to throw is the one that stopped the diff, as it came; what
171 # goes wrong while tidying up after it is only reported.
172 63 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 4 59 63
173 # What a failed rebuild left open. Not every failure leaves a savepoint
174 # -- the statement that failed may belong to another op -- and without
175 # one there is nothing to roll back.
176 4 for my $cleanup ('ROLLBACK TO dbio_rebuild', 'RELEASE dbio_rebuild') {
177 8 ▶ eval { $dbh->do($cleanup); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->do($cleanup); 1 } } or $@ =~ /no such savepoint/ or warn "$cleanup after a failed rebuild: $@" 8 0 8
Condition66%2/3
eval { do { $dbh->do($cleanup); 1 } } or $@ =~ /no such savepoint/
l!l&&r!l&&!r
620
MC/DC0%0/2
eval { do { $dbh->do($cleanup); 1 } } or $@ =~ /no such savepoint/
eval { do { $dbh->do($cleanup); 1 } } $@ =~ /no such savepoint/
Truth table66%2/3
A or B
A= eval { do { $dbh->do($cleanup); 1 } }B= $@ =~ /no such savepoint/
ABresult
1X1
011
000
178 or $@ =~ /no such savepoint/
179 or warn "$cleanup after a failed rebuild: $@";
180 }
181  
182 # After the savepoint is gone: foreign_keys cannot be switched inside one.
183 4 ▶ eval { $restore->(); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$restore(); 1 } } or warn "restoring the connection after a failed rebuild: $@" 4 0 4
184 or warn "restoring the connection after a failed rebuild: $@";
185  
186 4 die $error;
187 }
188  
189 59 $restore->();
190  
191 59 ▶ return 1 unless $before{foreign_keys};
Branch: 1 branch
BranchTrueFalseTotal
unless $before{"foreign_keys"} 17 42 59
192  
193 42 my $bad = $dbh->selectall_arrayref('PRAGMA foreign_key_check');
194 42 ▶ if (@$bad) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bad) 1 41 42
195 1 ▶ my %tables = map { ($_->[0] // '?') => 1 } @$bad;
Condition50%1/2
$_->[0] // "?"
l!l
10
MC/DC0%0/1
$_->[0] // "?"
$_->[0]
Truth table50%1/2
A
A= $_->[0]
Aresult
11
00
196 1 $self->schema->storage->throw_exception(
197 'Foreign key violations after table rebuild: ' . join(', ', sort keys %tables)
198 );
199 }
200  
201 41 return 1;
202 }
203  
204 185 1;
205  
206 __END__
« blib/lib/DBIO/SQLite/Introspect/Tables.pm ↑ Summary blib/lib/DBIO/SQLite/Test.pm »