]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-installsql
Add Debbugs::BugWalker to abstract out bug-walking code in
[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 aliased 'DBIx::Class::DeploymentHandler' => 'DH';
73
74 my %options = (debug           => 0,
75                help            => 0,
76                man             => 0,
77                developer_prepare => 0,
78                overwrite_deployment => 0,
79                service         => 'debbugs',
80                deployment_dir  => '/usr/share/debbugs/sqldeployment',
81               );
82
83 GetOptions(\%options,
84            'service|s',
85            'sysconfdir|c',
86            'install',
87            'upgrade',
88            'current_version|current-version',
89            'developer_prepare|developer-prepare',
90            'overwrite_deployment|overwrite-deployment',
91            'deployment_dir|deployment-dir=s',
92            'debug|d+','help|h|?','man|m');
93
94 pod2usage() if $options{help};
95 pod2usage({verbose=>2}) if $options{man};
96
97 $DEBUG = $options{debug};
98
99 my @USAGE_ERRORS;
100
101 my @exclusive_options = qw(install upgrade current_version);
102 if (1 < grep {exists $options{$_}} @exclusive_options) {
103       push @USAGE_ERRORS,"You must only give one of the ".
104           join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
105           " options";
106 }
107 if (not grep {exists $options{$_}} @exclusive_options) {
108     $options{current_version} = 1;
109 }
110
111 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
112
113 if (exists $options{sysconfdir}) {
114     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
115         delete $ENV{PGSYSCONFDIR};
116     } else {
117         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
118     }
119 }
120
121
122 my $schema = Debbugs::DB->connect('dbi:Pg:service='.$options{service}) or
123     die "Unable to connect to database";
124
125
126 my $dh = DH->new({schema => $schema,
127                   force_overwrite => $options{overwrite_deployment},
128                   script_directory => $options{deployment_dir},
129                   databases => 'PostgreSQL'
130                  });
131
132 if ($options{current_version}) {
133     print "The current database version is: ".$dh->database_version."\n";
134     exit 0;
135 } elsif ($options{install}) {
136     if ($options{developer_prepare}) {
137         $dh->prepare_install;
138     } else {
139         $dh->install;
140     }
141 } elsif ($options{upgrade}) {
142     if ($options{developer_prepare}) {
143         $dh->prepare_deploy;
144         $dh->prepare_upgrade;
145     } else {
146         $dh->upgrade;
147     }
148 }
149
150
151 __END__