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 2017 by Don Armstrong <don@donarmstrong.com>.
6 package Debbugs::DB::Util;
10 Debbugs::DB::Util -- Utility routines for the database
27 use base qw(DBIx::Class);
29 use Debbugs::Common qw(open_compressed_file);
34 Routines for select requests
40 $schema->select_one($sql,@bind_vals)
42 Returns the first column from the first row returned from a select statement
47 my ($self,$sql,@bind_vals) = @_;
48 my $sth = $self->prepare_execute($sql,@bind_vals);
49 my $results = $sth->fetchall_arrayref([0]);
50 return (ref($results) and ref($results->[0]))?$results->[0][0]:undef;
55 $schema->prepare_execute($sql,@bind_vals)
57 Prepares and executes a statement
62 my ($self,$sql,@bind_vals) = @_;
68 {dbi_dummy => __FILE__.__LINE__ })
69 or die "Unable to prepare statement: $sql";
70 $sth->execute(@bind_vals) or
71 die "Unable to execute statement: ".$dbh->errstr();
85 my $fh = open_compressed_file($fn) or
86 die "Unable to open $fn for reading: $!";
89 defined($sql) or die "Unable to read from file: $!";
90 $self->prepare_execute($sql);
97 =head2 Database Upgrade and Version
105 Returns the current database version (integer)
110 return $self->select_one('SELECT db_version()');
113 =item upgrades_to_run
115 C<upgrades_to_run();>
117 Returns the set of upgrades which will have to be run (in order) to upgrade the
118 database to the current version
122 sub upgrades_to_run {
123 my ($self,$deployment_dir) = @_;
125 my $current_version = $self->db_version();
128 File::Find::find(sub {
129 if (-f $_ and /^schema_(\d+)_to_(\d+)\.pl$/) {
130 push @files, {file => $File::Find::name,
140 print STDERR Dumper(@files);
141 @files = sort {$a->{from} <=> $b->{from}}
142 # strip out upgrades which don't need to be run
143 grep {$_->{from} >= $current_version } @files;
145 print STDERR Dumper(@files);