From 680fd4b59b5d7fdbf22d887a9ca10376329b3f95 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 17 Jun 2019 12:37:21 -0300
Subject: [PATCH] Bug 23133: DB update

---
 Koha/Schema/Result/OldReserve.pm              |  8 ++++----
 Koha/Schema/Result/Reserve.pm                 | 14 +++++++-------
 .../bug_23133_item_level_holds.perl           | 19 +++++++++++++++++++
 installer/data/mysql/kohastructure.sql        |  4 ++--
 4 files changed, 32 insertions(+), 13 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_23133_item_level_holds.perl

diff --git a/Koha/Schema/Result/OldReserve.pm b/Koha/Schema/Result/OldReserve.pm
index 24ca45a5ed..3b65aade80 100644
--- a/Koha/Schema/Result/OldReserve.pm
+++ b/Koha/Schema/Result/OldReserve.pm
@@ -137,7 +137,7 @@ __PACKAGE__->table("old_reserves");
   is_nullable: 1
   size: 10
 
-=head2 item_level_hold
+=head2 item_level
 
   data_type: 'tinyint'
   default_value: 0
@@ -198,7 +198,7 @@ __PACKAGE__->add_columns(
   },
   "itemtype",
   { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 },
-  "item_level_hold",
+  "item_level",
   { data_type => "tinyint", default_value => 0, is_nullable => 0 },
 );
 
@@ -297,8 +297,8 @@ __PACKAGE__->belongs_to(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-06-17 07:24:39
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZgGAW7ODBby3hGNJ41eeMA
+# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-06-17 15:35:57
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rQMv2lyDOGwH9gDqrzLdmA
 
 
 # You can replace this text with custom content, and it will be preserved on regeneration
diff --git a/Koha/Schema/Result/Reserve.pm b/Koha/Schema/Result/Reserve.pm
index 213df0816b..3c7c4f08da 100644
--- a/Koha/Schema/Result/Reserve.pm
+++ b/Koha/Schema/Result/Reserve.pm
@@ -141,7 +141,7 @@ __PACKAGE__->table("reserves");
   is_nullable: 1
   size: 10
 
-=head2 item_level_hold
+=head2 item_level
 
   data_type: 'tinyint'
   default_value: 0
@@ -212,7 +212,7 @@ __PACKAGE__->add_columns(
   },
   "itemtype",
   { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 },
-  "item_level_hold",
+  "item_level",
   { data_type => "tinyint", default_value => 0, is_nullable => 0 },
 );
 
@@ -321,8 +321,8 @@ __PACKAGE__->belongs_to(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-06-17 07:24:39
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:OMFqEWyKqFTcYj7vAFXy/g
+# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-06-17 15:35:57
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:5rdCr4lZTl6JUrJ/LujZQA
 
 __PACKAGE__->belongs_to(
   "item",
@@ -349,9 +349,9 @@ __PACKAGE__->belongs_to(
 );
 
 __PACKAGE__->add_columns(
-    '+item_level_hold' => { is_boolean => 1 },
-    '+lowestPriority'  => { is_boolean => 1 },
-    '+suspend'         => { is_boolean => 1 }
+    '+item_level'     => { is_boolean => 1 },
+    '+lowestPriority' => { is_boolean => 1 },
+    '+suspend'        => { is_boolean => 1 }
 );
 
 1;
diff --git a/installer/data/mysql/atomicupdate/bug_23133_item_level_holds.perl b/installer/data/mysql/atomicupdate/bug_23133_item_level_holds.perl
new file mode 100644
index 0000000000..1c9dadc34e
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_23133_item_level_holds.perl
@@ -0,0 +1,19 @@
+$DBversion = 'XXX';
+if( CheckVersion( $DBversion ) ) {
+
+    if ( column_exists( 'reserves', 'item_level_hold' ) ) {
+        $dbh->do(q{
+            ALTER TABLE reserves
+                CHANGE COLUMN `item_level_hold` `item_level` TINYINT(1) NOT NULL DEFAULT 0;
+        });
+    }
+    if ( column_exists( 'old_reserves', 'item_level_hold' ) ) {
+        $dbh->do(q{
+            ALTER TABLE old_reserves
+                CHANGE COLUMN `item_level_hold` `item_level` TINYINT(1) NOT NULL DEFAULT 0;
+        });
+    }
+
+    SetVersion( $DBversion );
+    print "Upgrade to $DBversion done (Bug 23133 - Rename reserves.item_level_hold for reserves.item_level)\n";
+}
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index e3d0930369..579efcd5fc 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1882,7 +1882,7 @@ CREATE TABLE `reserves` ( -- information related to holds/reserves in Koha
   `suspend` BOOLEAN NOT NULL DEFAULT 0,
   `suspend_until` DATETIME NULL DEFAULT NULL,
   `itemtype` VARCHAR(10) NULL DEFAULT NULL, -- If record level hold, the optional itemtype of the item the patron is requesting
-  `item_level_hold` BOOLEAN NOT NULL DEFAULT 0, -- Is the hpld placed at item level
+  `item_level` TINYINT(1) NOT NULL DEFAULT 0, -- Is the hold placed at item level
   PRIMARY KEY (`reserve_id`),
   KEY priorityfoundidx (priority,found),
   KEY `borrowernumber` (`borrowernumber`),
@@ -1922,7 +1922,7 @@ CREATE TABLE `old_reserves` ( -- this table holds all holds/reserves that have b
   `suspend` BOOLEAN NOT NULL DEFAULT 0, -- in this hold suspended (1 for yes, 0 for no)
   `suspend_until` DATETIME NULL DEFAULT NULL, -- the date this hold is suspended until (NULL for infinitely)
   `itemtype` VARCHAR(10) NULL DEFAULT NULL, -- If record level hold, the optional itemtype of the item the patron is requesting
-  `item_level_hold` BOOLEAN NOT NULL DEFAULT 0, -- Is the hpld placed at item level
+  `item_level` TINYINT(1) NOT NULL DEFAULT 0, -- Is the hold placed at item level
   PRIMARY KEY (`reserve_id`),
   KEY `old_reserves_borrowernumber` (`borrowernumber`),
   KEY `old_reserves_biblionumber` (`biblionumber`),
-- 
2.22.0