fix upgrade of cust_pay_pending for deleted customers, RT#75118
authorIvan Kohler <ivan@freeside.biz>
Fri, 10 Mar 2017 20:02:32 +0000 (12:02 -0800)
committerIvan Kohler <ivan@freeside.biz>
Fri, 10 Mar 2017 20:02:32 +0000 (12:02 -0800)
FS/FS/cust_pay_pending.pm

index d108341..8f313f4 100644 (file)
@@ -504,6 +504,16 @@ sub _upgrade_schema {
     )';
   my $sth = dbh->prepare($sql) or die dbh->errstr;
   $sth->execute or die $sth->errstr;
+
+  # fix records where custnum points to a nonexistent customer
+  $sql = 'UPDATE cust_pay_pending SET custnum = NULL
+    WHERE NOT EXISTS (
+      SELECT 1 FROM cust_main WHERE cust_main.custnum = cust_pay_pending.custnum
+    )';
+  $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+
+
   '';
 }