DiaColloDB/Upgrade/Base.pm

filter: Statementstmt 21.7% 20 / 92 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 31.5% 6 / 19 Podpod 100% 13 / 13 totaltotal 16.3% 39 / 239 CC 44 SCAR 46.2
CC
44
Cov
11%
revert
272.0
updateHeader
90.0
dbheader
72.0
CRAP 101.4
« DiaColloDB/EnumFile/FixedLen.pm ↑ Summary DiaColloDB/Compat/v0_11/Relation/Unigrams.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DiaColloDB::Upgrade::Base.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DiaColloDB utilities: auto-magic upgrade: base class / API
6  
7 package DiaColloDB::Upgrade::Base;
8 1 ▶use DiaColloDB::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DiaColloDB::Utils qw(:time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 our @ISA = qw(DiaColloDB::Logger);
15  
16 ##==============================================================================
17 ## API
18  
19 ## $up = $CLASS_OR_OBJECT->new($dbdir?, %opts)
20 ## + create a new upgrader for local DB directory $dbdir
21 ## + if $dbdir is specified, it is stored in $up->{dbdir} and its header is loaded to $up->{hdr}
22 ## + common %opts, %$up:
23 ## (
24 ## backup=>$bool, ##-- perform auto-backup? (default=1)
25 ## keep => $bool, ##-- keep temporary files? (default=0)
26 ## timestamp=>$stamp, ##-- timestamp of this upgrade operation (default:DiaColloDB::Utils::timestamp(time))
27 ## )
28 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
29 0 my $that = shift;
30 0 ▶ my $dbdir = scalar(@_)%2==0 ? undef : shift;
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 == 0 ? : 0 0 0
31 0 ▶ my $up = bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
32 dbdir=>$dbdir,
33 backup=>1,
34 keep=>0,
35 timestamp=>DiaColloDB::Utils::timestamp(time),
36 @_,
37 }, ref($that)||$that);
38  
39 ##-- load header if available
40 0 ▶ $up->{hdr} = $up->dbheader($up->{dbdir}) if (defined($up->{dbdir}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $up->{"dbdir"} 0 0 0
41 0 return $up;
42 }
43  
44 ## $pkg = $CLASS_OR_OBJECT->label()
45 ## + returns upgrade package name
46 0 ▶sub label {
Subroutine label: not called CC=2 SCAR=17.9
Pod documented
47 0 ▶ return ref($_[0])||$_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
48 }
49  
50 ## $version = $up->toversion()
51 ## + (reccommonded): returns default target version; default just returns $DiaColloDB::VERSION
52 0 ▶sub toversion {
Subroutine toversion: not called CC=1 SCAR=6.9
Pod documented
53 0 return $DiaColloDB::VERSION;
54 }
55  
56 ## $bool = $up->needed()
57 ## + returns true iff local index in $up->{dbdir} needs upgrade
58 ## + default implementation returns true iff $coldb->{version} is less than $CLASS_OR_OBJECT->toversion()
59 0 ▶sub needed {
Subroutine needed: not called CC=2 SCAR=17.9
Pod documented
60 0 my $up = shift;
61 0 my $header = $up->dbheader();
62 0 ▶ return version->parse($header->{version}//'0.0.0') < version->parse($up->toversion);
Condition0%0/2
$header->{"version"} // "0.0.0"
l!l
00
MC/DC0%0/1
$header->{"version"} // "0.0.0"
$header->{"version"}
Truth table0%0/2
A
A= $header->{"version"}
Aresult
11
00
63 }
64  
65 ## $bool = $up->upgrade()
66 ## + performs upgrade in-place on $up->{dbdir}
67 0 ▶sub upgrade {
Subroutine upgrade: not called CC=1 SCAR=6.9
Pod documented
68 0 $_[0]->logconfess("ugprade() method not implemented");
69 }
70  
71  
72 ##==============================================================================
73 ## Backup & Revert
74  
75 ## $bool = $up->backup()
76 ## + perform backup any files we expect to change to $up->backupdir()
77 ## + subclasses should call this from $up->upgrade()
78 ## + default implementation just backs up "$dbdir/header.json", emitting a warning
79 ## if $up->{backup} is false
80 0 ▶sub backup {
Subroutine backup: not called CC=3 SCAR=24.8
Pod documented
81 0 my $up = shift;
82  
83 ##-- were backups requested?
84 0 ▶ if (!$up->{backup}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($up->{"backup"}) 0 0 0
85 0 $up->warn("backup(): backups disabled by user request");
86 0 return 1;
87 }
88  
89 ##-- backup db-header
90 0 my $dbdir = $up->{dbdir};
91 0 my $backd = $up->backupdir;
92 0 $up->info("using backup directory $backd/");
93 0 $up->info("backing up $dbdir/header.json");
94 0 ▶ DiaColloDB::Utils::copyto_a("$dbdir/header.json", $backd)
Branch: 1 branch
BranchTrueFalseTotal
DiaColloDB::Utils::copyto_a("$dbdir/header.json", $backd) or $up->logconfess("updateHeader(): failed to backup header to $backd/: $!") 0 0 0
95 or $up->logconfess("updateHeader(): failed to backup header to $backd/: $!");
96  
97 0 return 1;
98 }
99  
100 ## $dir = $up->backupdir()
101 ## + returns name of a backup directory for this upgrade
102 0 ▶sub backupdir {
Subroutine backupdir: not called CC=1 SCAR=6.9
Pod documented
103 0 my $up = shift;
104 0 my ($dbdir,$stamp) = @$up{qw(dbdir timestamp)};
105 0 $stamp =~ s/\W//g;
106 0 $stamp =~ s/T/_/;
107 0 (my $suffix = $up->label."_$stamp") =~ s/^DiaColloDB::Upgrade:://;
108 0 return "$dbdir/upgrade_${suffix}.d";
109 }
110  
111 ## $bool = $up->revert()
112 ## + rolls back a previous upgrade on $up->{dbdir}
113 ## + default implementation deletes files returned by $up->files_created()
114 ## and copies files returned by $up->files_updated() from $up->backupdir
115 0 ▶sub revert {
Subroutine revert: not called CC=16 SCAR=56.1
Pod documented
116 0 my $up = shift;
117  
118 ##-- get backup directory
119 0 my $dbdir = $up->{dbdir};
120 0 my $backd = $up->backupdir;
121  
122 ##-- sanity check(s)
123 0 ▶ $up->logconfess("revert(): no DBDIR specified")
Branch: 1 branch
BranchTrueFalseTotal
unless $dbdir 0 0 0
124 if (!$dbdir);
125 0 ▶ $up->logconfess("revert(): no backup specified")
Branch: 1 branch
BranchTrueFalseTotal
unless $backd 0 0 0
126 if (!$backd);
127 0 ▶ $up->logconfess("revert(): backup directory $backd/ not found")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $backd 0 0 0
128 if (!-d $backd);
129 0 ▶ $up->logconfess("revert(): required method revert_created() not implemented")
Branch: 1 branch
BranchTrueFalseTotal
if ($up->can("revert_created") // \&DiaColloDB::Upgrade::Base::revert_created) eq \&DiaColloDB::Upgrade::Base::revert_created 0 0 0
Condition0%0/2
$up->can("revert_created") // \&DiaColloDB::Upgrade::Base::revert_created
l!l
00
MC/DC0%0/1
$up->can("revert_created") // \&DiaColloDB::Upgrade::Base::revert_created
$up->can("revert_created")
Truth table0%0/2
A
A= $up->can("revert_created")
Aresult
11
00
130 if (($up->can('revert_created')//\&revert_created) eq \&revert_created);
131 0 ▶ $up->logconfess("revert(): required method revert_updated() not implemented")
Branch: 1 branch
BranchTrueFalseTotal
if ($up->can("revert_updated") // \&DiaColloDB::Upgrade::Base::revert_updated) eq \&DiaColloDB::Upgrade::Base::revert_updated 0 0 0
Condition0%0/2
$up->can("revert_updated") // \&DiaColloDB::Upgrade::Base::revert_updated
l!l
00
MC/DC0%0/1
$up->can("revert_updated") // \&DiaColloDB::Upgrade::Base::revert_updated
$up->can("revert_updated")
Truth table0%0/2
A
A= $up->can("revert_updated")
Aresult
11
00
132 if (($up->can('revert_updated')//\&revert_updated) eq \&revert_updated);
133  
134 ##-- get file-lists
135 0 my @created = $up->revert_created;
136 0 my @updated = $up->revert_updated;
137  
138 ##-- ensure backups exist
139 0 foreach (@updated) {
140 0 ▶ $up->logconfess("revert(): no backup found for updated file '$_'") if (!-e "$backd/$_");
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$backd/$_" 0 0 0
141 }
142  
143 ##-- unlink updated and restored files
144 0 foreach (@created,@updated) {
145 0 $up->trace("revert(): removing $dbdir/$_");
146 0 ▶ !-e "$dbdir/$_"
Branch: 1 branch
BranchTrueFalseTotal
not -e "$dbdir/$_" or unlink "$dbdir/$_" or $up->logconfess("revert(): failed to remove $dbdir/${_}: $!") 0 0 0
Condition0%0/3
not -e "$dbdir/$_" or unlink "$dbdir/$_"
l!l&&r!l&&!r
000
MC/DC0%0/2
not -e "$dbdir/$_" or unlink "$dbdir/$_"
not -e "$dbdir/$_" unlink "$dbdir/$_"
Truth table0%0/3
not(A) or B
A= not -e "$dbdir/$_"B= unlink "$dbdir/$_"
ABresult
1X1
011
000
147 or CORE::unlink("$dbdir/$_")
148 or $up->logconfess("revert(): failed to remove $dbdir/$_: $!");
149 }
150  
151 ##-- restore backup files
152 0 foreach (@updated) {
153 0 $up->trace("revert(): restoring $dbdir/$_");
154 0 ▶ DiaColloDB::Utils::copyto_a("$backd/$_", $dbdir, from=>$backd)
Branch: 1 branch
BranchTrueFalseTotal
DiaColloDB::Utils::copyto_a("$backd/$_", $dbdir, "from", $backd) or $up->logconfess("revert(): failed to restore $dbdir/${_}: $!") 0 0 0
155 or $up->logconfess("revert(): failed to restore $dbdir/$_: $!");
156 }
157  
158 ##-- remove backup directory
159 0 $up->trace("revert(): removing backup directory $backd");
160 0 ▶ remove_tree($backd)
Branch: 1 branch
BranchTrueFalseTotal
remove_tree($backd) or $up->logconfess("revert(): failed to remove backup directory ${backd}: $!") 0 0 0
161 or $up->logconfess("revert(): failed to remove backup directory $backd: $!");
162  
163 0 return 1;
164 }
165  
166 ## @files = $up->revert_created()
167 ## + returns list of files created by this upgrade, for use with default revert() implementation
168 0 ▶sub revert_created {
Subroutine revert_created: not called CC=1 SCAR=6.9
Pod documented
169 0 $_[0]->logconfess("revert_created() method not implemented");
170 }
171  
172 ## @files = $up->revert_updated()
173 ## + returns list of files updated by this upgrade, for use with default revert() implementation
174 0 ▶sub revert_updated {
Subroutine revert_updated: not called CC=1 SCAR=6.9
Pod documented
175 0 $_[0]->logconfess("revert_updated() method not implemented");
176 }
177  
178 ##==============================================================================
179 ## Utilities
180  
181 ## \%hdr = $CLASS_OR_OBJECT->dbheader($dbdir?)
182 ## + reads $dbdir/header.json
183 ## + default uses cached $CLASS_OR_OBJECT->{hdr} if available
184 0 ▶sub dbheader {
Subroutine dbheader: not called CC=8 SCAR=42.8
Pod documented
185 0 my ($up,$dbdir) = @_;
186 0 ▶ $dbdir //= $up->{dbdir} if (ref($up));
Branch: 1 branch
BranchTrueFalseTotal
if ref $up 0 0 0
Condition0%0/3
$dbdir //= $up->{"dbdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbdir //= $up->{"dbdir"}
$dbdir $up->{"dbdir"}
Truth table0%0/3
A //= B
A= $dbdirB= $up->{"dbdir"}
ABresult
1X1
011
000
187 return $up->{hdr}
188 0 ▶ if (ref($up) && defined($up->{hdr}) && ($up->{dbdir}//'') eq $dbdir);
Branch: 1 branch
BranchTrueFalseTotal
if ref $up and defined $up->{"hdr"} and ($up->{"dbdir"} // "") eq $dbdir 0 0 0
Condition0%0/8
ref $up and defined $up->{"hdr"} and ($up->{"dbdir"} // "") eq $dbdir
!ll&&!rl&&r
000
ref $up and defined $up->{"hdr"}
!ll&&!rl&&r
000
$up->{"dbdir"} // ""
l!l
00
MC/DC0%0/3
ref $up and defined $up->{"hdr"} and ($up->{"dbdir"} // "") eq $dbdir
ref $up defined $up->{"hdr"} $up->{"dbdir"}
Truth table0%0/4
A and B and C
A= ref $upB= defined $up->{"hdr"}C= $up->{"dbdir"}
ABCresult
0XX0
10X0
1100
1111
189 0 ▶ my $hdr = DiaColloDB::Utils::loadJsonFile("$dbdir/header.json")
Branch: 1 branch
BranchTrueFalseTotal
my $hdr = DiaColloDB::Utils::loadJsonFile("$dbdir/header.json") or $up->logconfess("dbheader(): failed to read header $dbdir/header.json: $!") 0 0 0
190 or $up->logconfess("dbheader(): failed to read header $dbdir/header.json: $!");
191 0 return $hdr;
192 }
193  
194 ## \%uinfo = $up->uinfo($dbdir?,%info)
195 ## + returns a default upgrade-info structure for %info
196 ## + conventional keys %uinfo =
197 ## (
198 ## version_from => $vfrom, ##-- source version (default='unknown')
199 ## version_to => $vto, ##-- target version (default=$CLASS_OR_OBJECT->_toversion)
200 ## timestamp => $time, ##-- timestamp (default=$up->{timestamp} || DiaColloDB::Utils::timestamp(time))
201 ## by => $who, ##-- user or script-name (default=$CLASS)
202 ## )
203 0 ▶sub uinfo {
Subroutine uinfo: not called CC=7 SCAR=40.3
Pod documented
204 0 my $up = shift;
205 0 ▶ my $dbdir = ((scalar(@_)%2)==0 ? undef : shift) // $up->{hdr};
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 == 0 ? : 0 0 0
Condition0%0/3
(scalar(@_) % 2 == 0 ? undef : shift()) // $up->{"hdr"}
l!l&&r!l&&!r
000
MC/DC0%0/2
(scalar(@_) % 2 == 0 ? undef : shift()) // $up->{"hdr"}
(scalar(@_) % 2 == 0 ? undef : shift()) $up->{"hdr"}
Truth table0%0/3
A // B
A= (scalar(@_) % 2 == 0 ? undef : shift())B= $up->{"hdr"}
ABresult
1X1
011
000
206 0 ▶ my $header = $up->{hdr} // ($dbdir ? $up->dbheader($dbdir) : {});
Branch: 1 branch
BranchTrueFalseTotal
$dbdir ? : 0 0 0
Condition0%0/3
$up->{"hdr"} // ($dbdir ? $up->dbheader($dbdir) : {})
l!l&&r!l&&!r
000
MC/DC0%0/2
$up->{"hdr"} // ($dbdir ? $up->dbheader($dbdir) : {})
$up->{"hdr"} ($dbdir ? $up->dbheader($dbdir) : {})
Truth table0%0/3
A // B
A= $up->{"hdr"}B= ($dbdir ? $up->dbheader($dbdir) : {})
ABresult
1X1
011
000
207 return {
208 version_from=>($header->{version} // 'unknown'),
209 version_to=>$up->toversion,
210 0 ▶ timestamp=>($up->{timestamp} || DiaColloDB::Utils::timestamp(time)),
Condition0%0/5
$header->{"version"} // "unknown"
l!l
00
$up->{'timestamp'} || DiaColloDB::Utils::timestamp(time)
l!l&&r!l&&!r
000
MC/DC0%0/1
$header->{"version"} // "unknown"
$header->{"version"}
MC/DC0%0/2
$up->{'timestamp'} || DiaColloDB::Utils::timestamp(time)
$up->{'timestamp'} DiaColloDB::Utils::timestamp(time)
Truth table0%0/2
A
A= $header->{"version"}
Aresult
11
00
Truth table0%0/3
A || B
A= $up->{'timestamp'}B= DiaColloDB::Utils::timestamp(time)
ABresult
1X1
011
000
211 by=>$up->label,
212 @_
213 };
214 }
215  
216 ## $bool = $up->updateHeader(\%extra_uinfo, \%extra_header_data)
217 ## + updates header $dbdir/header.json, creating backup if requested
218 0 ▶sub updateHeader {
Subroutine updateHeader: not called CC=9 SCAR=45.0
Pod documented
219 0 my ($up,$xinfo,$xhdr) = @_;
220 0 my $dbdir = $up->{dbdir};
221  
222 ##-- backup old header if requested
223 !$up->{backup}
224 0 ▶ or DiaColloDB::Utils::copyto_a("$dbdir/header.json", $up->backupdir)
Branch: 1 branch
BranchTrueFalseTotal
not $up->{"backup"} or DiaColloDB::Utils::copyto_a("$dbdir/header.json", $up->backupdir) or $up->logconfess('updateHeader(): failed to backup header to ' . $up->backupdir . ": $!") 0 0 0
Condition0%0/3
not $up->{"backup"} or DiaColloDB::Utils::copyto_a("$dbdir/header.json", $up->backupdir)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $up->{"backup"} or DiaColloDB::Utils::copyto_a("$dbdir/header.json", $up->backupdir)
not $up->{"backup"} DiaColloDB::Utils::copyto_a("$dbdir/header.json", $up->backupdir)
Truth table0%0/3
not(A) or B
A= not $up->{"backup"}B= DiaColloDB::Utils::copyto_a("$dbdir/header.json", $up->backupdir)
ABresult
1X1
011
000
225 or $up->logconfess("updateHeader(): failed to backup header to ".$up->backupdir.": $!");
226  
227 ##-- get upgrade info
228 0 ▶ my $uinfo = $up->uinfo($dbdir, %{$xinfo//{}});
Condition0%0/2
$xinfo // {}
l!l
00
MC/DC0%0/1
$xinfo // {}
$xinfo
Truth table0%0/2
A
A= $xinfo
Aresult
11
00
229 0 ▶ return if (!defined($uinfo)); ##-- silent upgrade
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uinfo 0 0 0
230  
231 0 my $header = $up->dbheader($dbdir);
232 0 ▶ my $upgraded = ($header->{upgraded} //= []);
Condition0%0/2
$header->{"upgraded"} //= []
l!l
00
MC/DC0%0/1
$header->{"upgraded"} //= []
$header->{"upgraded"}
Truth table0%0/2
A
A= $header->{"upgraded"}
Aresult
11
00
233 0 unshift(@$upgraded, $uinfo);
234 0 ▶ $header->{version} = $uinfo->{version_to} if ($uinfo->{version_to});
Branch: 1 branch
BranchTrueFalseTotal
if $uinfo->{"version_to"} 0 0 0
235 0 ▶ @$header{keys %$xhdr} = values %$xhdr if ($xhdr);
Branch: 1 branch
BranchTrueFalseTotal
if $xhdr 0 0 0
236 0 ▶ DiaColloDB::Utils::saveJsonFile($header, "$dbdir/header.json")
Branch: 1 branch
BranchTrueFalseTotal
DiaColloDB::Utils::saveJsonFile($header, "$dbdir/header.json") or $up->logconfess("updateHeader(): failed to save header data to $dbdir/header.json: $!") 0 0 0
237 or $up->logconfess("updateHeader(): failed to save header data to $dbdir/header.json: $!");
238 0 return $up;
239 }
240  
241  
242 ##==============================================================================
243 ## Footer
244 1 1; ##-- be happy
« DiaColloDB/EnumFile/FixedLen.pm ↑ Summary DiaColloDB/Compat/v0_11/Relation/Unigrams.pm »