Bug 24289: Add foreign keys on *_holidays.branchcode tables

Those are missing foreign keys, it will also clean the holidays tables
and automatically remove the holidays when a library is removed.

Test plan:
0. Do not apply this patch
1. Create a new library
2. Create special and repeatable holiday for this library
3. Remove the library
=> Notice that the holidays still exist in DB
4. Apply this patch
5. Execute the update database entry
=> Notice that the holidays for this library have been removed from the DB
6. Repeat 1-3
=> Notice that the holidays have been removed along with the library

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Jonathan Druart 2019-12-26 11:42:43 +01:00 committed by Martin Renvoize
parent 90c92686eb
commit 89f572ff5f
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F
2 changed files with 37 additions and 4 deletions

View file

@ -0,0 +1,31 @@
$DBversion = 'XXX'; # will be replaced by the RM
if( CheckVersion( $DBversion ) ) {
if( !foreign_key_exists( 'repeatable_holidays', 'repeatable_holidays_ibfk_1' ) ) {
$dbh->do(q|
DELETE h
FROM repeatable_holidays h
LEFT JOIN branches b ON h.branchcode=b.branchcode
WHERE b.branchcode IS NULL;
|);
$dbh->do(q|
ALTER TABLE repeatable_holidays
ADD FOREIGN KEY repeatable_holidays_ibfk_1 (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE
|);
}
if( !foreign_key_exists( 'special_holidays', 'special_holidays_ibfk_1' ) ) {
$dbh->do(q|
DELETE h
FROM special_holidays h
LEFT JOIN branches b ON h.branchcode=b.branchcode
WHERE b.branchcode IS NULL;
|);
$dbh->do(q|
ALTER TABLE special_holidays
ADD FOREIGN KEY special_holidays_ibfk_1 (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE
|);
}
SetVersion( $DBversion );
print "Upgrade to $DBversion done (Bug 24289 - Adding foreign keys on *_holidays.branchcode tables)\n";
}

View file

@ -1320,13 +1320,14 @@ CREATE TABLE `printers_profile` (
DROP TABLE IF EXISTS `repeatable_holidays`;
CREATE TABLE `repeatable_holidays` ( -- information for the days the library is closed
`id` int(11) NOT NULL auto_increment, -- unique identifier assigned by Koha
`branchcode` varchar(10) NOT NULL default '', -- foreign key from the branches table, defines which branch this closing is for
`branchcode` varchar(10) NOT NULL, -- foreign key from the branches table, defines which branch this closing is for
`weekday` smallint(6) default NULL, -- day of the week (0=Sunday, 1=Monday, etc) this closing is repeated on
`day` smallint(6) default NULL, -- day of the month this closing is on
`month` smallint(6) default NULL, -- month this closing is in
`title` varchar(50) NOT NULL default '', -- title of this closing
`description` MEDIUMTEXT NOT NULL, -- description for this closing
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
CONSTRAINT `repeatable_holidays_ibfk_1` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
@ -1902,14 +1903,15 @@ CREATE TABLE `reviews` ( -- patron opac comments
DROP TABLE IF EXISTS `special_holidays`;
CREATE TABLE `special_holidays` ( -- non repeatable holidays/library closings
`id` int(11) NOT NULL auto_increment, -- unique identifier assigned by Koha
`branchcode` varchar(10) NOT NULL default '', -- foreign key from the branches table, defines which branch this closing is for
`branchcode` varchar(10) NOT NULL, -- foreign key from the branches table, defines which branch this closing is for
`day` smallint(6) NOT NULL default 0, -- day of the month this closing is on
`month` smallint(6) NOT NULL default 0, -- month this closing is in
`year` smallint(6) NOT NULL default 0, -- year this closing is in
`isexception` smallint(1) NOT NULL default 1, -- is this a holiday exception to a repeatable holiday (1 for yes, 0 for no)
`title` varchar(50) NOT NULL default '', -- title for this closing
`description` MEDIUMTEXT NOT NULL, -- description of this closing
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
CONSTRAINT `special_holidays_ibfk_1` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--