From 877a4eb85cb847bd314d6a9192fedb1dc35c5d02 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Tue, 14 Feb 2017 09:02:48 -0800 Subject: [PATCH 1/1] fix FK upgrade for pkg_referral: remove records referencing non-existant customer packages --- FS/FS/pkg_referral.pm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/FS/FS/pkg_referral.pm b/FS/FS/pkg_referral.pm index 333c2bf8a..a5ac79505 100644 --- a/FS/FS/pkg_referral.pm +++ b/FS/FS/pkg_referral.pm @@ -2,7 +2,7 @@ package FS::pkg_referral; use strict; use vars qw( @ISA ); -use FS::Record qw( qsearch qsearchs ); +use FS::Record qw( dbh ); @ISA = qw(FS::Record); @@ -108,6 +108,19 @@ sub check { $self->SUPER::check; } +sub _upgrade_schema { + my ($class, %opts) = @_; + + my $sql = ' + DELETE FROM pkg_referral WHERE NOT EXISTS + ( SELECT 1 FROM cust_pkg WHERE cust_pkg.pkgnum = pkg_referral.pkgnum ) + '; + + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute or die $sth->errstr; + ''; +} + =back =head1 BUGS -- 2.11.0