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
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
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 = "headermenu" ;
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",
56 "/usr/share/gforge/plugins/$pluginname/db/20120930-addoutermenusupport.sql",
57 "/usr/share/gforge/plugins/$pluginname/db/20121231-reorderentry.sql",
58 "/usr/share/gforge/plugins/$pluginname/db/20130120-addprojectcolumn.sql",
61 foreach my $file (@filelist) {
62 debug "Processing $file" ;
63 @reqlist = @{ &parse_sql_file ($file) } ;
65 foreach my $s (@reqlist) {
68 $sth = $dbh->prepare ($query) ;
75 &update_db_version ($target) ;
81 debug "It seems your database install/upgrade went well and smoothly. That's cool." ;
82 debug "Please enjoy using Debian GForge." ;
84 # There should be a commit at the end of every block above.
85 # If there is not, then it might be symptomatic of a problem.
86 # For safety, we roll back.
91 warn "Transaction aborted because $@" ;
92 debug "Transaction aborted because $@" ;
93 debug "Last SQL query was:\n$query\n(end of query)" ;
95 debug "Please report this bug on the Debian bug-tracking system." ;
96 debug "Please include the previous messages as well to help debugging." ;
97 debug "You should not worry too much about this," ;
98 debug "your DB is still in a consistent state and should be usable." ;
105 sub is_lesser ( $$ ) {
106 my $v1 = shift || 0 ;
107 my $v2 = shift || 0 ;
109 my $rc = system "dpkg --compare-versions $v1 lt $v2" ;
114 sub is_greater ( $$ ) {
115 my $v1 = shift || 0 ;
116 my $v2 = shift || 0 ;
118 my $rc = system "dpkg --compare-versions $v1 gt $v2" ;
126 print STDERR "$v\n" ;
129 sub create_metadata_table ( $ ) {
130 my $v = shift || "0" ;
131 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
132 # Do we have the metadata table?
134 $query = "SELECT count(*) FROM pg_class WHERE relname = '$tablename' and relkind = 'r'";
136 my $sth = $dbh->prepare ($query) ;
138 my @array = $sth->fetchrow_array () ;
141 # Let's create this table if we have it not
143 if ($array [0] == 0) {
144 debug "Creating $tablename table." ;
145 $query = "CREATE TABLE $tablename (key varchar primary key, value text not null)" ;
147 $sth = $dbh->prepare ($query) ;
152 $query = "SELECT count(*) FROM $tablename WHERE key = 'db-version'";
154 $sth = $dbh->prepare ($query) ;
156 @array = $sth->fetchrow_array () ;
159 # Empty table? We'll have to fill it up a bit
161 if ($array [0] == 0) {
162 debug "Inserting first data into $tablename table." ;
163 $query = "INSERT INTO $tablename (key, value) VALUES ('db-version', '$v')" ;
165 $sth = $dbh->prepare ($query) ;
171 sub update_db_version ( $ ) {
172 my $v = shift or die "Not enough arguments" ;
173 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
175 debug "Updating $tablename table." ;
176 $query = "UPDATE $tablename SET value = '$v' WHERE key = 'db-version'" ;
178 my $sth = $dbh->prepare ($query) ;
183 sub get_db_version () {
184 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
186 $query = "SELECT value FROM $tablename WHERE key = 'db-version'" ;
188 my $sth = $dbh->prepare ($query) ;
190 my @array = $sth->fetchrow_array () ;
193 my $version = $array [0] ;
198 sub drop_table_if_exists ( $ ) {
199 my $tname = shift or die "Not enough arguments" ;
200 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
201 my $sth = $dbh->prepare ($query) ;
203 my @array = $sth->fetchrow_array () ;
206 if ($array [0] != 0) {
207 # debug "Dropping table $tname" ;
208 $query = "DROP TABLE $tname" ;
210 $sth = $dbh->prepare ($query) ;
216 sub drop_sequence_if_exists ( $ ) {
217 my $sname = shift or die "Not enough arguments" ;
218 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
219 my $sth = $dbh->prepare ($query) ;
221 my @array = $sth->fetchrow_array () ;
224 if ($array [0] != 0) {
225 # debug "Dropping sequence $sname" ;
226 $query = "DROP SEQUENCE $sname" ;
228 $sth = $dbh->prepare ($query) ;
234 sub drop_index_if_exists ( $ ) {
235 my $iname = shift or die "Not enough arguments" ;
236 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
237 my $sth = $dbh->prepare ($query) ;
239 my @array = $sth->fetchrow_array () ;
242 if ($array [0] != 0) {
243 # debug "Dropping index $iname" ;
244 $query = "DROP INDEX $iname" ;
246 $sth = $dbh->prepare ($query) ;
252 sub drop_view_if_exists ( $ ) {
253 my $iname = shift or die "Not enough arguments" ;
254 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
255 my $sth = $dbh->prepare ($query) ;
257 my @array = $sth->fetchrow_array () ;
260 if ($array [0] != 0) {
261 # debug "Dropping view $iname" ;
262 $query = "DROP VIEW $iname" ;
264 $sth = $dbh->prepare ($query) ;
270 sub bump_sequence_to ( $$ ) {
271 my ($sth, @array, $seqname, $targetvalue) ;
274 $targetvalue = shift ;
277 $query = "select nextval ('$seqname')" ;
278 $sth = $dbh->prepare ($query) ;
280 @array = $sth->fetchrow_array () ;
282 } until $array[0] >= $targetvalue ;