]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-installsql
allow debbugs-installsql to take a --dsn option
[debbugs.git] / bin / debbugs-installsql
1 #!/usr/bin/perl
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
5 # more information.
6 # Copyright 2013-2014 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 debbugs-installsql - installs the SQL database using DBIx::Class::DeploymentHandler
18
19 =head1 SYNOPSIS
20
21 debbugs-installsql [ --upgrade | --install ]
22
23  Options:
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
30
31 =head1 OPTIONS
32
33 =over
34
35 =item B<--service>
36
37 Postgresl service to connect to (defaults to debbugs)
38
39 =item B<--sysconfdir>
40
41 Postgresql sysconf dir. May also be set using the PGSYSCONFDIR
42 environmental variable (which this option overrides).
43
44 =item B<--deployment-dir>
45
46 Deployment directory (defaults to /usr/share/debbugs/sqldeployment)
47
48 =item B<--debug, -d>
49
50 Debug verbosity. (Default 0)
51
52 =item B<--help, -h>
53
54 Display brief usage information.
55
56 =item B<--man, -m>
57
58 Display this manual.
59
60 =back
61
62 =head1 EXAMPLES
63
64 debbugs-installsql
65
66 =cut
67
68
69 use vars qw($DEBUG);
70
71 use Debbugs::DB;
72 use Debbugs::DB::Util qw(prepare_execute);
73 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
74
75 my %options = (debug           => 0,
76                help            => 0,
77                man             => 0,
78                developer_prepare => 0,
79                overwrite_deployment => 0,
80                service         => 'debbugs',
81                deployment_dir  => '/usr/share/debbugs/sqldeployment',
82               );
83
84 GetOptions(\%options,
85            'service|s=s',
86            'dsn=s',
87            'sysconfdir|c=s',
88            'install',
89            'install_version_storage|install-version-storage',
90            'upgrade',
91            'current_version|current-version',
92            'developer_prepare|developer-prepare',
93            'overwrite_deployment|overwrite-deployment|force_overwrite|force-overwrite',
94            'deployment_dir|deployment-dir=s',
95            'debug|d+','help|h|?','man|m');
96
97 pod2usage() if $options{help};
98 pod2usage({verbose=>2}) if $options{man};
99
100 $DEBUG = $options{debug};
101
102 my @USAGE_ERRORS;
103
104 my @exclusive_options = qw(install upgrade current_version install_version_storage);
105 if (1 < grep {exists $options{$_}} @exclusive_options) {
106       push @USAGE_ERRORS,"You must only give one of the ".
107           join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
108           " options";
109 }
110 if (not grep {exists $options{$_}} @exclusive_options) {
111     $options{current_version} = 1;
112 }
113
114 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
115
116 if (exists $options{sysconfdir}) {
117     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
118         delete $ENV{PGSYSCONFDIR};
119     } else {
120         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
121     }
122 }
123
124 if (not exists $options{dsn} or
125     not defined $options{dsn} or
126     not length $options{dsn}) {
127     $options{dsn} = 'dbi:Pg:service='.$options{service};
128 }
129
130
131 my $schema = Debbugs::DB->connect($options{dsn}) or
132     die "Unable to connect to database";
133
134
135 my $dh = DH->new({schema => $schema,
136                   force_overwrite => $options{overwrite_deployment},
137                   script_directory => $options{deployment_dir},
138                   databases => 'PostgreSQL',
139                   sql_translator_args => {producer_args=> {postgres_version => 9}},
140                  });
141
142 if ($options{current_version}) {
143     print "The current database version is: ".$dh->database_version."\n";
144     exit 0;
145 } elsif ($options{install}) {
146     $dh->prepare_install if $options{developer_prepare};
147     $dh->install unless $options{developer_prepare};
148     ## this is lame, but because the current release of DeploymentHandler does
149     ## not support WHERE or quoted indexes properly (fixed in git), we create
150     ## these indexes manually here.
151     $schema->storage->
152         dbh_do(sub{my ($s,$dbh) = @_;
153                    prepare_execute($dbh,<<SQL);
154 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
155  bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
156 CREATE UNIQUE INDEX bug_status_cache_bug_suite_idx ON
157  bug_status_cache(bug,suite) WHERE arch is NULL;
158 CREATE UNIQUE INDEX bug_status_cache_bug_idx ON
159  bug_status_cache(bug) WHERE arch is NULL AND suite IS NULL;
160 SQL
161                    });
162 } elsif ($options{upgrade}) {
163     $dh->prepare_deploy if $options{developer_prepare};
164     $dh->prepare_upgrade() if $options{developer_prepare};
165     $dh->upgrade unless $options{developer_prepare};
166 } elsif ($options{install_version_storage}) {
167     $dh->prepare_version_storage_install if $options{developer_prepare};
168     $dh->install_version_storage unless $options{developer_prepare};
169 }
170
171
172 __END__