X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lib%2FDebbugs%2FDB%2FUtil.pm;fp=lib%2FDebbugs%2FDB%2FUtil.pm;h=f7dc38f7a8e15715193de3cdd2fb00f3162d69e9;hb=084a31cbcee60e2f6de32d824aaec408018cd1d5;hp=7c52a044eb918916771ec1ac261fbda8d31abc28;hpb=1012ca820421c8b27e8fc9ae916b059d011c171a;p=debbugs.git diff --git a/lib/Debbugs/DB/Util.pm b/lib/Debbugs/DB/Util.pm index 7c52a04..f7dc38f 100644 --- a/lib/Debbugs/DB/Util.pm +++ b/lib/Debbugs/DB/Util.pm @@ -27,6 +27,7 @@ use strict; use base qw(DBIx::Class); use Debbugs::Common qw(open_compressed_file); +use File::Find qw(); =head2 select @@ -44,20 +45,8 @@ Returns the first column from the first row returned from a select statement sub select_one { my ($self,$sql,@bind_vals) = @_; - my $results = - $self->storage-> - dbh_do(sub { - my ($s,$dbh) = @_; - my $sth = $dbh-> - prepare_cached($sql, - {dbi_dummy => __FILE__.__LINE__ }) - or die "Unable to prepare statement: $sql"; - $sth->execute(@bind_vals) or - die "Unable to select one: ".$dbh->errstr(); - my $results = $sth->fetchall_arrayref([0]); - $sth->finish(); - return $results; - }); + my $sth = $self->prepare_execute($sql,@bind_vals); + my $results = $sth->fetchall_arrayref([0]); return (ref($results) and ref($results->[0]))?$results->[0][0]:undef; } @@ -80,7 +69,7 @@ sub prepare_execute { or die "Unable to prepare statement: $sql"; $sth->execute(@bind_vals) or die "Unable to execute statement: ".$dbh->errstr(); - $sth->finish(); + return $sth; }); } @@ -102,6 +91,62 @@ sub sql_file_in_txn { } + +=back + +=head2 Database Upgrade and Version + +=item db_version + +C + +Returns the current database version (integer) + +=cut +sub db_version { + my ($self) = @_; + return $self->select_one('SELECT db_version()'); +} + +=item upgrades_to_run + +C + +Returns the set of upgrades which will have to be run (in order) to upgrade the +database to the current version + +=cut + +sub upgrades_to_run { + my ($self,$deployment_dir) = @_; + + my $current_version = $self->db_version(); + + my @files; + File::Find::find(sub { + if (-f $_ and /^schema_(\d+)_to_(\d+)\.pl$/) { + push @files, {file => $File::Find::name, + from => $1, + to => $2, + }; + } + }, + $deployment_dir + ); + # sort the upgrades + use Data::Dumper; + print STDERR Dumper(@files); + @files = sort {$a->{from} <=> $b->{from}} + # strip out upgrades which don't need to be run + grep {$_->{from} >= $current_version } @files; + + print STDERR Dumper(@files); + return @files; +} + + + + =back =cut