#!/usr/bin/perl # debbugs-installsql installs the SQL database using DBIx::Class::DeploymentHandler # and is released under the terms of the GNU GPL version 3, or any # later version, at your option. See the file README and COPYING for # more information. # Copyright 2013-2014 by Don Armstrong . use warnings; use strict; use Getopt::Long; use Pod::Usage; =head1 NAME debbugs-installsql - installs and upgrades Debbugs Databases =head1 SYNOPSIS debbugs-installsql [ --upgrade | --install ] Options: --service postgresql service to connect to --sysconfdir postgresql system configuration directory --deployment-dir SQL deployment directory --debug, -d debugging level (Default 0) --help, -h display this help --man, -m display manual =head1 OPTIONS =over =item B<--service> Postgresl service to connect to (defaults to debbugs) =item B<--sysconfdir> Postgresql sysconf dir. May also be set using the PGSYSCONFDIR environmental variable (which this option overrides). =item B<--deployment-dir> Deployment directory (defaults to /usr/share/debbugs/sql/). This directory contains an upgrade subdirectory and scripts which perform the upgrade =item B<--debug, -d> Debug verbosity. (Default 0) =item B<--help, -h> Display brief usage information. =item B<--man, -m> Display this manual. =back =head1 EXAMPLES debbugs-installsql =cut use vars qw($DEBUG); # if we're running out of git, we want to use the git base directory as the # first INC directory. If you're not running out of git, or someone has given a # non-absolute INC, don't do that. use FindBin; use if (-d $FindBin::Bin.'/../.git/' && $INC[0] =~ m#^/#), lib => $FindBin::Bin.'/../lib/'; use Debbugs::DB; use JSON; my %options = (debug => 0, help => 0, man => 0, overwrite_deployment => 0, drop => 0, service => 'debbugs', deployment_dir => '/usr/share/debbugs/sql', ); GetOptions(\%options, 'service|s=s', 'dsn=s', 'sysconfdir|c=s', 'install', 'install_version_storage|install-version-storage', 'upgrade', 'current_version|current-version', 'deployment_dir|deployment-dir=s', 'debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; my @USAGE_ERRORS; my @exclusive_options = qw(install upgrade current_version install_version_storage); if (1 < grep {exists $options{$_}} @exclusive_options) { push @USAGE_ERRORS,"You must only give one of the ". join(', ',map {s/_/-/g; "--".$_} @exclusive_options). " options"; } if (not grep {exists $options{$_}} @exclusive_options) { $options{current_version} = 1; } pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; if (exists $options{sysconfdir}) { if (not defined $options{sysconfdir} or not length $options{sysconfdir}) { delete $ENV{PGSYSCONFDIR}; } else { $ENV{PGSYSCONFDIR} = $options{sysconfdir}; } } if (not exists $options{dsn} or not defined $options{dsn} or not length $options{dsn}) { $options{dsn} = 'dbi:Pg:service='.$options{service}; } my $s = Debbugs::DB->connect($options{dsn}) or die "Unable to connect to database"; if ($options{current_version}) { print "The current database version is: ".$s->db_version."\n"; exit 0; } elsif ($options{install}) { $s->sql_file_in_txn($options{deployment_dir}.'/debbugs_schema.sql'); } elsif ($options{upgrade}) { my @upgrades = $s->upgrades_to_run($options{deployment_dir}.'/upgrade'); for my $u_f (@upgrades) { eval { package fake; require $u_f->{file}; $s->txn_do(sub {fake::upgrade($s); $s->resultset('DbVersion')-> create({version => $u_f->{to}, metadata => JSON::encode_json({from => $u_f->{from}+0, to => $u_f->{to}+0, file => $u_f->{file}, }), }); }); }; if ($@) { print STDERR "Upgrade from $s->database_version to $u_f->{to} failed: $@"; exit 1; } } } elsif ($options{install_version_storage}) { if ($s->db_version > 1) { print STDERR "Version storage is already installed, current version is > 1\n"; exit 1; } # TODO Check if db_version already exists, and error out $s->sql_file_in_txn($options{deployment_dir}.'/version_storage.sql'); } __END__