2 # debbugs-updatesqlcache is part of debbugs, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2016 by Don Armstrong <don@donarmstrong.com>.
11 use Getopt::Long qw(:config no_ignore_case);
16 debbugs-updatesqlcache -- Update Debbugs SQL Cache
20 debbugs-updatesqlcache [options]
23 --quick, -q only load changed bugs
24 --progress Show progress bar
25 --service, -s service name
26 --sysconfdir, -c postgresql service config dir
27 --spool-dir debbugs spool directory
28 --debug, -d debugging level (Default 0)
29 --help, -h display this help
30 --man, -m display manual
48 Only update things which may have changed
52 Show progress bar (requires Term::ProgressBar)
56 Postgreql service to use; defaults to debbugs
58 =item B<--sysconfdir,-c>
60 System configuration directory to use; if not set, defaults to the
61 postgresql default. [Operates by setting PGSYSCONFDIR]
65 Debbugs spool directory; defaults to the value configured in the
66 debbugs configuration file.
70 Output more information about what is happening. Probably not useful
71 if you also set --progress.
79 Display brief useage information.
93 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list getsourcemaintainers);
94 use Debbugs::Config qw(:config);
95 use Debbugs::Status qw(bug_presence read_bug);
99 use List::AllUtils qw(natatime uniq);
110 service => $config{database},
114 Getopt::Long::Configure('pass_through');
115 GetOptions(\%options,
121 'spool_dir|spool-dir=s',
124 'debug|d+','help|h|?','man|m');
125 Getopt::Long::Configure('default');
127 pod2usage() if $options{help};
128 pod2usage({verbose=>2}) if $options{man};
130 $DEBUG = $options{debug};
133 ('update' => {function => \&update_cache,
134 arguments => {'suites|suite=s@' => 0,
137 'help' => {function => sub {pod2usage({verbose => 2});}}
141 $options{verbose} = $options{verbose} - $options{quiet};
143 if ($options{progress}) {
144 eval "use Term::ProgressBar";
145 push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
149 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
151 if (exists $options{sysconfdir}) {
152 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
153 delete $ENV{PGSYSCONFDIR};
155 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
159 if (exists $options{spool_dir} and defined $options{spool_dir}) {
160 $config{spool_dir} = $options{spool_dir};
164 if ($options{progress}) {
165 $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
166 warn "Unable to initialize progress bar: $@" if not $prog_bar;
170 my ($subcommand) = shift @ARGV;
171 if (not defined $subcommand) {
172 $subcommand = 'help';
173 print STDERR "You must provide a subcommand; displaying usage.\n";
175 } elsif (not exists $subcommands{$subcommand}) {
176 print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
181 handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
182 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
185 my ($options,$opts,$p,$config,$argv) = @_;
187 my $verbose = $options->{debug};
188 # select bugs to update
190 # basically, if this is a quick run, we want any bug which has
191 # been modified or any bug which belongs to a package which has a
192 # new version; otherwise, walk every bug
193 my $s = db_connect($options);
195 # get all of the possible architectures that we might care about
196 # select distinct s.codename,a.arch from bin_associations ba join bin_ver bv on ba.bin=bv.id join suite s on ba.suite=s.id join arch a on bv.arch=a.id;
199 if (exists $opts->{suites}) {
201 $s->resultset('Suite')->
202 search_rs({active => 1,
203 -or => {codename => [make_list($opts->{suites})],
204 suite_name => [make_list($opts->{suites})],
207 {result_class => 'DBIx::Class::ResultClass::HashRefInflator'}
211 $s->resultset('Suite')->
212 search_rs({active => 1,
214 {result_class => 'DBIx::Class::ResultClass::HashRefInflator'}
219 if ($options->{quick}) {
220 # identify the last time that we ran this query
221 my $last_query_time =
222 $s->resultset('BugStatusCache')->
225 order_by => { -desc => 'asof' },
226 columns => [qw(asof)],
229 my $dtf = $s->storage->datetime_parser;
230 if (defined $last_query_time) {
231 $last_query_time = $last_query_time->asof();
233 $last_query_time = DateTime->from_epoch(0);
235 # select last status update
236 $last_query_time = $dtf->format_datetime($last_query_time);
237 # select all bugs which are in packages which have had a binary
238 # association modified
240 map {$_->{bug_binpackages}{bug}}
241 $s->resultset('BinAssociation')->
242 search_rs({'me.modified' => {'>=',$last_query_time},
244 {columns => [qw(bug_binpackages.bug)],
249 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
252 # or a source association modified
254 map {$_->{bug_srcpackages}{bug}}
255 $s->resultset('SrcAssociation')->
256 search_rs({'me.modified' => {'>=',$last_query_time},
258 {columns => [qw(bug_srcpackages.bug)],
263 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
266 # or bugs which have been modified since we last ran
269 $s->resultset('Bug')->
270 search_rs({-or => {'me.log_modified' => {'>=',$last_query_time},
271 'me.last_modified' => {'>=',$last_query_time},
273 archived => ! $options->{archived},
275 {columns => [qw(id)],
276 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
282 ## or just select all of them
285 $s->resultset('Bug')->
286 search_rs({archived => ! $options->{archived}},
287 {columns => [qw(id)],
288 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
296 my $status = read_bug(bug => $bug);
297 next unless defined $status;
298 for my $suite (@suites) {
300 bug_presence(bug => $bug,
302 dist => $suite->{suite_name},
304 $s->resultset('BugStatusCache')->
305 update_bug_status($bug,
313 my $it = natatime 500,@bugs;
315 my $last_page = ceil(@bugs / 500);
316 $p->target($last_page) if defined $p;
317 while (my @b_sub = $it->()) {
318 $s->txn_do($update_bug,
321 $p->update($page) if defined $p;
327 sub handle_subcommand_arguments {
328 my ($argv,$args) = @_;
330 Getopt::Long::GetOptionsFromArray($argv,
335 for my $arg (keys %{$args}) {
336 next unless $args->{$arg};
337 my $r_arg = $arg; # real argument name
338 $r_arg =~ s/[=\|].+//g;
339 if (not defined $subopt->{$r_arg}) {
340 push @usage_errors, "You must give a $r_arg option";
343 pod2usage(join("\n",@usage_errors)) if @usage_errors;
348 my ($subcommand,$config,$options) = @_;
349 if (not lockpid($config->{spool_dir}.'/lock/debbugs-updatesqlcache-$subcommand')) {
350 if ($options->{quick}) {
351 # If this is a quick run, just exit
352 print STDERR "Another debbugs-updatesqlcache is running; stopping\n" if $options->{verbose};
355 print STDERR "Another debbugs-updatesqlcache is running; stopping\n";
362 # connect to the database; figure out how to handle errors
364 my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
365 die "Unable to connect to database: ";
369 my ($dirs,$p,$what,$verbose,$sub) = @_;
371 my $tot_dirs = @dirs;
373 my $avg_subfiles = 0;
374 my $completed_files = 0;
375 while (my $dir = shift @dirs) {
376 printf "Doing dir %s ...\n", $dir if $verbose;
378 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
379 my @subdirs = readdir(DIR);
382 my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
384 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
386 if ($avg_subfiles == 0) {
387 $avg_subfiles = @list;
390 $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
391 $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
394 for my $bug (@list) {
396 $p->update($completed_files) if $p;
397 print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);