From 7072b6d07f64fbcd0a6bb2481a55ade864e57c77 Mon Sep 17 00:00:00 2001
From: simith <simith@inlibro.com>
Date: Wed, 6 Aug 2014 08:40:32 -0400
Subject: [PATCH] Bug 12424 - ddc sorting of call numbers truncates long Cutter
 parts

This patch increases the cn_sort field length to 255 chars.

Modified:

C4/ClassSortRoutine.pm                    - change the hardcoded cn_sort length to 255
installer/data/mysql/kohastructure.sql    - alter tables items and deleteditems
installer/data/mysql/updatedatabase.pl    - alter tables items and deleteditems

Testing:

Before applying the patch:
0) Select an item
1) Edit the item selected
2) Change "Full call number" option to 530 F435_1996 v2p1
3) Save changes
4) Valide 530_000000000000000_F435_1996_ (table items - cn_sort column).

After applying the patch:
5) Edit again the item selected in 0
3) Save changes
4) Valide 530_000000000000000_F435_1996_V2P1 (table items - cn_sort column).
---
 C4/ClassSortRoutine.pm                 |  2 +-
 installer/data/mysql/kohastructure.sql | 27 +++++++++++++--------------
 installer/data/mysql/updatedatabase.pl |  8 ++++++++
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/C4/ClassSortRoutine.pm b/C4/ClassSortRoutine.pm
index 04c24a5..8cd5334 100644
--- a/C4/ClassSortRoutine.pm
+++ b/C4/ClassSortRoutine.pm
@@ -101,7 +101,7 @@ sub GetClassSortKey {
     # should replace with some way of getting column widths from
     # the DB schema -- since doing this should ideally be
     # independent of the DBMS, deferring for the moment.
-    return substr($key, 0, 30);
+    return substr($key, 0, 255);
 }
 
 =head2 _get_class_sort_key 
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index b46dc6a..925cc77 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2,7 +2,7 @@
 --
 -- Host: localhost    Database: koha30test
 -- ------------------------------------------------------
--- Server version	4.1.22
+-- Server version    4.1.22
 
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
@@ -911,7 +911,7 @@ CREATE TABLE `deleteditems` (
   `permanent_location` varchar(80) default NULL, -- linked to the CART and PROC temporary locations feature, stores the permanent shelving location
   `onloan` date default NULL, -- defines if item is checked out (NULL for not checked out, and checkout date for checked out)
   `cn_source` varchar(10) default NULL, -- classification source used on this item (MARC21 952$2)
-  `cn_sort` varchar(30) default NULL, -- normalized form of the call number (MARC21 952$o) used for sorting
+  `cn_sort` varchar(255) default NULL, -- normalized form of the call number (MARC21 952$o) used for sorting
   `ccode` varchar(10) default NULL, -- authorized value for the collection code associated with this item (MARC21 952$8)
   `materials` text default NULL, -- materials specified (MARC21 952$3)
   `uri` varchar(255) default NULL, -- URL for the item (MARC21 952$u)
@@ -1207,7 +1207,7 @@ CREATE TABLE `items` ( -- holdings/item information
   `permanent_location` varchar(80) default NULL, -- linked to the CART and PROC temporary locations feature, stores the permanent shelving location
   `onloan` date default NULL, -- defines if item is checked out (NULL for not checked out, and checkout date for checked out)
   `cn_source` varchar(10) default NULL, -- classification source used on this item (MARC21 952$2)
-  `cn_sort` varchar(30) default NULL,  -- normalized form of the call number (MARC21 952$o) used for sorting
+  `cn_sort` varchar(255) default NULL,  -- normalized form of the call number (MARC21 952$o) used for sorting
   `ccode` varchar(10) default NULL, -- authorized value for the collection code associated with this item (MARC21 952$8)
   `materials` text default NULL, -- materials specified (MARC21 952$3)
   `uri` varchar(255) default NULL, -- URL for the item (MARC21 952$u)
@@ -2174,10 +2174,10 @@ CREATE TABLE `suggestions` ( -- purchase suggestions
    branchcode VARCHAR(10) default NULL, -- foreign key linking the suggested branch to the branches table
    collectiontitle text default NULL, -- collection name for the suggested item
    itemtype VARCHAR(30) default NULL, -- suggested item type 
-	quantity SMALLINT(6) default NULL, -- suggested quantity to be purchased
-	currency VARCHAR(3) default NULL, -- suggested currency for the suggested price
-	price DECIMAL(28,6) default NULL, -- suggested price
-	total DECIMAL(28,6) default NULL, -- suggested total cost (price*quantity updated for currency)
+    quantity SMALLINT(6) default NULL, -- suggested quantity to be purchased
+    currency VARCHAR(3) default NULL, -- suggested currency for the suggested price
+    price DECIMAL(28,6) default NULL, -- suggested price
+    total DECIMAL(28,6) default NULL, -- suggested total cost (price*quantity updated for currency)
   PRIMARY KEY  (`suggestionid`),
   KEY `suggestedby` (`suggestedby`),
   KEY `managedby` (`managedby`)
@@ -2475,12 +2475,12 @@ CREATE TABLE `permissions` (
 
 DROP TABLE IF EXISTS `serialitems`;
 CREATE TABLE `serialitems` (
-	`itemnumber` int(11) NOT NULL,
-	`serialid` int(11) NOT NULL,
-	UNIQUE KEY `serialitemsidx` (`itemnumber`),
-	KEY `serialitems_sfk_1` (`serialid`),
-	CONSTRAINT `serialitems_sfk_1` FOREIGN KEY (`serialid`) REFERENCES `serial` (`serialid`) ON DELETE CASCADE ON UPDATE CASCADE,
-	CONSTRAINT `serialitems_sfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE
+    `itemnumber` int(11) NOT NULL,
+    `serialid` int(11) NOT NULL,
+    UNIQUE KEY `serialitemsidx` (`itemnumber`),
+    KEY `serialitems_sfk_1` (`serialid`),
+    CONSTRAINT `serialitems_sfk_1` FOREIGN KEY (`serialid`) REFERENCES `serial` (`serialid`) ON DELETE CASCADE ON UPDATE CASCADE,
+    CONSTRAINT `serialitems_sfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 --
@@ -3430,4 +3430,3 @@ CREATE TABLE IF NOT EXISTS `misc_files` ( -- miscellaneous files attached to rec
 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 3f1af57..1657982 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -8603,6 +8603,14 @@ if ( CheckVersion($DBversion) ) {
     SetVersion($DBversion);
 }
 
+$DBversion = "XXX";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do("ALTER TABLE  `items` CHANGE  `cn_sort`  `cn_sort` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL");
+    $dbh->do("ALTER TABLE  `deleteditems` CHANGE  `cn_sort`  `cn_sort` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL");
+    print "Upgrade to $DBversion done (Bug 12424 - ddc sorting of call numbers truncates long Cutter parts)\n";
+    SetVersion ($DBversion);
+}
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)
-- 
1.9.1