From d85065917380edcfed81dbde6c5c05dc25c8ea36 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 26 Sep 2019 14:36:06 +0100
Subject: [PATCH] Bug 23049: Add CHECK constraint to accountlines
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>
---
 installer/data/mysql/atomicupdate/bug_23049_debit.perl | 6 ++++++
 installer/data/mysql/kohastructure.sql                 | 1 +
 2 files changed, 7 insertions(+)

diff --git a/installer/data/mysql/atomicupdate/bug_23049_debit.perl b/installer/data/mysql/atomicupdate/bug_23049_debit.perl
index f5fa7c2c97..e59fdd936d 100644
--- a/installer/data/mysql/atomicupdate/bug_23049_debit.perl
+++ b/installer/data/mysql/atomicupdate/bug_23049_debit.perl
@@ -89,6 +89,12 @@ if ( CheckVersion($DBversion) ) {
           }
     );
 
+    $dbh->do(
+        qq{
+        ALTER TABLE accountlines ADD CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR debit_type IS NOT NULL)
+        }
+    );
+
     $dbh->do(
         qq{
         UPDATE accountlines SET debit_type = accounttype, accounttype = NULL WHERE accounttype IN (SELECT code from account_debit_types)
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 87d219ab29..b9466080dc 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2678,6 +2678,7 @@ CREATE TABLE `accountlines` (
   KEY `itemnumber` (`itemnumber`),
   KEY `branchcode` (`branchcode`),
   KEY `manager_id` (`manager_id`),
+  CONSTRAINT `accountlines_check_type` CHECK (accounttype IS NOT NULL OR debit_type IS NOT NULL),
   CONSTRAINT `accountlines_ibfk_borrowers` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE,
   CONSTRAINT `accountlines_ibfk_items` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE SET NULL ON UPDATE CASCADE,
   CONSTRAINT `accountlines_ibfk_borrowers_2` FOREIGN KEY (`manager_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE,
-- 
2.20.1