3 # Debian-specific script to upgrade the database between releases
4 # Roland Mas <lolando@debian.org>
13 use vars qw/$dbh @reqlist $query/ ;
14 use vars qw/$sys_default_domain $sys_cvs_host
15 $sys_shell_host $sys_users_host $sys_docs_host $sys_lists_host
16 $sys_dns1_host $sys_dns2_host $FTPINCOMING_DIR $FTPFILES_DIR
17 $sys_urlroot $sf_cache_dir $sys_name $sys_themeroot
18 $sys_news_group $sys_dbhost $sys_dbname $sys_dbuser $sys_dbpasswd
19 $sys_ldap_base_dn $sys_ldap_host $admin_login $admin_password
22 use vars qw/$pluginname/ ;
24 sub is_lesser ( $$ ) ;
25 sub is_greater ( $$ ) ;
27 sub parse_sql_file ( $ ) ;
29 require ("/usr/share/gforge/lib/include.pl") ; # Include a few predefined functions
30 require ("/usr/share/gforge/lib/sqlparser.pm") ; # Our magic SQL parser
32 debug "You'll see some debugging info during this installation." ;
33 debug "Do not worry unless told otherwise." ;
37 # debug "Connected to the database OK." ;
39 $pluginname = "forumml" ;
41 $dbh->{AutoCommit} = 0;
42 $dbh->{RaiseError} = 1;
44 my ($sth, @array, $version, $path, $target) ;
46 &create_metadata_table ("0") ;
48 $version = &get_db_version ;
50 if (is_lesser $version, $target) {
51 my @filelist = ( "/usr/share/gforge/plugins/$pluginname/db/$pluginname-init.sql" ) ;
53 foreach my $file (@filelist) {
54 debug "Processing $file" ;
55 @reqlist = @{ &parse_sql_file ($file) } ;
57 foreach my $s (@reqlist) {
60 $sth = $dbh->prepare ($query) ;
67 &update_db_version ($target) ;
72 debug "It seems your database install/upgrade went well and smoothly. That's cool." ;
73 debug "Please enjoy using Debian FusionForge." ;
75 # There should be a commit at the end of every block above.
76 # If there is not, then it might be symptomatic of a problem.
77 # For safety, we roll back.
82 warn "Transaction aborted because $@" ;
83 debug "Transaction aborted because $@" ;
84 debug "Last SQL query was:\n$query\n(end of query)" ;
86 debug "Please report this bug on the Debian bug-tracking system." ;
87 debug "Please include the previous messages as well to help debugging." ;
88 debug "You should not worry too much about this," ;
89 debug "your DB is still in a consistent state and should be usable." ;
96 sub is_lesser ( $$ ) {
100 my $rc = system "dpkg --compare-versions $v1 lt $v2" ;
105 sub is_greater ( $$ ) {
106 my $v1 = shift || 0 ;
107 my $v2 = shift || 0 ;
109 my $rc = system "dpkg --compare-versions $v1 gt $v2" ;
117 print STDERR "$v\n" ;
120 sub create_metadata_table ( $ ) {
121 my $v = shift || "0" ;
122 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
123 # Do we have the metadata table?
125 $query = "SELECT count(*) FROM pg_class WHERE relname = '$tablename' and relkind = 'r'";
127 my $sth = $dbh->prepare ($query) ;
129 my @array = $sth->fetchrow_array () ;
132 # Let's create this table if we have it not
134 if ($array [0] == 0) {
135 debug "Creating $tablename table." ;
136 $query = "CREATE TABLE $tablename (key varchar primary key, value text not null)" ;
138 $sth = $dbh->prepare ($query) ;
143 $query = "SELECT count(*) FROM $tablename WHERE key = 'db-version'";
145 $sth = $dbh->prepare ($query) ;
147 @array = $sth->fetchrow_array () ;
150 # Empty table? We'll have to fill it up a bit
152 if ($array [0] == 0) {
153 debug "Inserting first data into $tablename table." ;
154 $query = "INSERT INTO $tablename (key, value) VALUES ('db-version', '$v')" ;
156 $sth = $dbh->prepare ($query) ;
162 sub update_db_version ( $ ) {
163 my $v = shift or die "Not enough arguments" ;
164 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
166 debug "Updating $tablename table." ;
167 $query = "UPDATE $tablename SET value = '$v' WHERE key = 'db-version'" ;
169 my $sth = $dbh->prepare ($query) ;
174 sub get_db_version () {
175 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
177 $query = "SELECT value FROM $tablename WHERE key = 'db-version'" ;
179 my $sth = $dbh->prepare ($query) ;
181 my @array = $sth->fetchrow_array () ;
184 my $version = $array [0] ;
189 sub drop_table_if_exists ( $ ) {
190 my $tname = shift or die "Not enough arguments" ;
191 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
192 my $sth = $dbh->prepare ($query) ;
194 my @array = $sth->fetchrow_array () ;
197 if ($array [0] != 0) {
198 # debug "Dropping table $tname" ;
199 $query = "DROP TABLE $tname" ;
201 $sth = $dbh->prepare ($query) ;
207 sub drop_sequence_if_exists ( $ ) {
208 my $sname = shift or die "Not enough arguments" ;
209 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
210 my $sth = $dbh->prepare ($query) ;
212 my @array = $sth->fetchrow_array () ;
215 if ($array [0] != 0) {
216 # debug "Dropping sequence $sname" ;
217 $query = "DROP SEQUENCE $sname" ;
219 $sth = $dbh->prepare ($query) ;
225 sub drop_index_if_exists ( $ ) {
226 my $iname = shift or die "Not enough arguments" ;
227 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
228 my $sth = $dbh->prepare ($query) ;
230 my @array = $sth->fetchrow_array () ;
233 if ($array [0] != 0) {
234 # debug "Dropping index $iname" ;
235 $query = "DROP INDEX $iname" ;
237 $sth = $dbh->prepare ($query) ;
243 sub drop_view_if_exists ( $ ) {
244 my $iname = shift or die "Not enough arguments" ;
245 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
246 my $sth = $dbh->prepare ($query) ;
248 my @array = $sth->fetchrow_array () ;
251 if ($array [0] != 0) {
252 # debug "Dropping view $iname" ;
253 $query = "DROP VIEW $iname" ;
255 $sth = $dbh->prepare ($query) ;
261 sub bump_sequence_to ( $$ ) {
262 my ($sth, @array, $seqname, $targetvalue) ;
265 $targetvalue = shift ;
268 $query = "select nextval ('$seqname')" ;
269 $sth = $dbh->prepare ($query) ;
271 @array = $sth->fetchrow_array () ;
273 } until $array[0] >= $targetvalue ;