3 # $Id: db-delete.pl,v 1.1.1.1 2005/09/22 17:32:56 rhertzog Exp $
5 # Debian-specific script to delete plugin-specific tables
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 = "extratabs" ;
43 $dbh->{AutoCommit} = 0;
44 $dbh->{RaiseError} = 1;
46 my ($sth, @array, $version, $action, $path, $target, $rname) ;
48 my $pattern = "plugin_" . $pluginname . '_%' ;
50 $query = "SELECT relname FROM pg_class WHERE relname LIKE '$pattern' AND relkind='v'" ;
51 $sth = $dbh->prepare ($query) ;
53 while (@array = $sth->fetchrow_array ()) {
55 &drop_view_if_exists ($rname) ;
59 $query = "SELECT relname FROM pg_class WHERE relname LIKE '$pattern' AND relkind='r'" ;
60 $sth = $dbh->prepare ($query) ;
62 while (@array = $sth->fetchrow_array ()) {
64 &drop_table_if_exists ($rname) ;
68 $query = "SELECT relname FROM pg_class WHERE relname LIKE '$pattern' AND relkind='i'" ;
69 $sth = $dbh->prepare ($query) ;
71 while (@array = $sth->fetchrow_array ()) {
73 &drop_index_if_exists ($rname) ;
77 $query = "SELECT relname FROM pg_class WHERE relname LIKE '$pattern' AND relkind='s'" ;
78 $sth = $dbh->prepare ($query) ;
80 while (@array = $sth->fetchrow_array ()) {
82 &drop_sequence_if_exists ($rname) ;
89 debug "It seems your database deletion went well and smoothly. That's cool." ;
90 debug "Please enjoy using Debian GForge." ;
92 # There should be a commit at the end of every block above.
93 # If there is not, then it might be symptomatic of a problem.
94 # For safety, we roll back.
99 warn "Transaction aborted because $@" ;
100 debug "Transaction aborted because $@" ;
101 debug "Last SQL query was:\n$query\n(end of query)" ;
103 debug "Please report this bug on the Debian bug-tracking system." ;
104 debug "Please include the previous messages as well to help debugging." ;
105 debug "You should not worry too much about this," ;
106 debug "your DB is still in a consistent state and should be usable." ;
116 print STDERR "$v\n" ;
119 sub drop_table_if_exists ( $ ) {
120 my $tname = shift or die "Not enough arguments" ;
121 $query = "SELECT count(*) FROM pg_class WHERE relname='$tname' AND relkind='r'" ;
122 my $sth = $dbh->prepare ($query) ;
124 my @array = $sth->fetchrow_array () ;
127 if ($array [0] != 0) {
128 # debug "Dropping table $tname" ;
129 $query = "DROP TABLE $tname" ;
131 $sth = $dbh->prepare ($query) ;
137 sub drop_sequence_if_exists ( $ ) {
138 my $sname = shift or die "Not enough arguments" ;
139 $query = "SELECT count(*) FROM pg_class WHERE relname='$sname' AND relkind='S'" ;
140 my $sth = $dbh->prepare ($query) ;
142 my @array = $sth->fetchrow_array () ;
145 if ($array [0] != 0) {
146 # debug "Dropping sequence $sname" ;
147 $query = "DROP SEQUENCE $sname" ;
149 $sth = $dbh->prepare ($query) ;
155 sub drop_index_if_exists ( $ ) {
156 my $iname = shift or die "Not enough arguments" ;
157 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='i'" ;
158 my $sth = $dbh->prepare ($query) ;
160 my @array = $sth->fetchrow_array () ;
163 if ($array [0] != 0) {
164 # debug "Dropping index $iname" ;
165 $query = "DROP INDEX $iname" ;
167 $sth = $dbh->prepare ($query) ;
173 sub drop_view_if_exists ( $ ) {
174 my $iname = shift or die "Not enough arguments" ;
175 $query = "SELECT count(*) FROM pg_class WHERE relname='$iname' AND relkind='v'" ;
176 my $sth = $dbh->prepare ($query) ;
178 my @array = $sth->fetchrow_array () ;
181 if ($array [0] != 0) {
182 # debug "Dropping view $iname" ;
183 $query = "DROP VIEW $iname" ;
185 $sth = $dbh->prepare ($query) ;