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 the SQL database using DBIx::Class::DeploymentHandler
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/sqldeployment)
50 Drop tables before trying to create them. (Useful for --install, primarily).
51 Defaults to not drop tables for safety.
55 Debug verbosity. (Default 0)
59 Display brief usage information.
76 # if we're running out of git, we want to use the git base directory as the
77 # first INC directory. If you're not running out of git, or someone has given a
78 # non-absolute INC, don't do that.
80 use if (-d $FindBin::Bin.'/../.git/' && $INC[0] =~ m#^/#),
81 lib => $FindBin::Bin.'/../';
84 use Debbugs::DB::Util qw(prepare_execute);
85 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
87 my %options = (debug => 0,
90 overwrite_deployment => 0,
93 deployment_dir => '/usr/share/debbugs/sqldeployment',
101 'install_version_storage|install-version-storage',
104 'current_version|current-version',
105 'overwrite_deployment|overwrite-deployment|force_overwrite|force-overwrite',
106 'deployment_dir|deployment-dir=s',
107 'debug|d+','help|h|?','man|m');
109 pod2usage() if $options{help};
110 pod2usage({verbose=>2}) if $options{man};
112 $DEBUG = $options{debug};
116 my @exclusive_options = qw(install upgrade current_version install_version_storage);
117 if (1 < grep {exists $options{$_}} @exclusive_options) {
118 push @USAGE_ERRORS,"You must only give one of the ".
119 join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
122 if (not grep {exists $options{$_}} @exclusive_options) {
123 $options{current_version} = 1;
126 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
128 if (exists $options{sysconfdir}) {
129 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
130 delete $ENV{PGSYSCONFDIR};
132 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
136 if (not exists $options{dsn} or
137 not defined $options{dsn} or
138 not length $options{dsn}) {
139 $options{dsn} = 'dbi:Pg:service='.$options{service};
143 my $schema = Debbugs::DB->connect($options{dsn}) or
144 die "Unable to connect to database";
147 my $dh = DH->new({schema => $schema,
148 force_overwrite => $options{overwrite_deployment},
149 script_directory => $options{deployment_dir},
150 databases => 'PostgreSQL',
151 sql_translator_args => {producer_args => {postgres_version => 8.1},
152 add_drop_table => $options{drop},
156 if ($options{current_version}) {
157 print "The current database version is: ".$dh->database_version."\n";
159 } elsif ($options{install}) {
160 $dh->prepare_install;
162 ## this is lame, but because the current release of DeploymentHandler does
163 ## not support WHERE or quoted indexes properly (fixed in git), we create
164 ## these indexes manually here.
166 dbh_do(sub{my ($s,$dbh) = @_;
167 prepare_execute($dbh,<<SQL);
168 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
169 bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
170 CREATE UNIQUE INDEX bug_status_cache_bug_suite_idx ON
171 bug_status_cache(bug,suite) WHERE arch is NULL;
172 CREATE UNIQUE INDEX bug_status_cache_bug_idx ON
173 bug_status_cache(bug) WHERE arch is NULL AND suite IS NULL;
176 } elsif ($options{upgrade}) {
178 $dh->prepare_upgrade;
180 } elsif ($options{install_version_storage}) {
181 $dh->prepare_version_storage_install;
182 $dh->install_version_storage;