From bea91f15ef8fe7f508fb4911cea128dbdb03a6aa Mon Sep 17 00:00:00 2001 From: Katrin Fischer Date: Fri, 21 Jul 2023 14:39:07 +0000 Subject: [PATCH] Bug 30451: Update FK constraint on aqorders.subscriptionid This updates the FK constrant from ON DELETE CASCADE to ON DELETE SET NULL. This means that if a subscription linked to an order is deleted, we no longer will also delete the order, but we will just set subscrptinid in the order to NULL. This will avoid data loss that can cause the budgets/funds not to add up anymore with the real espenses of the library. To test: Preparation: * Create 2 subscriptions on different records * Create a new basket * Use the "order from subscription" functionality to create order lines for both of your subscriptions * Close basket Without patch: * Delete the first subscription * Verify the order line for this subscription is gone from your basket Apply patch: * Run database update and restart_all * Delete the second subscription * Verify the order line now remained in the basket Signed-off-by: Michaela Sieber JD amended patch: perl tidy Signed-off-by: Jonathan Druart Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 765d3d85b5b2b2e0711550b5b244d71967a2ee0c) Signed-off-by: Fridolin Somers (cherry picked from commit 43c55cad8a4a14fccc6112ca327603c07068d0c1) Signed-off-by: Pedro Amorim --- ..._do_not_delete_orders_with_subscription.pl | 22 +++++++++++++++++++ installer/data/mysql/kohastructure.sql | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100755 installer/data/mysql/atomicupdate/bug_30451_do_not_delete_orders_with_subscription.pl diff --git a/installer/data/mysql/atomicupdate/bug_30451_do_not_delete_orders_with_subscription.pl b/installer/data/mysql/atomicupdate/bug_30451_do_not_delete_orders_with_subscription.pl new file mode 100755 index 0000000000..bb3b7747fc --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_30451_do_not_delete_orders_with_subscription.pl @@ -0,0 +1,22 @@ +use Modern::Perl; + +return { + bug_number => "30451", + description => "Update FK constraint on aqorders.subscriptionid to ON DELETE SET NULL", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + if ( foreign_key_exists( 'aqorders', 'aqorders_subscriptionid' ) ) { + $dbh->do(q{ + ALTER TABLE aqorders + DROP FOREIGN KEY aqorders_subscriptionid + }); + } + $dbh->do(q{ + ALTER TABLE aqorders + ADD CONSTRAINT `aqorders_subscriptionid` FOREIGN KEY (`subscriptionid`) REFERENCES subscription(`subscriptionid`) ON DELETE SET NULL ON UPDATE CASCADE + }); + say $out "Update FK constraint on subscriptionid"; + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index ba5305fd30..34aca0332b 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -707,7 +707,7 @@ CREATE TABLE `aqorders` ( CONSTRAINT `aqorders_ibfk_1` FOREIGN KEY (`basketno`) REFERENCES `aqbasket` (`basketno`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `aqorders_ibfk_2` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `aqorders_ibfk_3` FOREIGN KEY (`invoiceid`) REFERENCES `aqinvoices` (`invoiceid`) ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT `aqorders_subscriptionid` FOREIGN KEY (`subscriptionid`) REFERENCES `subscription` (`subscriptionid`) ON DELETE CASCADE ON UPDATE CASCADE + CONSTRAINT `aqorders_subscriptionid` FOREIGN KEY (`subscriptionid`) REFERENCES `subscription` (`subscriptionid`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- 2.39.5