From 261e887dc07569944dfcc8733ee3d891b948e790 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 15 Oct 2019 12:00:41 +0100 Subject: [PATCH] Bug 23805: (follow-up) Handle unexpected types Signed-off-by: Kyle Hall Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- .../mysql/atomicupdate/bug_23805_credit.perl | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/installer/data/mysql/atomicupdate/bug_23805_credit.perl b/installer/data/mysql/atomicupdate/bug_23805_credit.perl index 27896d0313..e7c84793b2 100644 --- a/installer/data/mysql/atomicupdate/bug_23805_credit.perl +++ b/installer/data/mysql/atomicupdate/bug_23805_credit.perl @@ -61,7 +61,12 @@ if ( CheckVersion($DBversion) ) { unless ( foreign_key_exists( 'accountlines', 'accountlines_ibfk_credit_type' ) ) { $dbh->do( qq{ - ALTER TABLE accountlines ADD CONSTRAINT `accountlines_ibfk_credit_type` FOREIGN KEY (`credit_type_code`) REFERENCES `account_credit_types` (`code`) ON DELETE SET NULL ON UPDATE CASCADE + ALTER TABLE accountlines + ADD CONSTRAINT + `accountlines_ibfk_credit_type` + FOREIGN KEY (`credit_type_code`) REFERENCES `account_credit_types` (`code`) + ON DELETE RESTRICT + ON UPDATE CASCADE } ); } @@ -69,7 +74,11 @@ if ( CheckVersion($DBversion) ) { # Dropping the check constraint in accountlines $dbh->do( qq{ - ALTER TABLE accountlines ADD CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR credit_type_code IS NOT NULL) + ALTER TABLE + accountlines + ADD CONSTRAINT + `accountlines_check_type` + CHECK (credit_type_code IS NOT NULL OR debit_type_code IS NOT NULL) } ); @@ -101,6 +110,27 @@ if ( CheckVersion($DBversion) ) { } ); + # Add any unexpected accounttype codes to credit_types as appropriate + $dbh->do( + qq{ + INSERT IGNORE INTO account_credit_types ( + code, + description, + can_be_added_manually, + is_system + ) + SELECT + DISTINCT(accounttype), + "Unexpected type found during upgrade", + 1, + 0 + FROM + accountlines + WHERE + amount < 0 + } + ); + # Populating credit_type_code $dbh->do( qq{