]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-installsql
include function in instalsql for bin ver/src pkg linking
[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<--drop>
49
50 Drop tables before trying to create them. (Useful for --install, primarily).
51 Defaults to not drop tables for safety.
52
53 =item B<--debug, -d>
54
55 Debug verbosity. (Default 0)
56
57 =item B<--help, -h>
58
59 Display brief usage information.
60
61 =item B<--man, -m>
62
63 Display this manual.
64
65 =back
66
67 =head1 EXAMPLES
68
69 debbugs-installsql
70
71 =cut
72
73
74 use vars qw($DEBUG);
75
76 # if we're running out of git, we want to use the git base directory as the
77 # first INC directory. If you're not running out of git, or someone has given a
78 # non-absolute INC, don't do that.
79 use FindBin;
80 use if (-d $FindBin::Bin.'/../.git/' && $INC[0] =~ m#^/#),
81     lib => $FindBin::Bin.'/../';
82
83 use Debbugs::DB;
84 use Debbugs::DB::Util qw(prepare_execute);
85 use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
86
87 my %options = (debug           => 0,
88                help            => 0,
89                man             => 0,
90                overwrite_deployment => 0,
91                drop            => 0,
92                service         => 'debbugs',
93                deployment_dir  => '/usr/share/debbugs/sqldeployment',
94               );
95
96 GetOptions(\%options,
97            'service|s=s',
98            'dsn=s',
99            'sysconfdir|c=s',
100            'install',
101            'install_version_storage|install-version-storage',
102            'upgrade',
103            'drop',
104            'current_version|current-version',
105            'overwrite_deployment|overwrite-deployment|force_overwrite|force-overwrite',
106            'deployment_dir|deployment-dir=s',
107            'debug|d+','help|h|?','man|m');
108
109 pod2usage() if $options{help};
110 pod2usage({verbose=>2}) if $options{man};
111
112 $DEBUG = $options{debug};
113
114 my @USAGE_ERRORS;
115
116 my @exclusive_options = qw(install upgrade current_version install_version_storage);
117 if (1 < grep {exists $options{$_}} @exclusive_options) {
118       push @USAGE_ERRORS,"You must only give one of the ".
119           join(', ',map {s/_/-/g; "--".$_} @exclusive_options).
120           " options";
121 }
122 if (not grep {exists $options{$_}} @exclusive_options) {
123     $options{current_version} = 1;
124 }
125
126 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
127
128 if (exists $options{sysconfdir}) {
129     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
130         delete $ENV{PGSYSCONFDIR};
131     } else {
132         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
133     }
134 }
135
136 if (not exists $options{dsn} or
137     not defined $options{dsn} or
138     not length $options{dsn}) {
139     $options{dsn} = 'dbi:Pg:service='.$options{service};
140 }
141
142
143 my $schema = Debbugs::DB->connect($options{dsn}) or
144     die "Unable to connect to database";
145
146
147 my $dh = DH->new({schema => $schema,
148                   force_overwrite => $options{overwrite_deployment},
149                   script_directory => $options{deployment_dir},
150                   databases => 'PostgreSQL',
151                   sql_translator_args => {producer_args => {postgres_version => 8.1},
152                                           add_drop_table => $options{drop},
153                                          },
154                  });
155
156 if ($options{current_version}) {
157     print "The current database version is: ".$dh->database_version."\n";
158     exit 0;
159 } elsif ($options{install}) {
160     $dh->prepare_install;
161     $schema->storage->
162         dbh_do(sub {my ($s,$dbh) = @_;
163                     prepare_execute($dbh,<<'SQL');});
164 CREATE OR REPLACE FUNCTION bin_ver_to_src_pkg(bin_ver INT) RETURNS INT
165   AS $src_pkg_from_bin_ver$
166   DECLARE
167   src_pkg int;
168   BEGIN
169         SELECT sv.src_pkg INTO STRICT src_pkg
170                FROM bin_ver bv JOIN src_ver sv ON bv.src_ver=sv.id
171                WHERE bv.id=bin_ver;
172         RETURN src_pkg;
173   END
174   $src_pkg_from_bin_ver$ LANGUAGE plpgsql;
175
176 CREATE OR REPLACE FUNCTION src_ver_to_src_pkg(src_ver INT) RETURNS INT
177   AS $src_ver_to_src_pkg$
178   DECLARE
179   src_pkg int;
180   BEGIN
181         SELECT sv.src_pkg INTO STRICT src_pkg
182                FROM src_ver sv WHERE sv.id=src_ver;
183         RETURN src_pkg;
184   END
185   $src_ver_to_src_pkg$ LANGUAGE plpgsql;
186
187 CREATE OR REPLACE FUNCTION update_bin_pkg_src_pkg_bin_ver () RETURNS TRIGGER
188   AS $update_bin_pkg_src_pkg_bin_ver$
189   DECLARE
190   src_ver_rows integer;
191   BEGIN
192   IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE' )  THEN
193      -- if there is still a bin_ver with this src_pkg, then do nothing
194      PERFORM * FROM bin_ver bv JOIN src_ver sv ON bv.src_ver = sv.id
195             WHERE sv.id = OLD.src_ver LIMIT 2;
196      GET DIAGNOSTICS src_ver_rows = ROW_COUNT;
197      IF (src_ver_rows <= 1) THEN
198         DELETE FROM bin_pkg_src_pkg
199                WHERE bin_pkg=OLD.bin_pkg AND
200                      src_pkg=src_ver_to_src_pkg(OLD.src_ver);
201      END IF;
202   END IF;
203   IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
204      BEGIN
205      INSERT INTO bin_pkg_src_pkg (bin_pkg,src_pkg)
206         VALUES (NEW.bin_pkg,src_ver_to_src_pkg(NEW.src_ver))
207         ON CONFLICT (bin_pkg,src_pkg) DO NOTHING;
208      END;
209   END IF;
210   RETURN NULL;
211   END
212   $update_bin_pkg_src_pkg_bin_ver$ LANGUAGE plpgsql;
213 SQL
214     $dh->install;
215     ## this is lame, but because the current release of DeploymentHandler does
216     ## not support WHERE or quoted indexes properly (fixed in git), we create
217     ## these indexes manually here.
218     $schema->storage->
219         dbh_do(sub{my ($s,$dbh) = @_;
220                    prepare_execute($dbh,<<SQL);
221
222 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
223  bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
224 CREATE UNIQUE INDEX bug_status_cache_bug_suite_idx ON
225  bug_status_cache(bug,suite) WHERE arch is NULL;
226 CREATE UNIQUE INDEX bug_status_cache_bug_idx ON
227  bug_status_cache(bug) WHERE arch is NULL AND suite IS NULL;
228 SQL
229                    });
230 } elsif ($options{upgrade}) {
231     $dh->prepare_deploy;
232     $dh->prepare_upgrade;
233     $dh->upgrade;
234 } elsif ($options{install_version_storage}) {
235     $dh->prepare_version_storage_install;
236     $dh->install_version_storage;
237 }
238
239
240 __END__