fix upgrade for dangling cust_main_invoice, RT#75271
[freeside.git] / FS / FS / cust_main_invoice.pm
index 6c155ac..31a07bc 100644 (file)
@@ -2,7 +2,7 @@ package FS::cust_main_invoice;
 use base qw( FS::Record );
 
 use strict;
-use FS::Record qw( qsearchs );
+use FS::Record qw( qsearchs dbh );
 use FS::Conf;
 use FS::svc_acct;
 use FS::Msgcat qw(gettext);
@@ -167,6 +167,22 @@ Returns the parent customer object (see L<FS::cust_main>).
 
 =back
 
+=cut
+
+sub _upgrade_schema {
+  my ($class, %opts) = @_;
+
+  # delete records where custnum points to a nonexistent customer
+  my $sql = 'DELETE FROM cust_main_invoice
+    WHERE NOT EXISTS (
+      SELECT 1 FROM cust_main WHERE cust_main.custnum = cust_main_invoice.custnum
+    )';
+  my $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+
+  '';
+}
+
 =head1 BUGS
 
 =head1 SEE ALSO