use base qw(DBIx::Class);
use Debbugs::Common qw(open_compressed_file);
+use File::Find qw();
=head2 select
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;
}
or die "Unable to prepare statement: $sql";
$sth->execute(@bind_vals) or
die "Unable to execute statement: ".$dbh->errstr();
- $sth->finish();
+ return $sth;
});
}
}
+
+=back
+
+=head2 Database Upgrade and Version
+
+=over
+
+=item db_version
+
+C<db_version();>
+
+Returns the current database version (integer)
+
+=cut
+sub db_version {
+ my ($self) = @_;
+ return $self->select_one('SELECT db_version()');
+}
+
+=item upgrades_to_run
+
+C<upgrades_to_run();>
+
+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
+ @files = sort {$a->{from} <=> $b->{from}}
+ # strip out upgrades which don't need to be run
+ grep {$_->{from} >= $current_version } @files;
+
+ return @files;
+}
+
+
+
+
=back
=cut