blib/lib/Punk/Observe/Backend/SQLite.pm

filter: Statementstmt 90.0% 36 / 40 Branchbran 40.0% 4 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 80.3% 49 / 61 CC 7 SCAR 15.2
CC
7
Cov
79%
_locked
9.0
_tables
2.0
BEGIN
1.0
CRAP 4.6
« blib/lib/Punk/Observe/Backend/Pg.pm ↑ Summary blib/lib/Punk/Observe/Store.pm »
1 package Punk::Observe::Backend::SQLite;
2  
3 15 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶use parent -norequire, 'Punk::Observe::Backend';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use Punk::Observe ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 our $VERSION = $Punk::Observe::VERSION;
12  
13 # WAL mode and foreign keys, both off by default and both wanted.
14 #
15 # `foreign_keys` is off in SQLite for backwards compatibility, so every
16 # REFERENCES clause in the schema is decoration until it is turned on - and a
17 # panel outliving its dashboard is exactly the kind of thing that then shows
18 # up as a render failure a long way from the delete that caused it.
19 #
20 # WAL mode is what lets the reader drawing a page and the worker running a
21 # migration coexist. `busy_timeout` is the other half: without it a writer
22 # holding the lock for ten milliseconds is an immediate SQLITE_BUSY rather
23 # than a ten-millisecond wait.
24 60 ▶sub _on_connect {
Subroutine _on_connect: called CC=1 SCAR=0.0
25 60 my ($self, $dbh) = @_;
26 60 $dbh->do('PRAGMA foreign_keys = ON');
27 60 $dbh->do('PRAGMA busy_timeout = 5000');
28 60 eval { $dbh->do('PRAGMA journal_mode = WAL'); 1 }; # not on :memory:
29  
30 # BEGIN IMMEDIATE THROUGH DBI, NOT AROUND IT.
31 #
32 # `$dbh->do('BEGIN IMMEDIATE')` while DBI believes AutoCommit is on is the
33 # documented way to leave a transaction DBI does not know it has - and a
34 # transaction nobody commits is a connection holding the write lock for
35 # its whole life. With four workers that is four processes each pinning
36 # the database, and every write anywhere answering "database is locked"
37 # with nothing visibly contending.
38 #
39 # This makes begin_work issue BEGIN IMMEDIATE, so the lock discipline
40 # below is the one DBI is tracking.
41 60 $dbh->{sqlite_use_immediate_transaction} = 1;
42 60 return;
43 }
44  
45 # THE LOCK IS THE TRANSACTION.
46 #
47 # SQLite has no advisory lock, so there is nothing to take that is not also
48 # the write lock itself. BEGIN IMMEDIATE acquires it up front rather than on
49 # the first write, which is the difference between two workers serialising and
50 # two workers deadlocking halfway through a migration - the deferred default
51 # would let both begin, both read version 0, and one fail on upgrade.
52 #
53 # `busy_timeout` above is what makes the loser wait rather than fail.
54 67 ▶sub _locked {
Subroutine _locked: called CC=6 SCAR=21.9
55 67 my ($self, $code) = @_;
56 67 my $dbh = $self->dbh;
57  
58 67 my $outer = !$dbh->{AutoCommit}; # somebody else owns the transaction
59 67 ▶ $dbh->begin_work unless $outer;
Branch: 1 branch
BranchTrueFalseTotal
unless $outer 67 0 67
60  
61 67 my @r = eval { $code->() };
62 67 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 67 67
63 0 my $err = $@;
64 0 ▶ eval { $dbh->rollback } unless $outer;
Branch: 1 branch
BranchTrueFalseTotal
unless $outer 0 0 0
65 0 die $err;
66 }
67 67 ▶ $dbh->commit unless $outer;
Branch: 1 branch
BranchTrueFalseTotal
unless $outer 67 0 67
68 67 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 67 67
69 }
70  
71 53 ▶sub _tables {
Subroutine _tables: called CC=2 SCAR=7.1
72 53 my ($self) = @_;
73 53 my $t = $self->dbh->selectcol_arrayref(
74 "SELECT name FROM sqlite_master
75 WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name");
76 53 ▶ return $t || [];
Condition50%1/2
$t || []
l!l
530
MC/DC0%0/1
$t || []
$t
Truth table50%1/2
A
A= $t
Aresult
11
00
77 }
78  
79 15 1;
80  
81 __END__
« blib/lib/Punk/Observe/Backend/Pg.pm ↑ Summary blib/lib/Punk/Observe/Store.pm »