From: Ivan Kohler Date: Fri, 24 Feb 2017 18:22:29 +0000 (-0800) Subject: fix upgrades when cust_pkg.change_to_pkgnum points to a package that was removed... X-Git-Url: http://git.freeside.biz/gitweb/?a=commitdiff_plain;h=3951dc9619a5a0c5f148e89754ddd4ce54ede281;p=freeside.git fix upgrades when cust_pkg.change_to_pkgnum points to a package that was removed, RT#74825 --- diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index 691d28513..d40131262 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -573,6 +573,8 @@ sub upgrade_schema_data { #remove records referencing removed things with their FKs 'pkg_referral' => [], 'cust_bill_pkg_discount' => [], + #update records referencing removed things with their FKs + 'cust_pkg' => [], ; \%hash; diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index bd9ab816f..3d54439c5 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -5564,6 +5564,23 @@ sub forward_emails { return %email; } +# Used by FS::Upgrade to migrate to a new database. +sub _upgrade_schema { # class method + my ($class, %opts) = @_; + + my $sql = ' + UPDATE cust_pkg SET change_to_pkgnum = NULL + WHERE change_to_pkgnum IS NOT NULL + AND NOT EXISTS ( SELECT 1 FROM cust_pkg AS ctcp + WHERE ctcp.pkgnum = cust_pkg.change_to_pkgnum + ) + '; + + my $sth = dbh->prepare($sql) or die dbh->errstr; + $sth->execute or die $sth->errstr; + ''; +} + # Used by FS::Upgrade to migrate to a new database. sub _upgrade_data { # class method my ($class, %opts) = @_;