| 1 | package Punk::Observe::Backend; | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 15 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use DBI (); | ||||||||||||||||||||||||||||||||||||||||
|
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 | 15 | my %POOL; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | my %DIALECT = ( | |||||||||||||||||||||||||||||||||||||||||
| 16 | SQLite => { | ||||||||||||||||||||||||||||||||||||||||||
| 17 | serial => 'INTEGER PRIMARY KEY AUTOINCREMENT', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | bigint => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | text => 'TEXT', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | json => 'TEXT', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | bool => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | dbl => 'REAL', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | small => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | now => "CAST(strftime('%s','now') AS INTEGER)", | ||||||||||||||||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | Pg => { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | serial => 'BIGSERIAL PRIMARY KEY', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | bigint => 'BIGINT', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | text => 'TEXT', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | json => 'JSONB', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | bool => 'BOOLEAN', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | dbl => 'DOUBLE PRECISION', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | small => 'SMALLINT', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | now => 'EXTRACT(EPOCH FROM now())::BIGINT', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 107 | ▶ | sub dialect_name { return lc((split /::/, ref($_[0]) || $_[0])[-1]) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dialect_name: called CC=2 SCAR=9.2
Pod
documented
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 107 | ▶ | sub _project_dir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_project_dir: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 107 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 107 | my $engine = $self->dialect_name; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 107 | ▶ | return undef unless defined $engine && length $engine; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $engine and length $engine
MC/DC0%0/2
defined $engine and length $engine
defined $engine
length $engine
Truth table33%1/3
A and B
A= defined $engineB= length $engine
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 107 | ▶ | my $pm = $INC{'Punk/Observe/Backend.pm'} or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 107 | require File::Basename; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 107 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 107 | my $dir = File::Basename::dirname($pm); | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 107 | my @try = (File::Spec->catdir($dir, 'sqitch')); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 107 | my $up = $dir; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 107 | for (1 .. 5) { | |||||||||||||||||||||||||||||||||||||||||
| 53 | 535 | $up = File::Spec->catdir($up, File::Spec->updir); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 535 | push @try, File::Spec->catdir($up, 'sqitch'); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 107 | for my $c (@try) { | |||||||||||||||||||||||||||||||||||||||||
| 57 | 107 | my $p = File::Spec->catdir($c, $engine); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 107 | ▶ | return $p if -f File::Spec->catfile($p, 'sqitch.plan'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 107 | ▶ | sub _plan_changes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plan_changes: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 107 | my ($self, $dir) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 107 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 107 | ▶ | open my $fh, '<', File::Spec->catfile($dir, 'sqitch.plan') or return []; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 107 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 107 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 642 | ▶ | next if $line =~ /\A\s*\z/ || $line =~ /\A\s*[#%]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line =~ /\A\s*\z/ or $line =~ /\A\s*[#%]/
MC/DC100%2/2
$line =~ /\A\s*\z/ or $line =~ /\A\s*[#%]/
$line =~ /\A\s*\z/
$line =~ /\A\s*[#%]/
Truth table100%3/3
A or B
A= $line =~ /\A\s*\z/B= $line =~ /\A\s*[#%]/
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 321 | ▶ | my ($name) = $line =~ /\A(\S+)/ or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 321 | ▶ | next if $name =~ /\A\@/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 321 | $name =~ s/\@.*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 321 | push @out, $name; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 107 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 107 | return \@out; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 24 | ▶ | sub LATEST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
LATEST: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 24 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 24 | ▶ | return undef unless ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 24 | ▶ | my $dir = $self->_project_dir or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 24 | my $c = $self->_plan_changes($dir); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 24 | ▶ | return @$c ? $c->[-1] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 134 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 134 | my ($class, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 134 | ▶ | if ($class eq __PACKAGE__) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 70 | my $impl = $opt{backend}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 70 | ▶ | if (defined $impl && length $impl) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $impl and length $impl
MC/DC0%0/2
defined $impl and length $impl
defined $impl
length $impl
Truth table33%1/3
A and B
A= defined $implB= length $impl
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $impl =~ s/\A\+// or $impl = __PACKAGE__ . "::$impl"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 70 | $impl = __PACKAGE__ . '::' . _infer($opt{dsn}, $opt{dbh}); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 64 | (my $file = $impl) =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 64 | ▶ | eval { require "$file.pm"; 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | or Carp::croak("Punk::Observe::Backend: cannot load $impl - $@"); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 64 | return $impl->new(%opt); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | dsn => $opt{dsn}, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | user => $opt{user}, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | password => $opt{password}, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | attr => $opt{attr} || {}, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | dbh => $opt{dbh}, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 64 | ▶ | pid => $$, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{'attr'} || {}
MC/DC0%0/1
$opt{'attr'} || {}
$opt{'attr'}
Truth table50%1/2
A
A= $opt{'attr'}
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 64 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 70 | ▶ | sub _infer { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_infer: called CC=8 SCAR=30.3
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 70 | my ($dsn, $dbh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 70 | ▶ | if (!defined $dsn || !length $dsn) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $dsn and length $dsn
MC/DC0%0/2
defined $dsn and length $dsn
defined $dsn
length $dsn
Truth table33%1/3
A and B
A= defined $dsnB= length $dsn
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return 'SQLite' unless $dbh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $d = eval { $dbh->{Driver}{Name} } || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
$dbh->{'Driver'}{'Name'}
}
} || ''
MC/DC0%0/1
eval {
do {
$dbh->{'Driver'}{'Name'}
}
} || ''
eval {
do {
$dbh->{'Driver'}{'Name'}
}
}
Truth table0%0/2
A
A= eval {
do {
$dbh->{'Driver'}{'Name'}
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return $d eq 'Pg' ? 'Pg' : 'SQLite'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 70 | ▶ | return 'Pg' if $dsn =~ /\Adbi:Pg:/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 70 | ▶ | return 'SQLite' if $dsn =~ /\Adbi:SQLite:/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 6 | Carp::croak( | |||||||||||||||||||||||||||||||||||||||||
| 125 | "Punk::Observe::Backend: no backend for '$dsn'. Shipped are SQLite " | ||||||||||||||||||||||||||||||||||||||||||
| 126 | . "and Pg; pass backend => '+Your::Class' for anything else."); | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 886 | ▶ | sub dbh { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=10 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 886 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 886 | ▶ | if ($self->{dbh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | unless ($self->{_configured}++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $wait = eval { $self->{dbh}->selectrow_array('PRAGMA busy_timeout') }; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $self->_on_connect($self->{dbh}); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | eval { $self->{dbh}->do("PRAGMA busy_timeout = $wait") } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $wait and $wait =~ /\A\d+\z/
MC/DC0%0/2
defined $wait and $wait =~ /\A\d+\z/
defined $wait
$wait =~ /\A\d+\z/
Truth table0%0/3
A and B
A= defined $waitB= $wait =~ /\A\d+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | if defined $wait && $wait =~ /\A\d+\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return $self->{dbh}; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 886 | ▶ | my $key = join "\0", $$, $self->{dsn} // '', $self->{user} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$self->{"dsn"} // ""
$self->{"user"} // ""
MC/DC0%0/1
$self->{"dsn"} // ""
$self->{"dsn"}
MC/DC0%0/1
$self->{"user"} // ""
$self->{"user"}
Truth table50%1/2
A
A= $self->{"dsn"}
Truth table50%1/2
A
A= $self->{"user"}
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 886 | my $h = $POOL{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 886 | ▶ | return $h if $h && $h->{Active} && eval { $h->ping }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$h and $h->{"Active"} and eval {
do {
$h->ping
}
}
$h and $h->{"Active"}
MC/DC33%1/3
$h and $h->{"Active"} and eval {
do {
$h->ping
}
}
$h
$h->{"Active"}
eval {
do {
$h->ping
}
}
Truth table50%2/4
A and B and C
A= $hB= $h->{"Active"}C= eval {
do {
$h->ping
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | $h = DBI->connect($self->{dsn}, $self->{user}, $self->{password}, { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 147 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 149 | AutoInactiveDestroy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 60 | %{ $self->{attr} }, | |||||||||||||||||||||||||||||||||||||||||
| 151 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 60 | $self->_on_connect($h); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 60 | return $POOL{$key} = $h; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub _on_connect { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_on_connect: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | ▶ | sub disconnect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | ▶ | return if $self->{dbh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | ▶ | my $key = join "\0", $$, $self->{dsn} // '', $self->{user} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$self->{"dsn"} // ""
$self->{"user"} // ""
MC/DC0%0/1
$self->{"dsn"} // ""
$self->{"dsn"}
MC/DC0%0/1
$self->{"user"} // ""
$self->{"user"}
Truth table50%1/2
A
A= $self->{"dsn"}
Truth table50%1/2
A
A= $self->{"user"}
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | ▶ | my $h = delete $POOL{$key} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | eval { $h->disconnect }; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub dialect { $DIALECT{ (split /::/, ref($_[0]) || $_[0])[-1] } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dialect: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 8 | ▶ | sub schema_version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schema_version: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 8 | return eval { | |||||||||||||||||||||||||||||||||||||||||
| 172 | 8 | ▶ | my $rows = $self->dbh->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 'SELECT change FROM punk_observe_schema') or return undef; | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 8 | my %have = map { $_->[0] => 1 } @$rows; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 8 | ▶ | return undef unless %have; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 8 | ▶ | my $dir = $self->_project_dir or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 8 | my $last; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 8 | for my $c (@{ $self->_plan_changes($dir) }) { | |||||||||||||||||||||||||||||||||||||||||
| 180 | 24 | ▶ | $last = $c if $have{$c}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | return $last; | |||||||||||||||||||||||||||||||||||||||||
| 183 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 67 | ▶ | sub migrate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
migrate: called CC=6 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 67 | my ($self, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 67 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 67 | my $dir = $self->_project_dir; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 67 | ▶ | Carp::croak( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->dialect_name || 'unknown'
MC/DC0%0/1
$self->dialect_name || 'unknown'
$self->dialect_name
Truth table0%0/2
A
A= $self->dialect_name
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | "Punk::Observe::Backend: no sqitch project for the " | ||||||||||||||||||||||||||||||||||||||||||
| 193 | . ($self->dialect_name || 'unknown') . " engine. The change scripts " | ||||||||||||||||||||||||||||||||||||||||||
| 194 | . "ship in sqitch/ and are installed beside the module; a checkout " | ||||||||||||||||||||||||||||||||||||||||||
| 195 | . "runs from sqitch/ at the top of the distribution.") | ||||||||||||||||||||||||||||||||||||||||||
| 196 | unless $dir; | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 67 | my $changes = $self->_plan_changes($dir); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 67 | my %want = map { $_ => 1 } @$changes; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 67 | my @order = @$changes; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 67 | ▶ | if (defined $to) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my @upto; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | for my $c (@order) { push @upto, $c; last if $c eq $to } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | @order = @upto; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 67 | $dbh->do("CREATE TABLE IF NOT EXISTS punk_observe_schema ( | |||||||||||||||||||||||||||||||||||||||||
| 208 | change TEXT NOT NULL, applied_at INTEGER NOT NULL DEFAULT 0)"); | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 67 | ▶ | return $self->_locked(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 67 | ▶ | my $done = $dbh->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dbh->selectall_arrayref('SELECT change FROM punk_observe_schema') || []
MC/DC0%0/1
$dbh->selectall_arrayref('SELECT change FROM punk_observe_schema') || []
$dbh->selectall_arrayref('SELECT change FROM punk_observe_schema')
Truth table50%1/2
A
A= $dbh->selectall_arrayref('SELECT change FROM punk_observe_schema')
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 'SELECT change FROM punk_observe_schema') || []; | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 67 | my %have = map { $_->[0] => 1 } @$done; | |||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 67 | ▶ | if (!%have) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 53 | my @seen = grep { !/\Apunk_observe_schema\z/ } | |||||||||||||||||||||||||||||||||||||||||
| 217 | 53 | ▶ | @{ $self->_tables || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_tables || []
MC/DC0%0/1
$self->_tables || []
$self->_tables
Truth table50%1/2
A
A= $self->_tables
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 53 | ▶ | Carp::croak( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | "Punk::Observe::Backend: this database has tables (@seen) and " | ||||||||||||||||||||||||||||||||||||||||||
| 220 | . "no record of a deployed change, so it was not built by these " | ||||||||||||||||||||||||||||||||||||||||||
| 221 | . "scripts. Point `db` at an empty database, or record the " | ||||||||||||||||||||||||||||||||||||||||||
| 222 | . "changes it already has in punk_observe_schema.") | ||||||||||||||||||||||||||||||||||||||||||
| 223 | if @seen; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 67 | my $applied; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 67 | for my $change (@order) { | |||||||||||||||||||||||||||||||||||||||||
| 228 | 201 | ▶ | next if $have{$change}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 159 | $self->_deploy_change($dir, $change); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 159 | $dbh->do('INSERT INTO punk_observe_schema (change, applied_at) | |||||||||||||||||||||||||||||||||||||||||
| 231 | VALUES (?, ?)', undef, $change, time); | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 159 | $applied = $change; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 67 | ▶ | return defined $applied ? $applied | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | : (@order ? $order[-1] : undef); | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 67 | }); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 159 | ▶ | sub _deploy_change { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deploy_change: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 159 | my ($self, $dir, $change) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 159 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 159 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 159 | my $sql = $self->_read_script( | |||||||||||||||||||||||||||||||||||||||||
| 245 | File::Spec->catfile($dir, 'deploy', "$change.sql")); | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 159 | ▶ | Carp::croak("Punk::Observe::Backend: no deploy script for '$change'") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | unless defined $sql; | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 159 | $self->_run_script($sql); | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 159 | my $v = $self->_read_script( | |||||||||||||||||||||||||||||||||||||||||
| 252 | File::Spec->catfile($dir, 'verify', "$change.sql")); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 159 | ▶ | if (defined $v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 159 | ▶ | eval { $self->_run_script($v); 1 } or Carp::croak( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | "Punk::Observe::Backend: '$change' deployed but did not verify: $@"); | ||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 159 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 318 | ▶ | sub _read_script { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_script: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 318 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 318 | ▶ | open my $fh, '<', $path or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 318 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 318 | my $sql = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 318 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 318 | return $sql; | |||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 318 | ▶ | sub _run_script { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_script: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 318 | my ($self, $sql) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 318 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 318 | $sql =~ s/^[ \t]*--[^\n]*$//gm; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 318 | for my $stmt (split /;\s*$/m, $sql) { | |||||||||||||||||||||||||||||||||||||||||
| 275 | 2809 | ▶ | next unless $stmt =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 2809 | ▶ | next if $stmt =~ /\A\s*(?:BEGIN|COMMIT|ROLLBACK)\b/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 2173 | $dbh->do($stmt); | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 318 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub _locked { my ($self, $code) = @_; return $code->() } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_locked: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub _tables { return [] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tables: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub ddl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ddl: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my ($self, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | my $dir = $self->_project_dir or return []; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | for my $change (@{ $self->_plan_changes($dir) }) { | |||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $sql = $self->_read_script( | |||||||||||||||||||||||||||||||||||||||||
| 293 | File::Spec->catfile($dir, "deploy", "$change.sql")); | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | push @out, $sql if defined $sql; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | last if defined $to && $change eq $to; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $to and $change eq $to
MC/DC0%0/2
defined $to and $change eq $to
defined $to
$change eq $to
Truth table0%0/3
A and B
A= defined $toB= $change eq $to
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | return \@out; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | __END__ | ||||||||||||||||||||||||||||||||||||||||||