1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
6 package Debbugs::DB::Load;
10 Debbugs::DB::Load -- Utility routines for loading the database
26 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
27 use base qw(Exporter);
30 ($VERSION) = q$Revision$ =~ /^Revision:\s+([^\s+])/;
31 $DEBUG = 0 unless defined $DEBUG;
34 %EXPORT_TAGS = (load_bug => [qw(load_bug handle_load_bug_queue load_bug_log)],
35 load_debinfo => [qw(load_debinfo)],
36 load_package => [qw(load_packages)],
37 load_suite => [qw(load_suite)],
40 Exporter::export_ok_tags(keys %EXPORT_TAGS);
41 $EXPORT_TAGS{all} = [@EXPORT_OK];
44 use Params::Validate qw(validate_with :types);
45 use List::MoreUtils qw(natatime);
47 use Debbugs::Status qw(read_bug split_status_fields);
50 use Debbugs::Common qw(make_list getparsedaddrs);
51 use Debbugs::Config qw(:config);
52 use Debbugs::MIME qw(parse_to_mime_entity decode_rfc1522);
53 use DateTime::Format::Mail;
58 Routines to load bug; exported with :load_bug
64 load_bug(db => $schema,
65 data => split_status_fields($data),
69 Loads a bug's metadata into the database. (Does not load any messages)
73 =item db -- Debbugs::DB object
75 =item data -- Bug data (from read_bug) which has been split with split_status_fields
77 =item tags -- tag cache (hashref); optional
79 =item queue -- queue of operations to perform after bug is loaded; optional.
86 my %param = validate_with(params => \@_,
87 spec => {db => {type => OBJECT,
89 data => {type => HASHREF,
92 bug => {type => SCALAR,
95 tags => {type => HASHREF,
96 default => sub {return {}},
98 severities => {type => HASHREF,
99 default => sub {return {}},
102 queue => {type => HASHREF,
106 if (not exists $param{data} and not exists $param{bug}) {
107 croak "One of data or bug must be provided to load_bug";
109 if (not exists $param{data}) {
110 $param{data} = read_bug(bug => $param{bug});
112 my $data = $param{data};
113 my $tags = $param{tags};
114 my $queue = $param{queue};
115 my $severities = $param{severities};
117 if (not defined $queue) {
122 my $s_data = split_status_fields($data);
123 for my $tag (make_list($s_data->{keywords})) {
124 next unless defined $tag and length $tag;
125 # this allows for invalid tags. But we'll use this to try to
126 # find those bugs and clean them up
127 if (not exists $tags->{$tag}) {
128 $tags->{$tag} = $s->resultset('Tag')->
129 find_or_create({tag => $tag});
131 $tags{$tag} = $tags->{$tag};
133 my $severity = length($data->{severity}) ? $data->{severity} :
134 $config{default_severity};
135 if (not exists $severities->{$severity}) {
136 $severities->{$severity} =
137 $s->resultset('Severity')->
138 find_or_create({severity => $severity},
141 $severity = $severities->{$severity};
143 {id => $data->{bug_num},
144 creation => DateTime->from_epoch(epoch => $data->{date}),
145 log_modified => DateTime->from_epoch(epoch => $data->{log_modified}),
146 last_modified => DateTime->from_epoch(epoch => $data->{last_modified}),
147 archived => $data->{archived},
148 (defined $data->{unarchived} and length($data->{unarchived}))?
149 (unarchived => DateTime->from_epoch(epoch => $data->{unarchived})):(),
150 forwarded => $data->{forwarded} // '',
151 summary => $data->{summary} // '',
152 outlook => $data->{outlook} // '',
153 subject => $data->{subject} // '',
154 done_full => $data->{done} // '',
155 severity => $severity,
156 owner_full => $data->{owner} // '',
157 submitter_full => $data->{originator} // '',
162 submitter => 'originator',
164 for my $addr_type (keys %addr_map) {
166 $s->resultset('Correspondent')->
167 get_correspondent_id($addr_map{$addr_type})
169 my $b = $s->resultset('Bug')->update_or_create($bug) or
170 die "Unable to update or create bug $bug->{id}";
172 for my $ff (qw(found fixed)) {
173 my @elements = $s->resultset('BugVer')->search({bug => $data->{bug_num},
174 found => $ff eq 'found'?1:0,
176 my %elements_to_delete = map {($elements[$_]->ver_string(),
177 $elements[$_])} 0..$#elements;
179 my @elements_to_keep;
180 for my $version (@{$data->{"${ff}_versions"}}) {
181 if (exists $elements_to_delete{$version}) {
182 push @elements_to_keep,$version;
184 $elements_to_add{$version} = 1;
187 for my $version (@elements_to_keep) {
188 delete $elements_to_delete{$version};
190 for my $element (keys %elements_to_delete) {
191 $elements_to_delete{$element}->delete();
193 for my $element (keys %elements_to_add) {
194 # find source package and source version id
195 my $ne = $s->resultset('BugVer')->new_result({bug => $data->{bug_num},
196 ver_string => $element,
197 found => $ff eq 'found'?1:0,
200 if (my ($src_pkg,$src_ver) = $element =~ m{^([^\/]+)/(.+)$}) {
201 my $src_pkg_e = $s->resultset('SrcPkg')->single({pkg => $src_pkg});
202 if (defined $src_pkg_e) {
203 $ne->src_pkg($src_pkg_e->id());
204 my $src_ver_e = $s->resultset('SrcVer')->single({src_pkg => $src_pkg_e->id(),
207 $ne->src_ver($src_ver_e->id()) if defined $src_ver_e;
215 $s->txn_do(sub {$b->set_tags([values %tags ] )});
216 # because these bugs reference other bugs which might not exist
217 # yet, we can't handle them until we've loaded all bugs. queue
219 for my $merge_block (qw(merged block)) {
220 my $data_key = $merge_block;
221 $data_key .= 'with' if $merge_block eq 'merged';
222 if (@{$data->{$data_key}||[]}) {
223 my $count = $s->resultset('Bug')->search({id => [@{$data->{$data_key}}]})->count();
224 if ($count == @{$data->{$data_key}}) {
225 handle_load_bug_queue(db=>$s,
226 queue => {$merge_block,
227 {$data->{bug_num},[@{$data->{$data_key}}]}
230 $queue->{$merge_block}{$data->{bug_num}} = [@{$data->{$data_key}}];
235 if (not $can_queue and keys %{$queue}) {
236 handle_load_bug_queue(db => $s,queue => $queue);
239 # still need to handle merges, versions, etc.
242 =item handle_load_bug_queue
244 handle_load_bug_queue(db => $schema,queue => $queue);
246 Handles a queue of operations created by load bug. [These operations
247 are used to handle cases where a bug referenced by a loaded bug may
248 not exist yet. In cases where the bugs should exist, the queue is
249 cleared automatically by load_bug if queue is undefined.
253 sub handle_load_bug_queue{
254 my %param = validate_with(params => \@_,
255 spec => {db => {type => OBJECT,
257 queue => {type => HASHREF,
261 my $queue = $param{queue};
263 (merged => {set => 'BugMerged',
264 columns => [qw(bug merged)],
267 blocks => {set => 'BugBlock',
268 columns => [qw(bug blocks)],
272 for my $queue_type (keys %queue_types) {
273 for my $bug (%{$queue->{$queue_type}}) {
274 my $qt = $queue_types{$queue_type};
276 $s->resultset($qt->{set})->search({$qt->{bug},$bug})->delete();
277 $s->populate($qt->{set},[[@{$qt->{columns}}],
278 map {[$bug,$_]} @{$queue->{$queue_type}{$bug}}]) if
279 @{$queue->{$queue_type}{$bug}//[]};
286 =item load_bug_log -- load bug logs
288 load_bug_log(db => $s,
296 =item bug -- bug whose log should be loaded
303 my %param = validate_with(params => \@_,
304 spec => {db => {type => OBJECT,
306 bug => {type => SCALAR,
308 queue => {type => HASHREF,
314 my $log = Debbugs::Log->new(bug_num => $param{bug}) or
315 die "Unable to open log for $param{bug} for reading: $!";
316 while (my $record = $log->read_record()) {
317 next unless $record->{type} eq 'incoming-recv';
318 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
319 next if defined $msg_id and exists $seen_msg_ids{$msg_id};
320 $seen_msg_ids{$msg_id} = 1 if defined $msg_id;
321 next if defined $msg_id and $msg_id =~ /handler\..+\.ack(?:info)?\@/;
322 my $message = parse($record->{text});
323 # search for a message with this message id in the database
325 # check to see if the subject, to, and from match. if so, it's
326 # probably the same message.
328 # if not, create a new message
330 # add correspondents if necessary
332 # link message to bugs if necessary
342 Commands to handle src and package version loading from debinfo files
348 load_debinfo($schema,$binname, $binver, $binarch, $srcname, $srcver);
355 my ($schema,$binname, $binver, $binarch, $srcname, $srcver) = @_;
356 my $sp = $schema->resultset('SrcPkg')->find_or_create({pkg => $srcname});
357 my $sv = $schema->resultset('SrcVer')->find_or_create({src_pkg=>$sp->id(),
359 my $arch = $schema->resultset('Arch')->find_or_create({arch => $binarch});
360 my $bp = $schema->resultset('BinPkg')->find_or_create({pkg => $binname});
361 $schema->resultset('BinVer')->find_or_create({bin_pkg_id => $bp->id(),
362 src_ver_id => $sv->id(),
363 arch_id => $arch->id(),
377 load_package($schema,$suite,$component,$arch,$pkg)
382 my ($schema,$suite,$pkgs,$p) = @_;
383 my $suite_id = $schema->resultset('Suite')->
384 find_or_create({codename => $suite})->id;
388 my $src_max_last_modified = $schema->resultset('SrcAssociation')->
389 search_rs({suite => $suite_id},
390 {order_by => {-desc => ['me.modified']},
395 my $bin_max_last_modified = $schema->resultset('BinAssociation')->
396 search_rs({suite => $suite_id},
397 {order_by => {-desc => ['me.modified']},
405 for my $pkg_tuple (@{$pkgs}) {
406 my ($arch,$component,$pkg) = @{$pkg_tuple};
407 $maints{$pkg->{Maintainer}} = $pkg->{Maintainer};
408 if ($arch eq 'source') {
409 my $source = $pkg->{Package};
410 my $source_ver = $pkg->{Version};
411 $sources{$source}{$source_ver} = $pkg->{Maintainer};
413 my $source = $pkg->{Source} // $pkg->{Package};
414 my $source_ver = $pkg->{Version};
415 if ($source =~ /^\s*(\S+) \(([^\)]+)\)\s*$/) {
416 ($source,$source_ver) = ($1,$2);
418 $sources{$source}{$source_ver} = $pkg->{Maintainer};
419 $bins{$arch}{$pkg->{Package}} =
421 bin => $pkg->{Package},
422 bin_ver => $pkg->{Version},
423 src_ver => $source_ver,
425 maint => $pkg->{Maintainer},
429 # Retrieve and Insert new maintainers
431 $schema->resultset('Maintainer')->
432 get_maintainers(keys %maints);
434 $schema->resultset('Arch')->
435 get_archs(keys %bins);
436 # We want all of the source package/versions which are in this suite to
441 # Calculate which source packages are no longer in this suite
442 for my $s ($schema->resultset('SrcPkg')->
443 src_pkg_and_ver_in_suite($suite)) {
444 if (not exists $sources{$s->{pkg}} or
445 not exists $sources{$s->{pkg}}{$s->{src_vers}{ver}}
448 $s->{src_associations}{id};
450 $included_sa{$s->{pkg}}{$s->{src_vers}} = 1;
452 # Calculate which source packages are newly in this suite
453 for my $s (keys %sources) {
454 for my $v (keys %{$sources{$s}}) {
455 if (not exists $included_sa{$s} and
456 not $included_sa{$s}{$v}) {
458 [$s,$v,$sources{$s}{$v}];
460 $p->update() if defined $p;
464 # add new source packages
465 my $it = natatime 100, @sa_to_add;
466 while (my @v = $it->()) {
470 my $s_id = $schema->resultset('SrcPkg')->
471 get_src_pkg_id($svm->[0]);
472 my $sv_id = $schema->resultset('SrcVer')->
473 get_src_ver_id($s_id,$svm->[1],$maints->{$svm->[2]});
474 $schema->resultset('SrcAssociation')->
475 insert_suite_src_ver_association($suite_id,$sv_id);
480 $p->update($p->last_update()+
481 scalar @v) if defined $p;
483 # remove associations for packages not in this suite
485 $schema->resultset('SrcAssociation')->
486 search_rs({id => \@sa_to_del})->delete();
488 # update packages in this suite to have a modification time of now
489 $schema->resultset('SrcAssociation')->
490 search_rs({suite => $suite_id})->
491 update({modified => 'NOW()'});
492 ## Handle binary packages
496 # calculate which binary packages are no longer in this suite
497 for my $b ($schema->resultset('BinPkg')->
498 bin_pkg_and_ver_in_suite($suite)) {
499 if (not exists $bins{$b->{arch}{arch}} or
500 not exists $bins{$b->{arch}{arch}}{$b->{pkg}} or
501 ($bins{$b->{arch}{arch}}{$b->{pkg}}{bin_ver} ne
506 $b->{bin_associations}{id};
508 $included_bin{$b->{arch}{arch}}{$b->{pkg}} =
511 # calculate which binary packages are newly in this suite
512 for my $a (keys %bins) {
513 for my $pkg (keys %{$bins{$a}}) {
514 if (not exists $included_bin{$a} or
515 not exists $included_bin{$a}{$pkg} or
516 $bins{$a}{$pkg}{bin_ver} ne
517 $included_bin{$a}{$pkg}) {
521 $p->update() if defined $p;
525 $it = natatime 100, @bin_to_add;
526 while (my @v = $it->()) {
530 my $s_id = $schema->resultset('SrcPkg')->
531 get_src_pkg_id($bvm->{source});
532 my $sv_id = $schema->resultset('SrcVer')->
533 get_src_ver_id($s_id,$bvm->{src_ver},$maints->{$bvm->{maint}});
534 my $b_id = $schema->resultset('BinPkg')->
535 get_bin_pkg_id($bvm->{bin});
536 my $bv_id = $schema->resultset('BinVer')->
537 get_bin_ver_id($b_id,$bvm->{bin_ver},
538 $archs->{$bvm->{arch}},$sv_id);
539 $schema->resultset('BinAssociation')->
540 insert_suite_bin_ver_association($suite_id,$bv_id);
545 $p->update($p->last_update()+
546 scalar @v) if defined $p;
549 $schema->resultset('BinAssociation')->
550 search_rs({id => \@bin_to_del})->delete();
552 $schema->resultset('BinAssociation')->
553 search_rs({suite => $suite_id})->
554 update({modified => 'NOW()'});
569 load_suite($schema,$codename,$suite,$version,$active);
574 my ($schema,$codename,$suite,$version,$active) = @_;
575 if (ref($codename)) {
576 ($codename,$suite,$version) =
577 @{$codename}{qw(Codename Suite Version)};
580 my $s = $schema->resultset('Suite')->find_or_create({codename => $codename});
581 $s->suite_name($suite);
582 $s->version($version);