3 # $Id: db-upgrade.pl,v 1.1.1.1 2005/09/22 17:32:56 rhertzog Exp $
5 # Debian-specific script to upgrade the database between releases
6 # Roland Mas <lolando@debian.org>
8 # Adapted by Raphael Hertzog <hertzog@debian.org> for extratabs
17 use vars qw/$dbh @reqlist $query/ ;
18 use vars qw/$sys_default_domain $sys_cvs_host $sys_download_host
19 $sys_shell_host $sys_users_host $sys_docs_host $sys_lists_host
20 $sys_dns1_host $sys_dns2_host $FTPINCOMING_DIR $FTPFILES_DIR
21 $sys_urlroot $sf_cache_dir $sys_name $sys_themeroot
22 $sys_news_group $sys_dbhost $sys_dbname $sys_dbuser $sys_dbpasswd
23 $sys_ldap_base_dn $sys_ldap_host $admin_login $admin_password
24 $domain_name $newsadmin_groupid $statsadmin_groupid
26 use vars qw/$pluginname/ ;
28 sub is_lesser ( $$ ) ;
29 sub is_greater ( $$ ) ;
31 sub parse_sql_file ( $ ) ;
33 require ("/usr/share/gforge/lib/include.pl") ; # Include a few predefined functions
34 require ("/usr/share/gforge/lib/sqlparser.pm") ; # Our magic SQL parser
36 debug "You'll see some debugging info during this installation." ;
37 debug "Do not worry unless told otherwise." ;
41 # debug "Connected to the database OK." ;
43 $pluginname = "extratabs" ;
45 $dbh->{AutoCommit} = 0;
46 $dbh->{RaiseError} = 1;
48 my ($sth, @array, $version, $path, $target) ;
50 &create_metadata_table ("0") ;
52 $version = &get_db_version ;
54 if (is_lesser $version, $target) {
55 my @filelist = ( "/usr/share/gforge/plugins/$pluginname/db/$pluginname-init.sql" ) ;
57 foreach my $file (@filelist) {
58 debug "Processing $file" ;
59 @reqlist = @{ &parse_sql_file ($file) } ;
61 foreach my $s (@reqlist) {
64 $sth = $dbh->prepare ($query) ;
71 &update_db_version ($target) ;
77 $version = &get_db_version ;
79 if (is_lesser $version, $target) {
80 my @filelist = ( "/usr/share/gforge/plugins/$pluginname/db/20101203-add_type_for_iframe.sql" ) ;
82 foreach my $file (@filelist) {
83 debug "Processing $file" ;
84 @reqlist = @{ &parse_sql_file ($file) } ;
86 foreach my $s (@reqlist) {
89 $sth = $dbh->prepare ($query) ;
96 &update_db_version ($target) ;
101 debug "It seems your database install/upgrade went well and smoothly. That's cool." ;
102 debug "Please enjoy using Debian GForge." ;
104 # There should be a commit at the end of every block above.
105 # If there is not, then it might be symptomatic of a problem.
106 # For safety, we roll back.
111 warn "Transaction aborted because $@" ;
112 debug "Transaction aborted because $@" ;
113 debug "Last SQL query was:\n$query\n(end of query)" ;
115 debug "Please report this bug on the Debian bug-tracking system." ;
116 debug "Please include the previous messages as well to help debugging." ;
117 debug "You should not worry too much about this," ;
118 debug "your DB is still in a consistent state and should be usable." ;
125 sub is_lesser ( $$ ) {
126 my $v1 = shift || 0 ;
127 my $v2 = shift || 0 ;
129 my $rc = system "dpkg --compare-versions $v1 lt $v2" ;
134 sub is_greater ( $$ ) {
135 my $v1 = shift || 0 ;
136 my $v2 = shift || 0 ;
138 my $rc = system "dpkg --compare-versions $v1 gt $v2" ;
146 print STDERR "$v\n" ;
149 sub create_metadata_table ( $ ) {
150 my $v = shift || "0" ;
151 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
152 # Do we have the metadata table?
154 $query = "SELECT count(*) FROM pg_class WHERE relname = '$tablename' and relkind = 'r'";
156 my $sth = $dbh->prepare ($query) ;
158 my @array = $sth->fetchrow_array () ;
161 # Let's create this table if we have it not
163 if ($array [0] == 0) {
164 debug "Creating $tablename table." ;
165 $query = "CREATE TABLE $tablename (key varchar primary key, value text not null)" ;
167 $sth = $dbh->prepare ($query) ;
172 $query = "SELECT count(*) FROM $tablename WHERE key = 'db-version'";
174 $sth = $dbh->prepare ($query) ;
176 @array = $sth->fetchrow_array () ;
179 # Empty table? We'll have to fill it up a bit
181 if ($array [0] == 0) {
182 debug "Inserting first data into $tablename table." ;
183 $query = "INSERT INTO $tablename (key, value) VALUES ('db-version', '$v')" ;
185 $sth = $dbh->prepare ($query) ;
191 sub update_db_version ( $ ) {
192 my $v = shift or die "Not enough arguments" ;
193 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
195 debug "Updating $tablename table." ;
196 $query = "UPDATE $tablename SET value = '$v' WHERE key = 'db-version'" ;
198 my $sth = $dbh->prepare ($query) ;
203 sub get_db_version () {
204 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
206 $query = "SELECT value FROM $tablename WHERE key = 'db-version'" ;
208 my $sth = $dbh->prepare ($query) ;
210 my @array = $sth->fetchrow_array () ;
213 my $version = $array [0] ;
218 sub drop_table_if_exists ( $ ) {
219 my $tname = shift or die "Not enough arguments" ;
220 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
221 my $sth = $dbh->prepare ($query) ;
223 my @array = $sth->fetchrow_array () ;
226 if ($array [0] != 0) {
227 # debug "Dropping table $tname" ;
228 $query = "DROP TABLE $tname" ;
230 $sth = $dbh->prepare ($query) ;
236 sub drop_sequence_if_exists ( $ ) {
237 my $sname = shift or die "Not enough arguments" ;
238 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
239 my $sth = $dbh->prepare ($query) ;
241 my @array = $sth->fetchrow_array () ;
244 if ($array [0] != 0) {
245 # debug "Dropping sequence $sname" ;
246 $query = "DROP SEQUENCE $sname" ;
248 $sth = $dbh->prepare ($query) ;
254 sub drop_index_if_exists ( $ ) {
255 my $iname = shift or die "Not enough arguments" ;
256 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
257 my $sth = $dbh->prepare ($query) ;
259 my @array = $sth->fetchrow_array () ;
262 if ($array [0] != 0) {
263 # debug "Dropping index $iname" ;
264 $query = "DROP INDEX $iname" ;
266 $sth = $dbh->prepare ($query) ;
272 sub drop_view_if_exists ( $ ) {
273 my $iname = shift or die "Not enough arguments" ;
274 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
275 my $sth = $dbh->prepare ($query) ;
277 my @array = $sth->fetchrow_array () ;
280 if ($array [0] != 0) {
281 # debug "Dropping view $iname" ;
282 $query = "DROP VIEW $iname" ;
284 $sth = $dbh->prepare ($query) ;
290 sub bump_sequence_to ( $$ ) {
291 my ($sth, @array, $seqname, $targetvalue) ;
294 $targetvalue = shift ;
297 $query = "select nextval ('$seqname')" ;
298 $sth = $dbh->prepare ($query) ;
300 @array = $sth->fetchrow_array () ;
302 } until $array[0] >= $targetvalue ;