fix some dangling records on upgrade, #32456 and #38765
authorMark Wells <mark@freeside.biz>
Thu, 19 May 2016 23:57:06 +0000 (16:57 -0700)
committerMark Wells <mark@freeside.biz>
Thu, 19 May 2016 23:59:45 +0000 (16:59 -0700)
FS/FS/Upgrade.pm
FS/FS/cust_pay_pending.pm
FS/FS/password_history.pm

index eb2587b..aa0498e 100644 (file)
@@ -523,7 +523,9 @@ sub upgrade_schema_data {
     'cust_bill_pkg_detail' => [],
     #add necessary columns to RT schema
     'TicketSystem' => [],
     'cust_bill_pkg_detail' => [],
     #add necessary columns to RT schema
     'TicketSystem' => [],
-
+    #remove possible dangling records
+    'password_history' => [],
+    'cust_pay_pending' => [],
   ;
 
   \%hash;
   ;
 
   \%hash;
index 1a54203..dfb07b8 100644 (file)
@@ -470,6 +470,19 @@ sub _upgrade_data {  #class method
 
 }
 
 
 }
 
+sub _upgrade_schema {
+  my ($class, %opts) = @_;
+
+  # fix records where jobnum points to a nonexistent queue job
+  my $sql = 'UPDATE cust_pay_pending SET jobnum = NULL
+    WHERE NOT EXISTS (
+      SELECT 1 FROM queue WHERE queue.jobnum = cust_pay_pending.jobnum
+    )';
+  my $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+  '';
+}
+
 =back
 
 =head1 BUGS
 =back
 
 =head1 BUGS
index dd527b9..a34f616 100644 (file)
@@ -160,6 +160,29 @@ sub password_equals {
 
 }
 
 
 }
 
+sub _upgrade_schema {
+  # clean up history records where linked_acct has gone away
+  my @where;
+  for my $fk ( grep /__/, __PACKAGE__->dbdef_table->columns ) {
+    my ($table, $key) = split(/__/, $fk);
+    push @where, "
+      ( $fk IS NOT NULL AND NOT EXISTS(SELECT 1 FROM $table WHERE $table.$key = $fk) )";
+  }
+  my @recs = qsearch({
+      'table'     => 'password_history',
+      'extra_sql' => ' WHERE ' . join(' AND ', @where),
+  });
+  my $error;
+  if (@recs) {
+    warn "Removing unattached password_history records (".scalar(@recs).").\n";
+    foreach my $password_history (@recs) {
+      $error = $password_history->delete;
+      die $error if $error;
+    }
+  }
+  '';
+}
+
 =back
 
 =head1 BUGS
 =back
 
 =head1 BUGS