]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-installsql
complete documentation for --upgrade
[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/). This directory
47 contains an upgrade subdirectory and scripts which perform the upgrade
48
49 =item B<--debug, -d>
50
51 Debug verbosity. (Default 0)
52
53 =item B<--help, -h>
54
55 Display brief usage information.
56
57 =item B<--man, -m>
58
59 Display this manual.
60
61 =back
62
63 =head1 EXAMPLES
64
65 debbugs-installsql
66
67 =cut
68
69
70 use vars qw($DEBUG);
71
72 # if we're running out of git, we want to use the git base directory as the
73 # first INC directory. If you're not running out of git, or someone has given a
74 # non-absolute INC, don't do that.
75 use FindBin;
76 use if (-d $FindBin::Bin.'/../.git/' && $INC[0] =~ m#^/#),
77     lib => $FindBin::Bin.'/../lib/';
78
79 use Debbugs::DB;
80 use JSON;
81
82 my %options = (debug           => 0,
83                help            => 0,
84                man             => 0,
85                overwrite_deployment => 0,
86                drop            => 0,
87                service         => 'debbugs',
88                deployment_dir  => '/usr/share/debbugs/sql',
89               );
90
91 GetOptions(\%options,
92            'service|s=s',
93            'dsn=s',
94            'sysconfdir|c=s',
95            'install',
96            'install_version_storage|install-version-storage',
97            'upgrade',
98            'current_version|current-version',
99            'deployment_dir|deployment-dir=s',
100            'debug|d+','help|h|?','man|m');
101
102 pod2usage() if $options{help};
103 pod2usage({verbose=>2}) if $options{man};
104
105 $DEBUG = $options{debug};
106
107 my @USAGE_ERRORS;
108
109 my @exclusive_options = qw(install upgrade current_version install_version_storage);
110 if (1 < grep {exists $options{$_}} @exclusive_options) {
111       push @USAGE_ERRORS,"You must only give one of the ".
112           join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
113           " options";
114 }
115 if (not grep {exists $options{$_}} @exclusive_options) {
116     $options{current_version} = 1;
117 }
118
119 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
120
121 if (exists $options{sysconfdir}) {
122     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
123         delete $ENV{PGSYSCONFDIR};
124     } else {
125         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
126     }
127 }
128
129 if (not exists $options{dsn} or
130     not defined $options{dsn} or
131     not length $options{dsn}) {
132     $options{dsn} = 'dbi:Pg:service='.$options{service};
133 }
134
135
136 my $s = Debbugs::DB->connect($options{dsn}) or
137     die "Unable to connect to database";
138
139
140 if ($options{current_version}) {
141     print "The current database version is: ".$s->db_version."\n";
142     exit 0;
143 } elsif ($options{install}) {
144     $s->sql_file_in_txn($options{deployment_dir}.'/debbugs_schema.sql');
145 } elsif ($options{upgrade}) {
146     my @upgrades = $s->upgrades_to_run($options{deployment_dir}.'/upgrade');
147     for my $u_f (@upgrades) {
148         eval {
149             package fake;
150             require $u_f->{file};
151             $s->txn_do(sub {fake::upgrade($s);
152                             $s->resultset('DbVersion')->
153                                 create({version => $u_f->{to},
154                                         metadata => JSON::encode_json({from => $u_f->{from}+0,
155                                                                        to => $u_f->{to}+0,
156                                                                        file => $u_f->{file},
157                                                                       }),
158                                        });
159                         });
160         };
161         if ($@) {
162             print STDERR "Upgrade from $s->database_version to $u_f->{to} failed: $@";
163             exit 1;
164         }
165     }
166 } elsif ($options{install_version_storage}) {
167     if ($s->db_version > 1) {
168         print STDERR "Version storage is already installed, current version is > 1\n";
169         exit 1;
170     }
171     # TODO Check if db_version already exists, and error out
172     $s->sql_file_in_txn($options{deployment_dir}.'/version_storage.sql');
173 }
174
175
176 __END__