report errors connecting to sqlradius dbs on upgrade
[freeside.git] / FS / FS / Upgrade.pm
index 8f3a6e5..37167a8 100644 (file)
@@ -117,8 +117,19 @@ sub upgrade_sqlradius {
   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
 
   foreach my $part_export ( @part_export ) {
-    my $dbh = DBI->connect( map $part_export->option($_),
-                             qw ( datasrc username password ) );
+    my $dbh = DBI->connect(
+      ( map $part_export->option($_), qw ( datasrc username password ) ),
+      { PrintError => 0, PrintWarn => 0 }
+    );
+
+    unless $dbh {
+      warn "can't connect to RADIUS database ".
+           $part_export->option('datasrc').  ": $DBI::errstr\n";
+      next;
+    }
+
+    my $errmsg = 'Error adding FreesideStatus to '.
+                 $part_export->option('datasrc'). ': ';
   
     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
     my $group = "UserName";
@@ -128,18 +139,32 @@ sub upgrade_sqlradius {
     my $sth_alter = $dbh->prepare(
       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
     );
-    if ( $sth_alter && $sth_alter->execute ) {
-      my $sth_update = $dbh->prepare(
-       "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
-      ) or die $dbh->errstr;
-      $sth_update->execute or die $sth_update->errstr;
+    if ( $sth_alter ) {
+      if ( $sth_alter->execute ) {
+        my $sth_update = $dbh->prepare(
+         "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
+        ) or die $errmsg.$dbh->errstr;
+        $sth_update->execute or die $errmsg.$sth_update->errstr;
+      } else {
+        my $error = $sth_alter->errstr;
+        warn $errmsg.$error unless $error =~ /Duplicate column name/i;
+      }
+    } else {
+      my $error = $dbh->errstr;
+      warn $errmsg.$error; #unless $error =~ /exists/i;
     }
 
     my $sth_index = $dbh->prepare(
       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
     );
     if ( $sth_index ) {
-      $sth_index->execute;
+      unless ( $sth_index->execute ) {
+        my $error = $sth_index->errstr;
+        warn $errmsg.$error unless $error =~ /Duplicate key name/i;
+      }
+    } else {
+      my $error = $dbh->errstr;
+      warn $errmsg.$error; #unless $error =~ /exists/i;
     }
 
     my $sth = $dbh->prepare("SELECT UserName,
@@ -152,8 +177,8 @@ sub upgrade_sqlradius {
                                 AND AcctStopTime  != 0
                               GROUP BY $group
                             ")
-      or die $dbh->errstr;
-    $sth->execute() or die $sth->errstr;
+      or die $errmsg.$dbh->errstr;
+    $sth->execute() or die $errmsg.$sth->errstr;
   
     while (my $row = $sth->fetchrow_arrayref ) {
       my ($username, $realm, $start, $stop) = @$row;