Bug 23805: (follow-up) Handle unexpected types

Signed-off-by: Kyle Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Martin Renvoize 2019-10-15 12:00:41 +01:00
parent 12568a20db
commit 261e887dc0
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F

View file

@ -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{