From 42f4f8362b6ac21a1183b300ffae6719855d38de Mon Sep 17 00:00:00 2001 From: charles Date: Mon, 23 Feb 2015 09:42:02 -0500 Subject: [PATCH] Bug #13624: Remove columns branchcode, categorytype from table overduerules_transport_types Sponsored by Halland County Library This patch is a _requirement_ to Bug #12771, and will be necessary once we start managing more than 3 levels of overdue notice In installer/data/mysql/updatedatabase.pl - A new column and primary key, 'overduerules_id', is added to the table 'overduerules' - A new column referencing a foreign key, 'overduerules_id', is added to the table 'overduerules_transport_types' - The columns 'branchcode' and 'categorytype' are removed from the table 'overduerules_transport_types' - Data is migrated from the old tables to the new ones In installer/data/mysql/kohastructure.sql : - Table 'overduerules' - Added a primary key named 'overduerules_id' - Table 'overduerules_transport_types' - Added a foreign key named 'overduerules_id' - Dropped columns 'branchcode', 'categorycode' since we now have them referenced through 'overduerules_id' In tools/overduerules.pl - INSERT and DELETE queries on 'overduerules_transport_types' were changed to reflect the new schema In C4/Overdues.pm : - The SQL query of the function 'GetOverdueMessageTransportTypes' is changed to take into account the new design of 'overduerules_transport In t/db_dependent/Overdues.t : - The INSERT calls before the tests were changed to take into account the new design of 'overduerules' and 'overduerules_transport_types' TEST PLAN --------- 1) Apply the patch 2) Run the script installer/data/mysql/updatedatabase.pl - It will create new tables and migrate the data 3) Run Overdues.t. Tests should be successful --- C4/Overdues.pm | 7 ++++-- installer/data/mysql/kohastructure.sql | 8 +++---- installer/data/mysql/updatedatabase.pl | 41 ++++++++++++++++++++++++++++++++++ t/db_dependent/Overdues.t | 35 ++++++++++++++--------------- tools/overduerules.pl | 4 ++-- 5 files changed, 69 insertions(+), 26 deletions(-) diff --git a/C4/Overdues.pm b/C4/Overdues.pm index 1797923..291ef7c 100644 --- a/C4/Overdues.pm +++ b/C4/Overdues.pm @@ -937,8 +937,11 @@ sub GetOverdueMessageTransportTypes { return unless $categorycode and $letternumber; my $dbh = C4::Context->dbh; my $sth = $dbh->prepare(" - SELECT message_transport_type FROM overduerules_transport_types - WHERE branchcode = ? AND categorycode = ? AND letternumber = ? + SELECT message_transport_type + FROM overduerules odr LEFT JOIN overduerules_transport_types ott USING (overduerules_id) + WHERE branchcode = ? + AND categorycode = ? + AND letternumber = ? "); $sth->execute( $branchcode, $categorycode, $letternumber ); my @mtts; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 08ef9ee..18e4889 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1700,6 +1700,7 @@ CREATE TABLE `opac_news` ( -- data from the news tool DROP TABLE IF EXISTS `overduerules`; CREATE TABLE `overduerules` ( -- overdue notice status and triggers + `overduerules_id` mediumint NOT NULL AUTO_INCREMENT, -- unique identifier for the overduerules `branchcode` varchar(10) NOT NULL default '', -- foreign key from the branches table to define which branch this rule is for (if blank it's all libraries) `categorycode` varchar(10) NOT NULL default '', -- foreign key from the categories table to define which patron category this rule is for `delay1` int(4) default NULL, -- number of days after the item is overdue that the first notice is sent @@ -1711,7 +1712,7 @@ CREATE TABLE `overduerules` ( -- overdue notice status and triggers `delay3` int(4) default NULL, -- number of days after the item is overdue that the third notice is sent `letter3` varchar(20) default NULL, -- foreign key from the letter table to define which notice should be sent as the third notice `debarred3` int(1) default 0, -- is the patron restricted when the third notice is sent (1 for yes, 0 for no) - PRIMARY KEY (`branchcode`,`categorycode`) + PRIMARY KEY (`overduerules_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- @@ -2596,12 +2597,11 @@ CREATE TABLE `message_transport_types` ( DROP TABLE IF EXISTS `overduerules_transport_types`; CREATE TABLE overduerules_transport_types( `id` INT(11) NOT NULL AUTO_INCREMENT, - `branchcode` varchar(10) NOT NULL DEFAULT '', - `categorycode` VARCHAR(10) NOT NULL DEFAULT '', + `overduerules_id` mediumint NOT NULL, `letternumber` INT(1) NOT NULL DEFAULT 1, `message_transport_type` VARCHAR(20) NOT NULL DEFAULT 'email', PRIMARY KEY (id), - CONSTRAINT overduerules_fk FOREIGN KEY (branchcode, categorycode) REFERENCES overduerules (branchcode, categorycode) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT overduerules_fk FOREIGN KEY (overduerules_id) REFERENCES overduerules (overduerules_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT mtt_fk FOREIGN KEY (message_transport_type) REFERENCES message_transport_types (message_transport_type) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 0ed56c6..ef90370 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -9793,6 +9793,47 @@ if(CheckVersion($DBversion)) { SetVersion($DBversion); } +$DBversion = "XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do("SET FOREIGN_KEY_CHECKS=0"); + $dbh->do("ALTER TABLE overduerules RENAME old_overduerules"); + $dbh->do("CREATE TABLE overduerules ( + `overduerules_id` mediumint NOT NULL AUTO_INCREMENT, + `branchcode` varchar(10) NOT NULL DEFAULT '', + `categorycode` varchar(10) NOT NULL DEFAULT '', + `delay1` int(4) DEFAULT NULL, + `letter1` varchar(20) DEFAULT NULL, + `debarred1` varchar(1) DEFAULT '0', + `delay2` int(4) DEFAULT NULL, + `debarred2` varchar(1) DEFAULT '0', + `letter2` varchar(20) DEFAULT NULL, + `delay3` int(4) DEFAULT NULL, + `letter3` varchar(20) DEFAULT NULL, + `debarred3` int(1) DEFAULT '0', + PRIMARY KEY (`overduerules_id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $dbh->do("INSERT INTO overduerules(branchcode, categorycode, delay1, letter1, debarred1, delay2, debarred2, letter2, delay3, letter3, debarred3) SELECT * FROM old_overduerules"); + $dbh->do("DROP TABLE old_overduerules"); + $dbh->do("ALTER TABLE overduerules_transport_types + ADD COLUMN overduerules_id mediumint NOT NULL"); + my $update = $dbh->prepare("update overduerules_transport_types set overduerules_id = (select overduerules_id from overduerules where branchcode=? and categorycode=? limit 1) where id=?"); + my $mtts = $dbh->selectall_arrayref("select * from overduerules_transport_types", { Slice => {} }); + $dbh->do("DELETE FROM overduerules_transport_types"); + $dbh->do("ALTER TABLE overduerules_transport_types + DROP FOREIGN KEY overduerules_fk, + ADD FOREIGN KEY overduerules_transport_types_fk (overduerules_id) REFERENCES overduerules (overduerules_id) ON DELETE CASCADE ON UPDATE CASCADE, + DROP COLUMN branchcode, + DROP COLUMN categorycode"); + my $s = $dbh->prepare("insert into overduerules_transport_types (id, overduerules_id, letternumber, message_transport_type) values(?,?,?,?)"); + foreach my $mtt(@$mtts){ + $s->execute($mtt->{id}, 1, $mtt->{letternumber}, $mtt->{message_transport_type} ); + $update->execute($mtt->{branchcode}, $mtt->{categorycode}, $mtt->{id}); + } + $dbh->do("SET FOREIGN_KEY_CHECKS=1"); + print "Upgrade to $DBversion done (Bug 13624 - Remove columns branchcode, categorytype from table overduerules_transport_types)\n"; + SetVersion ($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/t/db_dependent/Overdues.t b/t/db_dependent/Overdues.t index 4fc8402..7c53e97 100644 --- a/t/db_dependent/Overdues.t +++ b/t/db_dependent/Overdues.t @@ -24,26 +24,25 @@ $dbh->do(q| |); $dbh->do(q| - INSERT INTO overduerules ( branchcode, categorycode ) VALUES - ('CPL', 'PT'), - ('CPL', 'YA'), - ('', 'PT'), - ('', 'YA') + INSERT INTO overduerules ( overduerules_id, branchcode, categorycode ) VALUES + (1, 'CPL', 'PT'), + (2, 'CPL', 'YA'), + (3, '', 'PT'), + (4, '', 'YA') |); -$dbh->do(q| - INSERT INTO overduerules_transport_types( branchcode, categorycode, letternumber, message_transport_type ) VALUES - ('CPL', 'PT', 1, 'email'), - ('CPL', 'PT', 2, 'sms'), - ('CPL', 'PT', 3, 'email'), - ('CPL', 'YA', 3, 'print'), - ('', 'PT', 1, 'email'), - ('', 'PT', 2, 'email'), - ('', 'PT', 2, 'sms'), - ('', 'PT', 3, 'sms'), - ('', 'PT', 3, 'email'), - ('', 'PT', 3, 'print'), - ('', 'YA', 2, 'sms') +$dbh->do(q|INSERT INTO overduerules_transport_types (overduerules_id, letternumber, message_transport_type) VALUES + (1, 1, 'email'), + (1, 2, 'sms'), + (1, 3, 'email'), + (2, 3, 'print'), + (3, 1, 'email'), + (3, 2, 'email'), + (3, 2, 'sms'), + (3, 3, 'sms'), + (3, 3, 'email'), + (3, 3, 'print'), + (4, 2, 'sms') |); my $mtts; diff --git a/tools/overduerules.pl b/tools/overduerules.pl index 178a569..807a2da 100755 --- a/tools/overduerules.pl +++ b/tools/overduerules.pl @@ -89,12 +89,12 @@ if ($op eq 'save') { INSERT INTO overduerules_transport_types( branchcode, categorycode, letternumber, message_transport_type ) VALUES ( - ?, ?, ?, ? + (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?), ?, ? ) "); my $sth_delete_mtt = $dbh->prepare(" DELETE FROM overduerules_transport_types - WHERE branchcode = ? AND categorycode = ? + WHERE overduerules_id = (SELECT overduerules_id FROM overduerules WHERE branchcode = ? AND categorycode = ?) "); foreach my $key (@names){ -- 1.9.1