2 # debbugs-installsql installs the SQL database using DBIx::Class::DeploymentHandler
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
6 # Copyright 2013-2014 by Don Armstrong <don@donarmstrong.com>.
17 debbugs-installsql - installs and upgrades Debbugs Databases
21 debbugs-installsql [ --upgrade | --install ]
24 --service postgresql service to connect to
25 --sysconfdir postgresql system configuration directory
26 --deployment-dir SQL deployment directory
27 --debug, -d debugging level (Default 0)
28 --help, -h display this help
29 --man, -m display manual
37 Postgresl service to connect to (defaults to debbugs)
41 Postgresql sysconf dir. May also be set using the PGSYSCONFDIR
42 environmental variable (which this option overrides).
44 =item B<--deployment-dir>
46 Deployment directory (defaults to /usr/share/debbugs/sql/upgrade)
50 Debug verbosity. (Default 0)
54 Display brief usage information.
71 # if we're running out of git, we want to use the git base directory as the
72 # first INC directory. If you're not running out of git, or someone has given a
73 # non-absolute INC, don't do that.
75 use if (-d $FindBin::Bin.'/../.git/' && $INC[0] =~ m#^/#),
76 lib => $FindBin::Bin.'/../lib/';
81 my %options = (debug => 0,
84 overwrite_deployment => 0,
87 deployment_dir => '/usr/share/debbugs/sql',
95 'install_version_storage|install-version-storage',
97 'current_version|current-version',
98 'deployment_dir|deployment-dir=s',
99 'debug|d+','help|h|?','man|m');
101 pod2usage() if $options{help};
102 pod2usage({verbose=>2}) if $options{man};
104 $DEBUG = $options{debug};
108 my @exclusive_options = qw(install upgrade current_version install_version_storage);
109 if (1 < grep {exists $options{$_}} @exclusive_options) {
110 push @USAGE_ERRORS,"You must only give one of the ".
111 join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
114 if (not grep {exists $options{$_}} @exclusive_options) {
115 $options{current_version} = 1;
118 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
120 if (exists $options{sysconfdir}) {
121 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
122 delete $ENV{PGSYSCONFDIR};
124 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
128 if (not exists $options{dsn} or
129 not defined $options{dsn} or
130 not length $options{dsn}) {
131 $options{dsn} = 'dbi:Pg:service='.$options{service};
135 my $s = Debbugs::DB->connect($options{dsn}) or
136 die "Unable to connect to database";
139 if ($options{current_version}) {
140 print "The current database version is: ".$s->db_version."\n";
142 } elsif ($options{install}) {
143 $s->sql_file_in_txn($options{deployment_dir}.'/debbugs_schema.sql');
144 } elsif ($options{upgrade}) {
145 my @upgrades = $s->upgrades_to_run($options{deployment_dir}.'/upgrade');
146 for my $u_f (@upgrades) {
149 require $u_f->{file};
150 $s->txn_do(sub {fake::upgrade($s);
151 $s->resultset('DbVersion')->
152 create({version => $u_f->{to},
153 metadata => JSON::encode_json({from => $u_f->{from}+0,
155 file => $u_f->{file},
161 print STDERR "Upgrade from $s->database_version to $u_f->{to} failed: $@";
165 } elsif ($options{install_version_storage}) {
166 # TODO Check if db_version already exists, and error out
167 $s->sql_file_in_txn($options{deployment_dir}.'/version_storage.sql');