From 938ff88906005edf05ed9a1fd43d458fafe1520b Mon Sep 17 00:00:00 2001 From: Katrin Fischer Date: Sun, 29 Oct 2023 14:14:37 +0000 Subject: [PATCH] Bug 34520: Fix FK for item_groups in reserves for new installations There was a discrepancy between the database update for reserves and the kohastructure.sql definition. This makes sure that the FK is always "ON DELETE SET NULL". To test: * Before applying this path * sudo koha-mysql kohadev * show create table reserves; CONSTRAINT `reserves_ibfk_ig` FOREIGN KEY (`item_group_id`) REFERENCES `item_groups` (`item_group_id`) ON DELETE CASCADE ON UPDATE CASCADE * Apply patch * Run database update * Check database again: CONSTRAINT `reserves_ibfk_ig` FOREIGN KEY (`item_group_id`) REFERENCES `item_groups` (`item_group_id`) ON DELETE SET NULL ON UPDATE CASCADE * reset_all (create a new database) * Check database again: CONSTRAINT `reserves_ibfk_ig` FOREIGN KEY (`item_group_id`) REFERENCES `item_groups` (`item_group_id`) ON DELETE SET NULL ON UPDATE CASCADE Signed-off-by: David Nind Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 6dd4626711866fbf92b49f88770a6b3bfb75ddca) Signed-off-by: Fridolin Somers (cherry picked from commit d0090275b0fd2cd728183c072765a5a7cfdbbe54) Signed-off-by: Matt Blenkinsop --- .../atomicupdate/bug_34520_item_groups_fk.pl | 30 +++++++++++++++++++ installer/data/mysql/kohastructure.sql | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 installer/data/mysql/atomicupdate/bug_34520_item_groups_fk.pl diff --git a/installer/data/mysql/atomicupdate/bug_34520_item_groups_fk.pl b/installer/data/mysql/atomicupdate/bug_34520_item_groups_fk.pl new file mode 100755 index 0000000000..215dfdb84d --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_34520_item_groups_fk.pl @@ -0,0 +1,30 @@ +use Modern::Perl; + +return { + bug_number => "34520", + description => "Correct item_groups FK in reserves table", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + if ( foreign_key_exists( 'reserves', 'reserves_ibfk_ig' ) ) { + $dbh->do( + q| + ALTER TABLE reserves + DROP FOREIGN KEY reserves_ibfk_ig + | + ); + + $dbh->do( + q| + ALTER TABLE reserves + ADD CONSTRAINT reserves_ibfk_ig + FOREIGN KEY (item_group_id) + REFERENCES item_groups (item_group_id) ON DELETE SET NULL ON UPDATE CASCADE + | + ); + } + + say $out "FK 'reserves_ibfk_ig' on reserves updated to ON DELETE SET NULL"; + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 80f23c85af..fc69b6897f 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4961,7 +4961,7 @@ CREATE TABLE `reserves` ( CONSTRAINT `reserves_ibfk_4` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `reserves_ibfk_5` FOREIGN KEY (`itemtype`) REFERENCES `itemtypes` (`itemtype`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `reserves_ibfk_6` FOREIGN KEY (`desk_id`) REFERENCES `desks` (`desk_id`) ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT `reserves_ibfk_ig` FOREIGN KEY (`item_group_id`) REFERENCES `item_groups` (`item_group_id`) ON DELETE CASCADE ON UPDATE CASCADE + CONSTRAINT `reserves_ibfk_ig` FOREIGN KEY (`item_group_id`) REFERENCES `item_groups` (`item_group_id`) 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