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>
15 use vars qw/$dbh @reqlist $query/ ;
16 use vars qw/$sys_default_domain $sys_cvs_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
24 use vars qw/$pluginname/ ;
26 sub is_lesser ( $$ ) ;
27 sub is_greater ( $$ ) ;
29 sub parse_sql_file ( $ ) ;
31 require ("/usr/share/gforge/lib/include.pl") ; # Include a few predefined functions
32 require ("/usr/share/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 = "scmgit" ;
43 $dbh->{AutoCommit} = 0;
44 $dbh->{RaiseError} = 1;
46 my ($sth, @array, $version, $path, $target) ;
48 &create_metadata_table ("0") ;
50 $version = &get_db_version ;
52 if (is_lesser $version, $target) {
53 my @filelist = ( "/usr/share/gforge/plugins/$pluginname/lib/$pluginname-init.sql" ) ;
55 foreach my $file (@filelist) {
56 debug "Processing $file" ;
57 @reqlist = @{ &parse_sql_file ($file) } ;
59 foreach my $s (@reqlist) {
62 $sth = $dbh->prepare ($query) ;
69 &update_db_version ($target) ;
74 &update_with_sql("/usr/share/gforge/plugins/$pluginname/lib/20121019-multiple-repos.sql","0.2");
76 debug "It seems your database install/upgrade went well and smoothly. That's cool." ;
77 debug "Please enjoy using Debian GForge." ;
79 # There should be a commit at the end of every block above.
80 # If there is not, then it might be symptomatic of a problem.
81 # For safety, we roll back.
86 warn "Transaction aborted because $@" ;
87 debug "Transaction aborted because $@" ;
88 debug "Last SQL query was:\n$query\n(end of query)" ;
90 debug "Please report this bug on the Debian bug-tracking system." ;
91 debug "Please include the previous messages as well to help debugging." ;
92 debug "You should not worry too much about this," ;
93 debug "your DB is still in a consistent state and should be usable." ;
100 sub is_lesser ( $$ ) {
101 my $v1 = shift || 0 ;
102 my $v2 = shift || 0 ;
104 my $rc = system "dpkg --compare-versions $v1 lt $v2" ;
109 sub is_greater ( $$ ) {
110 my $v1 = shift || 0 ;
111 my $v2 = shift || 0 ;
113 my $rc = system "dpkg --compare-versions $v1 gt $v2" ;
121 print STDERR "$v\n" ;
124 sub create_metadata_table ( $ ) {
125 my $v = shift || "0" ;
126 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
127 # Do we have the metadata table?
129 $query = "SELECT count(*) FROM pg_class WHERE relname = '$tablename' and relkind = 'r'";
131 my $sth = $dbh->prepare ($query) ;
133 my @array = $sth->fetchrow_array () ;
136 # Let's create this table if we have it not
138 if ($array [0] == 0) {
139 debug "Creating $tablename table." ;
140 $query = "CREATE TABLE $tablename (key varchar primary key, value text not null)" ;
142 $sth = $dbh->prepare ($query) ;
147 $query = "SELECT count(*) FROM $tablename WHERE key = 'db-version'";
149 $sth = $dbh->prepare ($query) ;
151 @array = $sth->fetchrow_array () ;
154 # Empty table? We'll have to fill it up a bit
156 if ($array [0] == 0) {
157 debug "Inserting first data into $tablename table." ;
158 $query = "INSERT INTO $tablename (key, value) VALUES ('db-version', '$v')" ;
160 $sth = $dbh->prepare ($query) ;
166 sub update_db_version ( $ ) {
167 my $v = shift or die "Not enough arguments" ;
168 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
170 debug "Updating $tablename table." ;
171 $query = "UPDATE $tablename SET value = '$v' WHERE key = 'db-version'" ;
173 my $sth = $dbh->prepare ($query) ;
178 sub get_db_version () {
179 my $tablename = "plugin_" .$pluginname . "_meta_data" ;
181 $query = "SELECT value FROM $tablename WHERE key = 'db-version'" ;
183 my $sth = $dbh->prepare ($query) ;
185 my @array = $sth->fetchrow_array () ;
188 my $version = $array [0] ;
193 sub drop_table_if_exists ( $ ) {
194 my $tname = shift or die "Not enough arguments" ;
195 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
196 my $sth = $dbh->prepare ($query) ;
198 my @array = $sth->fetchrow_array () ;
201 if ($array [0] != 0) {
202 # debug "Dropping table $tname" ;
203 $query = "DROP TABLE $tname" ;
205 $sth = $dbh->prepare ($query) ;
211 sub drop_sequence_if_exists ( $ ) {
212 my $sname = shift or die "Not enough arguments" ;
213 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
214 my $sth = $dbh->prepare ($query) ;
216 my @array = $sth->fetchrow_array () ;
219 if ($array [0] != 0) {
220 # debug "Dropping sequence $sname" ;
221 $query = "DROP SEQUENCE $sname" ;
223 $sth = $dbh->prepare ($query) ;
229 sub drop_index_if_exists ( $ ) {
230 my $iname = shift or die "Not enough arguments" ;
231 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
232 my $sth = $dbh->prepare ($query) ;
234 my @array = $sth->fetchrow_array () ;
237 if ($array [0] != 0) {
238 # debug "Dropping index $iname" ;
239 $query = "DROP INDEX $iname" ;
241 $sth = $dbh->prepare ($query) ;
247 sub drop_view_if_exists ( $ ) {
248 my $iname = shift or die "Not enough arguments" ;
249 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
250 my $sth = $dbh->prepare ($query) ;
252 my @array = $sth->fetchrow_array () ;
255 if ($array [0] != 0) {
256 # debug "Dropping view $iname" ;
257 $query = "DROP VIEW $iname" ;
259 $sth = $dbh->prepare ($query) ;
265 sub bump_sequence_to ( $$ ) {
266 my ($sth, @array, $seqname, $targetvalue) ;
269 $targetvalue = shift ;
272 $query = "select nextval ('$seqname')" ;
273 $sth = $dbh->prepare ($query) ;
275 @array = $sth->fetchrow_array () ;
277 } until $array[0] >= $targetvalue ;
280 sub update_with_sql ( $$ ) {
281 my $sqlfile = shift or die "Not enough arguments" ;
282 my $target = shift or die "Not enough arguments" ;
283 $sqlfile =~ s/\.sql$//;
284 my $version = &get_db_version ;
285 if (&is_lesser ($version, $target)) {
286 &debug ("Upgrading database with $sqlfile.sql") ;
288 @reqlist = @{ &parse_sql_file ("$sqlfile.sql") } ;
289 foreach my $s (@reqlist) {
292 my $sth = $dbh->prepare ($query) ;
298 &update_db_version ($target) ;