5 # Debian-specific script to upgrade the database between releases
6 # Roland Mas <lolando@debian.org>
15 use vars qw/$dbh @reqlist $query/ ;
16 use vars qw/$sys_default_domain $sys_cvs_host $sys_download_host
17 $sys_shell_host $sys_users_host $sys_docs_host $sys_lists_host
18 $sys_dns1_host $sys_dns2_host $FTPINCOMING_DIR $FTPFILES_DIR
19 $sys_urlroot $sf_cache_dir $sys_name $sys_themeroot
20 $sys_news_group $sys_dbhost $sys_dbname $sys_dbuser $sys_dbpasswd
21 $sys_ldap_base_dn $sys_ldap_host $admin_login $admin_password
22 $server_admin $domain_name $newsadmin_groupid $statsadmin_groupid
24 use vars qw/$pluginname/ ;
26 sub is_lesser ( $$ ) ;
27 sub is_greater ( $$ ) ;
29 sub parse_sql_file ( $ ) ;
31 require ("/usr/lib/gforge/lib/include.pl") ; # Include a few predefined functions
32 require ("/usr/lib/gforge/lib/sqlparser.pm") ; # Our magic SQL parser
34 debug "You'll see some debugging info during this installation." ;
35 debug "Do not worry unless told otherwise." ;
39 # debug "Connected to the database OK." ;
41 $pluginname = "ldapextauth" ;
43 $dbh->{AutoCommit} = 0;
44 $dbh->{RaiseError} = 1;
46 my ($sth, @array, $version, $path, $target) ;
48 &create_metadata_table ("0") ;
52 $version = &get_db_version ;
54 if (is_lesser $version, $target) {
56 # "/usr/lib/gforge/plugins/$pluginname/lib/$pluginname-init.sql",
59 foreach my $file (@filelist) {
60 debug "Processing $file" ;
61 @reqlist = @{ &parse_sql_file ($file) } ;
63 foreach my $s (@reqlist) {
66 $sth = $dbh->prepare ($query) ;
73 &update_db_version ($target) ;
78 debug "It seems your database install/upgrade went well and smoothly. That's cool." ;
79 debug "Please enjoy using Debian GForge." ;
81 # There should be a commit at the end of every block above.
82 # If there is not, then it might be symptomatic of a problem.
83 # For safety, we roll back.
88 warn "Transaction aborted because $@" ;
89 debug "Transaction aborted because $@" ;
90 debug "Last SQL query was:\n$query\n(end of query)" ;
92 debug "Please report this bug on the Debian bug-tracking system." ;
93 debug "Please include the previous messages as well to help debugging." ;
94 debug "You should not worry too much about this," ;
95 debug "your DB is still in a consistent state and should be usable." ;
102 sub is_lesser ( $$ ) {
103 my $v1 = shift || 0 ;
104 my $v2 = shift || 0 ;
106 my $rc = system "dpkg --compare-versions $v1 lt $v2" ;
111 sub is_greater ( $$ ) {
112 my $v1 = shift || 0 ;
113 my $v2 = shift || 0 ;
115 my $rc = system "dpkg --compare-versions $v1 gt $v2" ;
123 print STDERR "$v\n" ;
126 sub create_metadata_table ( $ ) {
127 my $v = shift || "0" ;
128 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
129 # Do we have the metadata table?
131 $query = "SELECT count(*) FROM pg_class WHERE relname = '$tablename' and relkind = 'r'";
133 my $sth = $dbh->prepare ($query) ;
135 my @array = $sth->fetchrow_array () ;
138 # Let's create this table if we have it not
140 if ($array [0] == 0) {
141 debug "Creating $tablename table." ;
142 $query = "CREATE TABLE $tablename (key varchar primary key, value text not null)" ;
144 $sth = $dbh->prepare ($query) ;
149 $query = "SELECT count(*) FROM $tablename WHERE key = 'db-version'";
151 $sth = $dbh->prepare ($query) ;
153 @array = $sth->fetchrow_array () ;
156 # Empty table? We'll have to fill it up a bit
158 if ($array [0] == 0) {
159 debug "Inserting first data into $tablename table." ;
160 $query = "INSERT INTO $tablename (key, value) VALUES ('db-version', '$v')" ;
162 $sth = $dbh->prepare ($query) ;
168 sub update_db_version ( $ ) {
169 my $v = shift or die "Not enough arguments" ;
170 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
172 debug "Updating $tablename table." ;
173 $query = "UPDATE $tablename SET value = '$v' WHERE key = 'db-version'" ;
175 my $sth = $dbh->prepare ($query) ;
180 sub get_db_version () {
181 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
183 $query = "SELECT value FROM $tablename WHERE key = 'db-version'" ;
185 my $sth = $dbh->prepare ($query) ;
187 my @array = $sth->fetchrow_array () ;
190 my $version = $array [0] ;
195 sub drop_table_if_exists ( $ ) {
196 my $tname = shift or die "Not enough arguments" ;
197 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
198 my $sth = $dbh->prepare ($query) ;
200 my @array = $sth->fetchrow_array () ;
203 if ($array [0] != 0) {
204 # debug "Dropping table $tname" ;
205 $query = "DROP TABLE $tname" ;
207 $sth = $dbh->prepare ($query) ;
213 sub drop_sequence_if_exists ( $ ) {
214 my $sname = shift or die "Not enough arguments" ;
215 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
216 my $sth = $dbh->prepare ($query) ;
218 my @array = $sth->fetchrow_array () ;
221 if ($array [0] != 0) {
222 # debug "Dropping sequence $sname" ;
223 $query = "DROP SEQUENCE $sname" ;
225 $sth = $dbh->prepare ($query) ;
231 sub drop_index_if_exists ( $ ) {
232 my $iname = shift or die "Not enough arguments" ;
233 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
234 my $sth = $dbh->prepare ($query) ;
236 my @array = $sth->fetchrow_array () ;
239 if ($array [0] != 0) {
240 # debug "Dropping index $iname" ;
241 $query = "DROP INDEX $iname" ;
243 $sth = $dbh->prepare ($query) ;
249 sub drop_view_if_exists ( $ ) {
250 my $iname = shift or die "Not enough arguments" ;
251 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
252 my $sth = $dbh->prepare ($query) ;
254 my @array = $sth->fetchrow_array () ;
257 if ($array [0] != 0) {
258 # debug "Dropping view $iname" ;
259 $query = "DROP VIEW $iname" ;
261 $sth = $dbh->prepare ($query) ;
267 sub bump_sequence_to ( $$ ) {
268 my ($sth, @array, $seqname, $targetvalue) ;
271 $targetvalue = shift ;
274 $query = "select nextval ('$seqname')" ;
275 $sth = $dbh->prepare ($query) ;
277 @array = $sth->fetchrow_array () ;
279 } until $array[0] >= $targetvalue ;