Arepa/PackageDb.pm

filter: Statementstmt 94.7% 127 / 134 Branchbran 80.0% 24 / 30 Conditioncond 45.0% 9 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 23 / 23 Podpod 84.6% 11 / 13 totaltotal 84.0% 195 / 232 CC 24 SCAR 12.8
CC
24
Cov
87%
insert_source_package
8.6
new
6.2
get_compilation_queue
4.0
CRAP 3.6
« Arepa/Web/Queue.pm ↑ Summary Mojolicious/Plugin/MoreTagHelpers.pm »
1 package Arepa::PackageDb;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use TheSchwartz;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Migraine;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use constant SOURCE_PACKAGE_FIELDS => qw(name full_version
Subroutine BEGIN: called CC=1 SCAR=0.0
12 architecture distribution
13 5 comments);
14 5 ▶use constant COMPILATION_QUEUE_FIELDS => qw(source_package_id architecture
Subroutine BEGIN: called CC=1 SCAR=0.0
15 distribution builder
16 status compilation_requested_at
17 compilation_started_at
18 5 compilation_completed_at);
19  
20 6 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
Pod documented
21 6 my ($class, $path) = @_;
22  
23 # See if the DB was there before connecting, so we know if we have to
24 # create the table structure
25 6 my $create_tables = 0;
26 6 ▶ if (!defined $path || -z $path || ! -e $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $path or -z $path or not -e $path) 4 2 6
Condition50%3/6
not defined $path or -z $path or not -e $path
l!l&&r!l&&!r
042
not defined $path or -z $path
l!l&&r!l&&!r
006
MC/DC33%1/3
not defined $path or -z $path or not -e $path
not defined $path -z $path not -e $path
Truth table50%2/4
not(A) or B or not(C)
A= not defined $pathB= -z $pathC= not -e $path
ABCresult
1XX1
01X1
0011
0000
27 4 $create_tables = 1;
28 }
29  
30 6 ▶ my $dsn = "dbi:SQLite:dbname=" . ($path || "");
Condition50%1/2
$path || ''
l!l
60
MC/DC0%0/1
$path || ''
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
31 6 my $self = bless {
32 path => $path,
33 dbh => DBI->connect($dsn),
34 }, $class;
35  
36 6 ▶ if ($create_tables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create_tables) 4 2 6
37 4 $self->create_db;
38 }
39 6 my $migration_dir = '/usr/share/arepa/migrations';
40 6 my $migraine = Migraine->new($dsn, migrations_dir => $migration_dir);
41 6 $migraine->migrate;
42  
43 6 return $self;
44 }
45  
46 4 ▶sub create_db {
Subroutine create_db: called CC=3 SCAR=12.0
Pod undocumented
47 4 my ($self) = @_;
48 4 my $r;
49  
50 4 $r = $self->_dbh->do(<<EOSQL);
51 CREATE TABLE source_packages (id INTEGER PRIMARY KEY,
52 name VARCHAR(50),
53 full_version VARCHAR(20),
54 architecture VARCHAR(10),
55 distribution VARCHAR(30),
56 comments TEXT);
57 EOSQL
58 4 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 4 4
59 0 croak "Couldn't create table 'source_packages' in $self->{path}";
60 }
61  
62 4 $r = $self->_dbh->do(<<EOSQL);
63 CREATE TABLE compilation_queue (id INTEGER PRIMARY KEY,
64 source_package_id INTEGER,
65 architecture VARCHAR(10),
66 distribution VARCHAR(30),
67 builder VARCHAR(50),
68 status VARCHAR(20),
69 compilation_requested_at TIMESTAMP,
70 compilation_started_at TIMESTAMP,
71 compilation_completed_at TIMESTAMP);
72 EOSQL
73 4 ▶ if (!$r) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $r) 0 4 4
74 0 croak "Couldn't create table 'compilation_queue' in $self->{path}";
75 }
76 }
77  
78 12 ▶sub default_timestamp {
Subroutine default_timestamp: called CC=1 SCAR=0.0
Pod undocumented
79 12 my ($self) = @_;
80  
81 12 my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
82 12 return sprintf("%i-%02i-%02i %02i:%02i:%02i",
83 $year+1900, $mon+1, $mday, $hour, $min, $sec);
84 }
85  
86 128 ▶sub _dbh {
Subroutine _dbh: called CC=1 SCAR=0.0
87 128 my ($self) = @_;
88 128 return $self->{dbh};
89 }
90  
91 12 ▶sub get_source_package_id {
Subroutine get_source_package_id: called CC=3 SCAR=11.0
Pod documented
92 12 my ($self, $name, $full_version) = @_;
93  
94 12 my $extra_sql = "AND full_version = ?";
95 12 my @bind_vars = ($full_version);
96 12 ▶ if ($full_version eq '*latest*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_version eq "*latest*") 1 11 12
97 1 $extra_sql = "ORDER BY full_version DESC LIMIT 1";
98 1 @bind_vars = ();
99 }
100  
101 12 my $sth = $self->_dbh->prepare("SELECT id FROM source_packages
102 WHERE name = ?
103 $extra_sql");
104 12 $sth->execute($name, @bind_vars);
105 12 $sth->bind_columns(\my $id);
106 12 ▶ return $sth->fetch ? $id : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch ? : 5 7 12
107 }
108  
109 16 ▶sub get_source_package_by_id {
Subroutine get_source_package_by_id: called CC=2 SCAR=6.9
Pod documented
110 16 my ($self, $id) = @_;
111  
112 16 my $fields = join(", ", SOURCE_PACKAGE_FIELDS);
113 16 my $sth = $self->_dbh->prepare("SELECT $fields
114 FROM source_packages
115 WHERE id = ?");
116 16 $sth->execute($id);
117 16 $sth->bind_columns(\my $name, \my $fv, \my $arch, \my $distro,
118 \my $comments);
119 16 ▶ return $sth->fetch ? (id => $id,
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch ? : 15 1 16
120 name => $name,
121 full_version => $fv,
122 architecture => $arch,
123 distribution => $distro,
124 comments => $comments) :
125 croak "Can't find a source package with id '$id'";
126 }
127  
128 8 ▶sub insert_source_package {
Subroutine insert_source_package: called CC=7 SCAR=21.5
Pod documented
129 8 my ($self, %props) = @_;
130  
131 8 my (@fields, @field_values);
132 # Check that the props are valid
133 8 foreach my $field (keys %props) {
134 33 ▶ if (not grep { $_ eq $field } SOURCE_PACKAGE_FIELDS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $field;} ("name", "full_version", "architecture", "distribution", "comments")) 0 33 33
135 0 croak "Don't recognise field '$field'";
136 }
137 }
138 # Check that at least we have 'name' and 'full_version'
139 8 ▶ if (!defined $props{name} || !defined $props{full_version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $props{"name"} and defined $props{"full_version"}) 0 8 8
Condition33%1/3
defined $props{"name"} and defined $props{"full_version"}
!ll&&!rl&&r
008
MC/DC0%0/2
defined $props{"name"} and defined $props{"full_version"}
defined $props{"name"} defined $props{"full_version"}
Truth table33%1/3
A and B
A= defined $props{"name"}B= defined $props{"full_version"}
ABresult
0X0
100
111
140 0 croak "At least 'name' and 'full_version' are needed in a source package\n";
141 }
142  
143 my $id = $self->get_source_package_id($props{name},
144 8 $props{full_version});
145 8 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 1 7 8
146 1 return $id;
147 }
148 else {
149 my $sth = $self->_dbh->prepare("INSERT INTO source_packages (" .
150 join(", ", keys %props) .
151 ") VALUES (" .
152 7 join(", ", map { "?" }
153 keys %props) .
154 ")");
155 7 ▶ if ($sth->execute(values %props)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->execute(values %props)) { } 7 0 7
156 7 return $self->_dbh->last_insert_id(undef, undef,
157 qw(source_packages), undef);
158 }
159 else {
160 0 print STDERR "ERROR: SQL query failed: ", $self->_dbh->errstr, "\n";
161 0 return 0;
162 }
163 }
164 }
165  
166 5 ▶sub request_compilation {
Subroutine request_compilation: called CC=2 SCAR=6.9
Pod documented
167 5 my ($self, $source_id, $arch, $dist, $tstamp) = @_;
168 5 ▶ $tstamp ||= $self->default_timestamp;
Condition66%2/3
$tstamp ||= $self->default_timestamp
l!l&&r!l&&!r
230
MC/DC0%0/2
$tstamp ||= $self->default_timestamp
$tstamp $self->default_timestamp
Truth table66%2/3
A ||= B
A= $tstampB= $self->default_timestamp
ABresult
1X1
011
000
169  
170 # Check that the source package id is valid. We are not going to use the
171 # returned value for anything, but it will die with an exception if the id
172 # is not valid
173 5 my %source_package = $self->get_source_package_by_id($source_id);
174  
175 5 my $sth = $self->_dbh->prepare("INSERT INTO compilation_queue (
176 source_package_id,
177 architecture,
178 distribution,
179 status,
180 compilation_requested_at)
181 VALUES (?, ?, ?, ?, ?)");
182 5 $sth->execute($source_id, $arch, $dist, "pending", $tstamp);
183  
184 5 my $compilation_id = $self->_dbh->last_insert_id('%', '',
185 'compilation_queue',
186 'id');
187 my $theschwartz_db_conf = [
188 { dsn => "dbi:SQLite:dbname=" . $self->{path} }
189 5 ];
190 5 my $client = TheSchwartz->new(databases => $theschwartz_db_conf);
191 5 $client->insert('Arepa::Job::CompilePackage',
192 { compilation_queue_id => $compilation_id });
193 }
194  
195 28 ▶sub get_compilation_queue {
Subroutine get_compilation_queue: called CC=4 SCAR=13.9
Pod documented
196 28 my ($self, %user_opts) = @_;
197 28 my %opts = (order => "compilation_requested_at", %user_opts);
198  
199 28 my $fields = join(", ", COMPILATION_QUEUE_FIELDS);
200 28 my ($condition, $limit) = ("", "");
201 28 ▶ if (exists $opts{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"status"}) 24 4 28
202 24 $condition = "WHERE status = " . $self->_dbh->quote($opts{status});
203 }
204 28 ▶ if (exists $opts{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"limit"}) 4 24 28
205 4 $limit = "LIMIT " . $self->_dbh->quote($opts{limit});
206 }
207 28 my $sth = $self->_dbh->prepare("SELECT id, $fields
208 FROM compilation_queue
209 $condition
210 ORDER BY $opts{order}
211 $limit");
212 28 $sth->execute;
213 28 $sth->bind_columns(\my $id,
214 \my $source_id, \my $arch, \my $distro,
215 \my $builder, \my $stat, \my $requested_at,
216 \my $started_at, \my $completed_at);
217 28 my @queue = ();
218 28 ▶ while ($sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 24 28 52
219 24 push @queue, {id => $id,
220 source_package_id => $source_id,
221 architecture => $arch,
222 distribution => $distro,
223 builder => $builder,
224 status => $stat,
225 compilation_requested_at => $requested_at,
226 compilation_started_at => $started_at,
227 compilation_completed_at => $completed_at}
228 }
229 28 return @queue;
230 }
231  
232 3 ▶sub get_compilation_request_by_id {
Subroutine get_compilation_request_by_id: called CC=2 SCAR=7.0
Pod documented
233 3 my ($self, $compilation_id) = @_;
234  
235 3 my $fields = join(", ", COMPILATION_QUEUE_FIELDS);
236 3 my $sth = $self->_dbh->prepare("SELECT $fields
237 FROM compilation_queue
238 WHERE id = ?");
239 3 $sth->execute($compilation_id);
240 3 $sth->bind_columns(\my $source_id, \my $arch, \my $distro,
241 \my $builder, \my $stat, \my $requested_at,
242 \my $started_at, \my $completed_at);
243 3 my @queue = ();
244 3 ▶ if ($sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) { } 3 0 3
245 3 $sth->finish;
246 3 return (id => $compilation_id,
247 source_package_id => $source_id,
248 architecture => $arch,
249 distribution => $distro,
250 builder => $builder,
251 status => $stat,
252 compilation_requested_at => $requested_at,
253 compilation_started_at => $started_at,
254 compilation_completed_at => $completed_at);
255 }
256 else {
257 0 croak "Can't find any compilation request with id '$compilation_id'";
258 }
259 }
260  
261 5 ▶sub _set_compilation_status {
Subroutine _set_compilation_status: called CC=2 SCAR=7.4
262 5 my ($self, $status, $compilation_id, $tstamp) = @_;
263 5 ▶ $tstamp ||= $self->default_timestamp;
Condition33%1/3
$tstamp ||= $self->default_timestamp
l!l&&r!l&&!r
050
MC/DC0%0/2
$tstamp ||= $self->default_timestamp
$tstamp $self->default_timestamp
Truth table33%1/3
A ||= B
A= $tstampB= $self->default_timestamp
ABresult
1X1
011
000
264  
265 5 my $sth = $self->_dbh->prepare("UPDATE compilation_queue
266 SET status = ?,
267 compilation_completed_at = ?
268 WHERE id = ?");
269 5 $sth->execute($status, $tstamp, $compilation_id);
270 }
271  
272 4 ▶sub mark_compilation_started {
Subroutine mark_compilation_started: called CC=2 SCAR=7.4
Pod documented
273 4 my ($self, $compilation_id, $builder, $tstamp) = @_;
274 4 ▶ $tstamp ||= $self->default_timestamp;
Condition33%1/3
$tstamp ||= $self->default_timestamp
l!l&&r!l&&!r
040
MC/DC0%0/2
$tstamp ||= $self->default_timestamp
$tstamp $self->default_timestamp
Truth table33%1/3
A ||= B
A= $tstampB= $self->default_timestamp
ABresult
1X1
011
000
275 4 my $sth = $self->_dbh->prepare("UPDATE compilation_queue
276 SET status = ?,
277 builder = ?,
278 compilation_started_at = ?
279 WHERE id = ?");
280 4 $sth->execute('compiling', $builder, $tstamp, $compilation_id);
281 }
282  
283 3 ▶sub mark_compilation_completed {
Subroutine mark_compilation_completed: called CC=1 SCAR=0.0
Pod documented
284 3 my ($self, $compilation_id, $tstamp) = @_;
285 3 $self->_set_compilation_status('compiled', $compilation_id, $tstamp);
286 }
287  
288 1 ▶sub mark_compilation_failed {
Subroutine mark_compilation_failed: called CC=1 SCAR=0.0
Pod documented
289 1 my ($self, $compilation_id, $tstamp) = @_;
290 1 $self->_set_compilation_status('compilationfailed',
291 $compilation_id, $tstamp);
292 }
293  
294 1 ▶sub mark_compilation_pending {
Subroutine mark_compilation_pending: called CC=1 SCAR=0.0
Pod documented
295 1 my ($self, $compilation_id, $tstamp) = @_;
296 1 $self->_set_compilation_status('pending', $compilation_id, $tstamp);
297 1 my $theschwartz_db_conf = [{ dsn => "dbi:SQLite:dbname=".$self->{path} }];
298 1 my $client = TheSchwartz->new(databases => $theschwartz_db_conf);
299 1 $client->insert('Arepa::Job::CompilePackage',
300 { compilation_queue_id => $compilation_id });
301 }
302  
303 5 1;
304  
305 __END__
« Arepa/Web/Queue.pm ↑ Summary Mojolicious/Plugin/MoreTagHelpers.pm »