]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-installsql
1156f178362e6cc40a58c16889ade459d68a302d
[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 and upgrades Debbugs Databases
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/sql/upgrade)
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 # 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.
74 use FindBin;
75 use if (-d $FindBin::Bin.'/../.git/' && $INC[0] =~ m#^/#),
76     lib => $FindBin::Bin.'/../lib/';
77
78 use Debbugs::DB;
79
80 my %options = (debug           => 0,
81                help            => 0,
82                man             => 0,
83                overwrite_deployment => 0,
84                drop            => 0,
85                service         => 'debbugs',
86                deployment_dir  => '/usr/share/debbugs/sql',
87               );
88
89 GetOptions(\%options,
90            'service|s=s',
91            'dsn=s',
92            'sysconfdir|c=s',
93            'install',
94            'install_version_storage|install-version-storage',
95            'upgrade',
96            'current_version|current-version',
97            'deployment_dir|deployment-dir=s',
98            'debug|d+','help|h|?','man|m');
99
100 pod2usage() if $options{help};
101 pod2usage({verbose=>2}) if $options{man};
102
103 $DEBUG = $options{debug};
104
105 my @USAGE_ERRORS;
106
107 my @exclusive_options = qw(install upgrade current_version install_version_storage);
108 if (1 < grep {exists $options{$_}} @exclusive_options) {
109       push @USAGE_ERRORS,"You must only give one of the ".
110           join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
111           " options";
112 }
113 if (not grep {exists $options{$_}} @exclusive_options) {
114     $options{current_version} = 1;
115 }
116
117 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
118
119 if (exists $options{sysconfdir}) {
120     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
121         delete $ENV{PGSYSCONFDIR};
122     } else {
123         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
124     }
125 }
126
127 if (not exists $options{dsn} or
128     not defined $options{dsn} or
129     not length $options{dsn}) {
130     $options{dsn} = 'dbi:Pg:service='.$options{service};
131 }
132
133
134 my $s = Debbugs::DB->connect($options{dsn}) or
135     die "Unable to connect to database";
136
137
138 if ($options{current_version}) {
139     print "The current database version is: ".$s->database_version."\n";
140     exit 0;
141 } elsif ($options{install}) {
142     $s->sql_file_in_txn($options{deployment_dir}.'/debbugs_schema.sql');
143 } elsif ($options{upgrade}) {
144     my @upgrades = $s->upgrades_to_run($options{deployment_dir});
145     for my $u_f (@upgrades) {
146         eval {
147             $s->sql_file_in_txn($u_f->file);
148         };
149         if ($@) {
150             print STDERR "Upgrade from $s->database_version to $u_f->version failed: $@";
151             exit 1;
152         }
153     }
154 } elsif ($options{install_version_storage}) {
155     # TODO Check if db_version already exists, and error out
156     $s->sql_file_in_txn($options{deployment_dir}.'/version_storage.sql');
157 }
158
159
160 __END__