Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 95 lines (76 sloc) 2.29 KB
#!/usr/bin/perl
use strict;
use DBI;
use Act::Config;
use Act::Database;
use Getopt::Std qw(getopts);
my %databases = (
main => [ $Config->database_dsn,
$Config->database_user,
$Config->database_passwd,
],
test => [ $Config->database_test_dsn,
$Config->database_test_user,
$Config->database_test_passwd,
],
);
# command line options
my %opts;
getopts('n', \%opts) or usage();
my $database = shift || 'main';
my $c = $databases{$database} or usage();
my $dbh = DBI->connect(@$c,
{ AutoCommit => 0,
PrintError => 0,
RaiseError => 1,
pg_enable_utf8 => 1,
}
) or die "can't connect to database $database: " . $DBI::errstr;
# check schema version
my ($version, $required) = Act::Database::get_versions($dbh);
if ($version > $required) {
die "database $database schema version $version is too recent: this code runs $required\n";
}
if ($version == $required) {
print "-- database $database schema version $version is up to date, exiting.\n";
exit;
}
print "-- database $database schema version $version needs update to $required\n";
for my $v ($version + 1 .. $required) {
print "-- schema update from $version to $v\n";
my $sql = Act::Database::get_update($v);
print "$sql\n";
$dbh->do($sql) unless $opts{n};
$version = $v;
}
my $sql = "UPDATE schema SET current_version = " . $dbh->quote($version) . ';';
print "$sql\n";
unless ($opts{n}) {
$dbh->do($sql);
$dbh->commit;
}
$dbh->disconnect;
exit;
###############################################
sub usage
{
my $list = join '|', sort keys %databases;
die "usage: $0 [-n] [$list]\n";
}
=head1 NAME
dbupdate - bring database schema up to date
=head1 SYNOPSIS
bin/dbupdate [-n] [main|test]
=head1 DESCRIPTION
dbupdate compares the database's schema version to the version
expected by the current code. If the database needs upgrading,
dbupdate executes the SQL commands necessary to bring the
schema up to date.
If no database name is supplied on the command line, C<bin/dbupdate>
operates on the C<main> database.
=head1 OPTIONS
=over 4
=item B<-n>
Dry run: SQL commands are printed but not run.
=back
=cut
Something went wrong with that request. Please try again.