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 Debug verbosity. (Default 0)
54 Display brief usage information.
72 use Debbugs::DB::Util qw(prepare_execute);
73 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
75 my %options = (debug => 0,
78 developer_prepare => 0,
79 overwrite_deployment => 0,
81 deployment_dir => '/usr/share/debbugs/sqldeployment',
88 'install_version_storage|install-version-storage',
90 'current_version|current-version',
91 'developer_prepare|developer-prepare',
92 'overwrite_deployment|overwrite-deployment|force_overwrite|force-overwrite',
93 'deployment_dir|deployment-dir=s',
94 'debug|d+','help|h|?','man|m');
96 pod2usage() if $options{help};
97 pod2usage({verbose=>2}) if $options{man};
99 $DEBUG = $options{debug};
103 my @exclusive_options = qw(install upgrade current_version install_version_storage);
104 if (1 < grep {exists $options{$_}} @exclusive_options) {
105 push @USAGE_ERRORS,"You must only give one of the ".
106 join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
109 if (not grep {exists $options{$_}} @exclusive_options) {
110 $options{current_version} = 1;
113 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
115 if (exists $options{sysconfdir}) {
116 if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
117 delete $ENV{PGSYSCONFDIR};
119 $ENV{PGSYSCONFDIR} = $options{sysconfdir};
124 my $schema = Debbugs::DB->connect('dbi:Pg:service='.$options{service}) or
125 die "Unable to connect to database";
128 my $dh = DH->new({schema => $schema,
129 force_overwrite => $options{overwrite_deployment},
130 script_directory => $options{deployment_dir},
131 databases => 'PostgreSQL',
132 sql_translator_args => {producer_args=> {postgres_version => 9}},
135 if ($options{current_version}) {
136 print "The current database version is: ".$dh->database_version."\n";
138 } elsif ($options{install}) {
139 $dh->prepare_install if $options{developer_prepare};
140 $dh->install unless $options{developer_prepare};
141 ## this is lame, but because the current release of DeploymentHandler does
142 ## not support WHERE or quoted indexes properly (fixed in git), we create
143 ## these indexes manually here.
145 dbh_do(sub{my ($s,$dbh) = @_;
146 prepare_execute($dbh,<<SQL);
147 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
148 bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
149 CREATE UNIQUE INDEX bug_status_cache_bug_suite_idx ON
150 bug_status_cache(bug,suite) WHERE arch is NULL;
151 CREATE UNIQUE INDEX bug_status_cache_bug_idx ON
152 bug_status_cache(bug) WHERE arch is NULL AND suite IS NULL;
155 } elsif ($options{upgrade}) {
156 $dh->prepare_deploy if $options{developer_prepare};
157 $dh->prepare_upgrade() if $options{developer_prepare};
158 $dh->upgrade unless $options{developer_prepare};
159 } elsif ($options{install_version_storage}) {
160 $dh->prepare_version_storage_install if $options{developer_prepare};
161 $dh->install_version_storage unless $options{developer_prepare};