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