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 $sys_download_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
20 $server_admin $domain_name $newsadmin_groupid $statsadmin_groupid
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 # $version = &get_db_version ;
74 # if (is_lesser $version, $target) {
75 # debug "Adding local data." ;
77 # do "/etc/gforge/local.pl" or die "Cannot read /etc/gforge/local.pl" ;
79 # my $ip_address = qx/host $domain_name | awk '{print \}'/ ;
82 # "INSERT INTO plugin_".$pluginname."_sample_data (domain, ip_address) VALUES ('$domain_name', '$ip_address')",
85 # foreach my $s (@reqlist) {
88 # $sth = $dbh->prepare ($query) ;
94 # &update_db_version ($target) ;
95 # debug "Committing." ;
99 debug "It seems your database install/upgrade went well and smoothly. That's cool." ;
100 debug "Please enjoy using Debian FusionForge." ;
102 # There should be a commit at the end of every block above.
103 # If there is not, then it might be symptomatic of a problem.
104 # For safety, we roll back.
109 warn "Transaction aborted because $@" ;
110 debug "Transaction aborted because $@" ;
111 debug "Last SQL query was:\n$query\n(end of query)" ;
113 debug "Please report this bug on the Debian bug-tracking system." ;
114 debug "Please include the previous messages as well to help debugging." ;
115 debug "You should not worry too much about this," ;
116 debug "your DB is still in a consistent state and should be usable." ;
123 sub is_lesser ( $$ ) {
124 my $v1 = shift || 0 ;
125 my $v2 = shift || 0 ;
127 my $rc = system "dpkg --compare-versions $v1 lt $v2" ;
132 sub is_greater ( $$ ) {
133 my $v1 = shift || 0 ;
134 my $v2 = shift || 0 ;
136 my $rc = system "dpkg --compare-versions $v1 gt $v2" ;
144 print STDERR "$v\n" ;
147 sub create_metadata_table ( $ ) {
148 my $v = shift || "0" ;
149 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
150 # Do we have the metadata table?
152 $query = "SELECT count(*) FROM pg_class WHERE relname = '$tablename' and relkind = 'r'";
154 my $sth = $dbh->prepare ($query) ;
156 my @array = $sth->fetchrow_array () ;
159 # Let's create this table if we have it not
161 if ($array [0] == 0) {
162 debug "Creating $tablename table." ;
163 $query = "CREATE TABLE $tablename (key varchar primary key, value text not null)" ;
165 $sth = $dbh->prepare ($query) ;
170 $query = "SELECT count(*) FROM $tablename WHERE key = 'db-version'";
172 $sth = $dbh->prepare ($query) ;
174 @array = $sth->fetchrow_array () ;
177 # Empty table? We'll have to fill it up a bit
179 if ($array [0] == 0) {
180 debug "Inserting first data into $tablename table." ;
181 $query = "INSERT INTO $tablename (key, value) VALUES ('db-version', '$v')" ;
183 $sth = $dbh->prepare ($query) ;
189 sub update_db_version ( $ ) {
190 my $v = shift or die "Not enough arguments" ;
191 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
193 debug "Updating $tablename table." ;
194 $query = "UPDATE $tablename SET value = '$v' WHERE key = 'db-version'" ;
196 my $sth = $dbh->prepare ($query) ;
201 sub get_db_version () {
202 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
204 $query = "SELECT value FROM $tablename WHERE key = 'db-version'" ;
206 my $sth = $dbh->prepare ($query) ;
208 my @array = $sth->fetchrow_array () ;
211 my $version = $array [0] ;
216 sub drop_table_if_exists ( $ ) {
217 my $tname = shift or die "Not enough arguments" ;
218 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
219 my $sth = $dbh->prepare ($query) ;
221 my @array = $sth->fetchrow_array () ;
224 if ($array [0] != 0) {
225 # debug "Dropping table $tname" ;
226 $query = "DROP TABLE $tname" ;
228 $sth = $dbh->prepare ($query) ;
234 sub drop_sequence_if_exists ( $ ) {
235 my $sname = shift or die "Not enough arguments" ;
236 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
237 my $sth = $dbh->prepare ($query) ;
239 my @array = $sth->fetchrow_array () ;
242 if ($array [0] != 0) {
243 # debug "Dropping sequence $sname" ;
244 $query = "DROP SEQUENCE $sname" ;
246 $sth = $dbh->prepare ($query) ;
252 sub drop_index_if_exists ( $ ) {
253 my $iname = shift or die "Not enough arguments" ;
254 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
255 my $sth = $dbh->prepare ($query) ;
257 my @array = $sth->fetchrow_array () ;
260 if ($array [0] != 0) {
261 # debug "Dropping index $iname" ;
262 $query = "DROP INDEX $iname" ;
264 $sth = $dbh->prepare ($query) ;
270 sub drop_view_if_exists ( $ ) {
271 my $iname = shift or die "Not enough arguments" ;
272 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
273 my $sth = $dbh->prepare ($query) ;
275 my @array = $sth->fetchrow_array () ;
278 if ($array [0] != 0) {
279 # debug "Dropping view $iname" ;
280 $query = "DROP VIEW $iname" ;
282 $sth = $dbh->prepare ($query) ;
288 sub bump_sequence_to ( $$ ) {
289 my ($sth, @array, $seqname, $targetvalue) ;
292 $targetvalue = shift ;
295 $query = "select nextval ('$seqname')" ;
296 $sth = $dbh->prepare ($query) ;
298 @array = $sth->fetchrow_array () ;
300 } until $array[0] >= $targetvalue ;