From 921c62774b26197ed24ca983859fe00b119f82fb Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 16 Aug 2010 20:11:09 +0000 Subject: [PATCH] fix upgrade with ancient cust_bill_pkg_detail.classnum but new DBIx::DBSchema, RT#9640 --- FS/FS/Upgrade.pm | 68 ++++++++++++++++++++++++++++++++- FS/FS/cust_bill_pkg_detail.pm | 88 +++++++++++++++++++++---------------------- FS/bin/freeside-upgrade | 4 +- 3 files changed, 111 insertions(+), 49 deletions(-) diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index 72761b49e..333c277de 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -12,7 +12,7 @@ use FS::svc_domain; $FS::svc_domain::whois_hack = 1; @ISA = qw( Exporter ); -@EXPORT_OK = qw( upgrade upgrade_sqlradius ); +@EXPORT_OK = qw( upgrade_schema upgrade upgrade_sqlradius ); $DEBUG = 1; @@ -33,7 +33,7 @@ database upgrades. =over 4 -=item +=item upgrade =cut @@ -86,6 +86,9 @@ sub upgrade { } +=item upgrade_data + +=cut sub upgrade_data { my %opt = @_; @@ -147,6 +150,67 @@ sub upgrade_data { } +=item upgrade_schema + +=cut + +sub upgrade_schema { + my %opt = @_; + + my $data = upgrade_schema_data(%opt); + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + local $FS::UID::AutoCommit = 0; + + foreach my $table ( keys %$data ) { + + my $class = "FS::$table"; + eval "use $class;"; + die $@ if $@; + + if ( $class->can('_upgrade_schema') ) { + warn "Upgrading $table schema...\n"; + + my $start = time; + + $class->_upgrade_schema(%opt); + + if ( $oldAutoCommit ) { + warn " committing\n"; + dbh->commit or die dbh->errstr; + } + + #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n"; + warn " done in ". (time-$start). " seconds\n"; + + } else { + warn "WARNING: asked for schema upgrade of $table,". + " but FS::$table has no _upgrade_schema method\n"; + } + + } + +} + +=item upgrade_schema_data + +=cut + +sub upgrade_schema_data { + my %opt = @_; + + tie my %hash, 'Tie::IxHash', + + #fix classnum character(1) + 'cust_bill_pkg_detail' => [], + + ; + + \%hash; + +} + sub upgrade_sqlradius { #my %opt = @_; diff --git a/FS/FS/cust_bill_pkg_detail.pm b/FS/FS/cust_bill_pkg_detail.pm index 4d9ee8191..b8af01303 100644 --- a/FS/FS/cust_bill_pkg_detail.pm +++ b/FS/FS/cust_bill_pkg_detail.pm @@ -231,11 +231,8 @@ sub formatted { } -# _upgrade_data -# -# Used by FS::Upgrade to migrate to a new database. - -sub _upgrade_data { # class method +# Used by FS::Upgrade to migrate to a new database schema +sub _upgrade_schema { # class method my ($class, %opts) = @_; @@ -313,51 +310,50 @@ sub _upgrade_data { # class method } +} - if ( defined( dbdef->table($class->table)->column('billpkgnum') ) && - defined( dbdef->table($class->table)->column('invnum') ) && - defined( dbdef->table($class->table)->column('pkgnum') ) - ) { - - warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG; - - my @cbpd = qsearch({ 'table' => $class->table, - 'hashref' => {}, - 'extra_sql' => 'WHERE invnum IS NOT NULL AND '. - 'pkgnum IS NOT NULL', - }); - - if (scalar(@cbpd)) { - warn "$me Found unmigrated invoice line item details\n" if $DEBUG; - - foreach my $cbpd ( @cbpd ) { - my $detailnum = $cbpd->detailnum; - warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1; - my $cust_bill_pkg = - qsearchs({ 'table' => 'cust_bill_pkg', - 'hashref' => { 'invnum' => $cbpd->invnum, - 'pkgnum' => $cbpd->pkgnum, - }, - 'order_by' => 'ORDER BY billpkgnum LIMIT 1', - }); - if ($cust_bill_pkg) { - $cbpd->billpkgnum($cust_bill_pkg->billpkgnum); - $cbpd->invnum(''); - $cbpd->pkgnum(''); - my $error = $cbpd->replace; - - warn "*** WARNING: error replacing line item detail ". - "(cust_bill_pkg_detail) $detailnum: $error ***\n" - if $error; - } else { - warn "Found orphaned line item detail $detailnum during upgrade.\n"; - } +# Used by FS::Upgrade to migrate to a new database +sub _upgrade_data { # class method - } # foreach $cbpd + my ($class, %opts) = @_; + + warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG; + + my @cbpd = qsearch({ 'table' => $class->table, + 'hashref' => {}, + 'extra_sql' => 'WHERE invnum IS NOT NULL AND '. + 'pkgnum IS NOT NULL', + }); + + if (scalar(@cbpd)) { + warn "$me Found unmigrated invoice line item details\n" if $DEBUG; + + foreach my $cbpd ( @cbpd ) { + my $detailnum = $cbpd->detailnum; + warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1; + my $cust_bill_pkg = + qsearchs({ 'table' => 'cust_bill_pkg', + 'hashref' => { 'invnum' => $cbpd->invnum, + 'pkgnum' => $cbpd->pkgnum, + }, + 'order_by' => 'ORDER BY billpkgnum LIMIT 1', + }); + if ($cust_bill_pkg) { + $cbpd->billpkgnum($cust_bill_pkg->billpkgnum); + $cbpd->invnum(''); + $cbpd->pkgnum(''); + my $error = $cbpd->replace; + + warn "*** WARNING: error replacing line item detail ". + "(cust_bill_pkg_detail) $detailnum: $error ***\n" + if $error; + } else { + warn "Found orphaned line item detail $detailnum during upgrade.\n"; + } - } # if @cbpd + } # foreach $cbpd - } # if billpkgnum, invnum, and pkgnum columns defined + } # if @cbpd ''; diff --git a/FS/bin/freeside-upgrade b/FS/bin/freeside-upgrade index 4a6fac293..e22afa26c 100755 --- a/FS/bin/freeside-upgrade +++ b/FS/bin/freeside-upgrade @@ -11,7 +11,7 @@ use FS::Schema qw( dbdef dbdef_dist reload_dbdef ); use FS::Misc::prune qw(prune_applications); use FS::Conf; use FS::Record qw(qsearch); -use FS::Upgrade qw(upgrade upgrade_sqlradius); +use FS::Upgrade qw(upgrade_schema upgrade upgrade_sqlradius); my $start = time; @@ -82,6 +82,8 @@ if ( $DRY_RUN ) { or die "Error: ". $dbh->errstr. "\n executing: $statement"; } + upgrade_schema(); + dbdef_create($dbh, $dbdef_file); delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload reload_dbdef($dbdef_file); -- 2.11.0