Follow-up fix for Bug 4473 - Recent comments view for the OPAC
[koha.git] / installer / data / mysql / updatedatabase.pl
index 3180cfc..f43637b 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-
 # Database Updater
 # This script checks for required updates to the database.
 
@@ -66,8 +65,11 @@ my $dbh = C4::Context->dbh;
 $|=1; # flushes output
 
 
-# Deal with virtualshelves
+# Record the version we are coming from
+
+my $original_version = C4::Context->preference("Version");
 
+# Deal with virtualshelves
 my $DBversion = "3.00.00.001";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     # update virtualshelves table to
@@ -2544,7 +2546,7 @@ $DBversion = '3.01.00.045';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do("INSERT INTO systempreferences (variable,value,options,explanation,type)VALUES('HidePatronName', '0', '', 'If this is switched on, patron''s cardnumber will be shown instead of their name on the holds and catalog screens', 'YesNo')");
     SetVersion ($DBversion);
-    print "Upgrade to $DBversion done (added a preference to hide the patrons name in the staff catalog)";
+    print "Upgrade to $DBversion done (added a preference to hide the patrons name in the staff catalog)\n";
 }
 
 $DBversion = "3.01.00.046";
@@ -2714,7 +2716,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
           PRIMARY KEY  (`id`)
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
              ");
-    SetVersion ($DBversion);
+    SetVersion ($DBversion);print "Upgrade to $DBversion done (Created table fieldmapping)\n";print "Upgrade to 3.01.00.064 done (Version number skipped: nothing done)\n";
 }
 
 $DBversion = '3.01.00.065';
@@ -2762,7 +2764,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 $DBversion = "3.01.00.068";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("ALTER TABLE issuingrules ADD COLUMN `finedays` int(11) default NULL AFTER `fine` ");
-       print "Upgrade done (Adding finedays in issuingrules table)\n";
+       print "Upgrade to $DBversion done (Adding finedays in issuingrules table)\n";
     SetVersion ($DBversion);
 }
 
@@ -2785,13 +2787,13 @@ CREATE TABLE IF NOT EXISTS `search_history` (
 SEARCHHIST
        $dbh->do($create);
 
-       print "Upgrade done (added OPAC search history preference and table)\n";
+       print "Upgrade to $DBversion done (added OPAC search history preference and table)\n";
 }
 
 $DBversion = "3.01.00.070";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("ALTER TABLE authorised_values ADD COLUMN `lib_opac` VARCHAR(80) default NULL AFTER `lib`");
-       print "Upgrade done (Added a lib_opac field in authorised_values table)\n";
+       print "Upgrade to $DBversion done (Added a lib_opac field in authorised_values table)\n";
 }
 
 $DBversion = "3.01.00.071";
@@ -3166,14 +3168,14 @@ ALTER table suggestions
     ADD itemtype VARCHAR(30) default NULL
     ;
 SUGGESTIONS
-    print "Upgrade to $DBversion done Suggestions";
+    print "Upgrade to $DBversion done (Suggestions)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = "3.01.00.087";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do("ALTER table aqbudgets drop column budget_amount_sublevel;");
-    print "Upgrade to $DBversion done drop column budget_amount_sublevel from aqbudgets\n";
+    print "Upgrade to $DBversion done (Drop column budget_amount_sublevel from aqbudgets)\n";
     SetVersion ($DBversion);
 }
 
@@ -3316,7 +3318,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 $DBversion = "3.01.00.100";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("INSERT INTO `systempreferences` (`variable`, `value`, `options`, `explanation`, `type`) VALUES ('casAuthentication', '0', '', 'Enable or disable CAS authentication', 'YesNo'), ('casLogout', '1', '', 'Does a logout from Koha should also log out of CAS ?', 'YesNo'), ('casServerUrl', 'https://localhost:8443/cas', '', 'URL of the cas server', 'Free')");
-       print "Upgrade done (added CAS authentication system preferences)\n";
+       print "Upgrade to $DBversion done (added CAS authentication system preferences)\n";
     SetVersion ($DBversion);
 }
 
@@ -3338,14 +3340,14 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do(
     "UPDATE permissions set description = 'Edit catalog (Modify bibliographic/holdings data)' where module_bit = 9 and code = 'edit_catalogue'"
     );
-       print "Upgrade done (fixed spelling error in edit_catalogue permission)\n";
+       print "Upgrade to $DBversion done (fixed spelling error in edit_catalogue permission)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = "3.01.00.103";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("INSERT INTO permissions (module_bit, code, description) VALUES (13, 'moderate_tags', 'Moderate patron tags')");
-       print "Upgrade done (adding patron permissions for tags tool)\n";
+       print "Upgrade to $DBversion done (adding patron permissions for tags tool)\n";
     SetVersion ($DBversion);
 }
 
@@ -3399,7 +3401,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 $DBversion = "3.01.00.106";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES ( 'OpacAddMastheadLibraryPulldown', '0', '', 'Adds a pulldown menu to select the library to search on the opac masthead.', 'YesNo' )");
-       print "Upgrade done (added OpacAddMastheadLibraryPulldown system preferences)\n";
+       print "Upgrade to $DBversion done (added OpacAddMastheadLibraryPulldown system preferences)\n";
     SetVersion ($DBversion);
 }
 
@@ -3418,7 +3420,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        ADD `field_separator` VARCHAR( 2 ) NOT NULL AFTER `csv_separator` ,
        ADD `subfield_separator` VARCHAR( 2 ) NOT NULL AFTER `field_separator` 
        });
-       print "Upgrade done (added separators for csv export)\n";
+       print "Upgrade to $DBversion done (added separators for csv export)\n";
     SetVersion ($DBversion);
 }
 
@@ -3427,27 +3429,27 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do(qq{
        ALTER TABLE `export_format` ADD `encoding` VARCHAR(255) NOT NULL AFTER `subfield_separator`
        });
-       print "Upgrade done (added encoding for csv export)\n";
+       print "Upgrade to $DBversion done (added encoding for csv export)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.01.00.110';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do('ALTER TABLE `categories` ADD COLUMN `enrolmentperioddate` DATE NULL DEFAULT NULL AFTER `enrolmentperiod`');
-    print "Upgrade done (Add enrolment period date support)\n";
+    print "Upgrade to $DBversion done (Add enrolment period date support)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.01.00.111';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
-    print "Upgrade done (mark DBrev for 3.2-alpha release)\n";
+    print "Upgrade to $DBversion done (mark DBrev for 3.2-alpha release)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.01.00.112';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES ('SpineLabelShowPrintOnBibDetails', '0', '', 'If turned on, a \"Print Label\" link will appear for each item on the bib details page in the staff interface.', 'YesNo');");
-       print "Upgrade done ( added Show Spine Label Printer on Bib Items Details preferences )\n";
+       print "Upgrade to $DBversion done ( added Show Spine Label Printer on Bib Items Details preferences )\n";
     SetVersion ($DBversion);
 }
 
@@ -3461,7 +3463,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do(
         "INSERT INTO systempreferences (variable,value,type)
          VALUES('OPACXSLTDetailsDisplay',?,'YesNo')", {}, $value ? 1 : 0);
-    print "Upgrade done (added two new syspref: OPACXSLTResultsDisplay and OPACXSLTDetailDisplay). You may have to go in Admin > System preference to tweak XSLT related syspref both in OPAC and Search tabs.\n     ";
+    print "Upgrade to $DBversion done (added two new syspref: OPACXSLTResultsDisplay and OPACXSLTDetailDisplay). You may have to go in Admin > System preference to tweak XSLT related syspref both in OPAC and Search tabs.\n";
     SetVersion ($DBversion);
 }
 
@@ -3487,7 +3489,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        if (C4::Context->preference('OrderPdfFormat') eq 'pdfformat::example'){
                $dbh->do("UPDATE `systempreferences` set value='pdfformat::layout2pages' WHERE variable='OrderPdfFormat'");
        }
-       print "Upgrade done ( corrected default OrderPdfFormat value if still set wrong )\n";
+       print "Upgrade to $DBversion done (corrected default OrderPdfFormat value if still set wrong )\n";
     SetVersion ($DBversion);
 }
 
@@ -3612,7 +3614,7 @@ $DBversion = "3.01.00.129";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
        $dbh->do("UPDATE `permissions` SET `code` = 'items_batchdel' WHERE `permissions`.`module_bit` =13 AND `permissions`.`code` = 'batchdel' LIMIT 1 ;");
        $dbh->do("UPDATE `permissions` SET `code` = 'items_batchmod' WHERE `permissions`.`module_bit` =13 AND `permissions`.`code` = 'batchmod' LIMIT 1 ;");
-       print "Upgrade done (Change permissions names for item batch modification / deletion)\n";
+       print "Upgrade to $DBversion done (Change permissions names for item batch modification / deletion)\n";
 
     SetVersion ($DBversion);
 }
@@ -3620,7 +3622,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 $DBversion = "3.01.00.130";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do("UPDATE reserves SET expirationdate = NULL WHERE expirationdate = '0000-00-00'");
-    print "Upgrade done (change reserves.expirationdate values of 0000-00-00 to NULL (bug 1532)"; 
+    print "Upgrade to $DBversion done (change reserves.expirationdate values of 0000-00-00 to NULL (bug 1532)\n";
     SetVersion ($DBversion);
 }
 
@@ -3652,7 +3654,7 @@ if (C4::Context->preference('Version') < TransformToNum($DBversion)){
 $DBversion = '3.01.00.134';
 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('DisplayMultiPlaceHold','1','Display the ability to place multiple holds or not','','YesNo')");
-    print "Upgrade to $DBversion done adding syspref DisplayMultiPlaceHold to control whether multiple holds can be placed from the search results page";
+    print "Upgrade to $DBversion done (adding syspref DisplayMultiPlaceHold to control whether multiple holds can be placed from the search results page)\n";
     SetVersion ($DBversion);
 }
 
@@ -3662,7 +3664,7 @@ if (C4::Context->preference('Version') < TransformToNum($DBversion)){
         INSERT INTO `letter` (module, code, name, title, content) VALUES
 ('reserves', 'HOLD_PRINT', 'Hold Available for Pickup (print notice)', 'Hold Available for Pickup (print notice)', '<<branches.branchname>>\r\n<<branches.branchaddress1>>\r\n<<branches.branchaddress2>>\r\n\r\n\r\nChange Service Requested\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<<borrowers.firstname>> <<borrowers.surname>>\r\n<<borrowers.address>>\r\n<<borrowers.city>> <<borrowers.zipcode>>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<<borrowers.firstname>> <<borrowers.surname>> <<borrowers.cardnumber>>\r\n\r\nYou have a hold available for pickup as of <<reserves.waitingdate>>:\r\n\r\nTitle: <<biblio.title>>\r\nAuthor: <<biblio.author>>\r\nCopy: <<items.copynumber>>\r\n')
 ");
-    print "Upgrade to $DBversion done (bug 4377: added HOLD_PRINT message template)";
+    print "Upgrade to $DBversion done (bug 4377: added HOLD_PRINT message template)\n";
     SetVersion ($DBversion);
 }
 
@@ -3671,7 +3673,7 @@ if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do(qq{
 INSERT INTO permissions (module_bit, code, description) VALUES
    ( 9, 'edit_items', 'Edit Items');});
-    print "Upgrade to $DBversion done Adding a new permission to edit items";
+    print "Upgrade to $DBversion done (Adding a new permission to edit items)\n";
     SetVersion ($DBversion);
 }
 
@@ -3688,14 +3690,14 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
           (15, 'renew_subscription', 'Renew a subscription'),
           (15, 'routing', 'Routing');
                  ");
-    print "Upgrade to $DBversion done (adding granular permissions for serials)";
+    print "Upgrade to $DBversion done (adding granular permissions for serials)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = "3.01.00.138";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do("DELETE FROM systempreferences WHERE variable = 'GranularPermissions'");
-    print "Upgrade to $DBversion done (bug 4896: removing GranularPermissions syspref; use of granular permissions is now the default)";
+    print "Upgrade to $DBversion done (bug 4896: removing GranularPermissions syspref; use of granular permissions is now the default)\n";
     SetVersion ($DBversion);
 }
 
@@ -3717,14 +3719,14 @@ $DBversion = '3.01.00.141';
 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do(qq{DELETE FROM message_attributes WHERE message_attribute_id=3;});
     $dbh->do(qq{DELETE FROM letter WHERE code='EVENT' AND title='Upcoming Library Event';});
-    print "Upgrade to $DBversion done Remove upcoming events messaging option (bug 2434)";
+    print "Upgrade to $DBversion done Remove upcoming events messaging option (bug 2434)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.01.00.142';
 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do(qq{DELETE FROM message_transports WHERE message_attribute_id=3;});
-    print "Upgrade to $DBversion done Remove upcoming events messaging option part 2 (bug 2434)";
+    print "Upgrade to $DBversion done (Remove upcoming events messaging option part 2 (bug 2434))\n";
     SetVersion ($DBversion);
 }
 
@@ -3732,14 +3734,14 @@ $DBversion = '3.01.00.143';
 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do(qq{CREATE INDEX auth_value_idx ON authorised_values (authorised_value)});
     $dbh->do(qq{CREATE INDEX auth_val_cat_idx ON borrower_attribute_types (authorised_value_category)});
-    print "Create index on authorised_values and borrower_attribute_types (bug 4139)";
+    print "Upgrade to $DBversion done (Create index on authorised_values and borrower_attribute_types (bug 4139))\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.01.00.144';
 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do(qq{UPDATE systempreferences SET value='normal' where value='default' and variable='IntranetBiblioDefaultView'});
-    print "Update the 'default' to 'normal' for the IntranetBiblioDefaultView syspref (bug 5007)";
+    print "Upgrade to $DBversion done (Update the 'default' to 'normal' for the IntranetBiblioDefaultView syspref (bug 5007))\n";
     SetVersion ($DBversion);
 }
 
@@ -3807,8 +3809,11 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion ($DBversion);
 }
 
+# This is the point where 3.2.x and master diverged, we can use $original_version to make sure we don't
+# apply updates that have already been done
+
 $DBversion = "3.03.00.001";
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.00.005")) {
     $dbh->do("DELETE FROM subscriptionroutinglist WHERE borrowernumber IS NULL;");
     $dbh->do("ALTER TABLE subscriptionroutinglist MODIFY COLUMN `borrowernumber` int(11) NOT NULL;");
     $dbh->do("DELETE FROM subscriptionroutinglist WHERE subscriptionid IS NULL;");
@@ -3835,7 +3840,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 }
 
 $DBversion = '3.03.00.002';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.00.006")) {
     $dbh->do("UPDATE language_rfc4646_to_iso639 SET iso639_2_code='arm' WHERE rfc4646_subtag='hy';");
     $dbh->do("UPDATE language_rfc4646_to_iso639 SET iso639_2_code='eng' WHERE rfc4646_subtag='en';");
     $dbh->do("INSERT INTO language_rfc4646_to_iso639(rfc4646_subtag,iso639_2_code) VALUES( 'fi','fin');");
@@ -3851,14 +3856,14 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 }
 
 $DBversion = '3.03.00.003';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.00.007")) {
     $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('UseTablesortForCirc','0','If on, use the JQuery tablesort function on the list of current borrower checkouts on the circulation page. Note that the use of this function may slow down circ for patrons with may checkouts.','','YesNo');");
     print "Upgrade to $DBversion done (Add UseTablesortForCirc syspref)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.03.00.004';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.01.001")) {
     my $count = $dbh->selectrow_array('SELECT COUNT(*) FROM letter WHERE module = ? AND code = ?', {}, 'suggestions', 'ACCEPTED');
     $dbh->do(q/
 INSERT INTO `letter`
@@ -3898,7 +3903,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 }
 
 $DBversion = '3.03.00.006';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.01.002")) {
     $dbh->do("ALTER TABLE deletedborrowers ADD `privacy` int(11) AFTER smsalertnumber;");
     $dbh->do("ALTER TABLE deletedborrowers CHANGE `cardnumber` `cardnumber` varchar(16);");
     print "Upgrade to $DBversion done (Fix differences between borrowers and deletedborrowers)\n";
@@ -3919,19 +3924,19 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 $DBversion = '3.03.00.008';
 if (C4::Context->preference('Version') < TransformToNum($DBversion)){
     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('OPACNoResultsFound','','Display this HTML when no results are found for a search in the OPAC','70|10','Textarea')");
-    print "Upgrade to $DBversion done adding syspref OPACNoResultsFound to control what displays when no results are found for a search in the OPAC.";
+    print "Upgrade to $DBversion done (adding syspref OPACNoResultsFound to control what displays when no results are found for a search in the OPAC.)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.03.00.009';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.01.003")) {
     $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('IntranetUserCSS','','Add CSS to be included in the Intranet',NULL,'free')");
     print "Upgrade to $DBversion done (Add IntranetUserCSS syspref)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = "3.03.00.010";
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.02.001")) {
     $dbh->do("UPDATE `marc_subfield_structure` SET liblibrarian = 'Distance from earth' WHERE liblibrarian = 'Distrance from earth' AND tagfield = '034' AND tagsubfield = 'r';");
     $dbh->do("UPDATE `marc_subfield_structure` SET libopac = 'Distance from earth' WHERE libopac = 'Distrance from earth' AND tagfield = '034' AND tagsubfield = 'r';");
     print "Upgrade to $DBversion done (Fix misspelled 034r subfield in MARC21 Frameworks)\n";
@@ -3996,14 +4001,14 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 };
 
 $DBversion = '3.03.00.017';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.03.001")) {
     $dbh->do("ALTER TABLE  `currency` CHANGE `rate` `rate` FLOAT( 15, 5 ) NULL DEFAULT NULL;");
     print "Upgrade to $DBversion done (Enable currency rates >= 100)\n";
     SetVersion ($DBversion);
 }
 
 $DBversion = '3.03.00.018';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.03.002")) {
     $dbh->do( q|update language_descriptions set description = 'Nederlands' where lang = 'nl' and subtag = 'nl'|);
     $dbh->do( q|update language_descriptions set description = 'Dansk' where lang = 'da' and subtag = 'da'|);
     print "Upgrade to $DBversion done (Correct language descriptions)\n";
@@ -4011,7 +4016,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 }
 
 $DBversion = '3.03.00.019';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.03.003")) {
     # Fix bokmÃ¥l
     $dbh->do("UPDATE language_subtag_registry SET description = 'Norwegian bokm&#229;l' WHERE subtag = 'nb';");
     $dbh->do("INSERT INTO language_rfc4646_to_iso639(rfc4646_subtag,iso639_2_code) VALUES( 'nb','nob');");
@@ -4038,7 +4043,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 };
 
 $DBversion = '3.03.00.021';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.05.001")) {
     $dbh->do("ALTER TABLE items MODIFY enumchron TEXT");
     $dbh->do("ALTER TABLE deleteditems MODIFY enumchron TEXT");
     print "Upgrade to $DBversion done (bug 5642: longer serial enumeration)\n";
@@ -4055,7 +4060,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 # due to a mismatch in kohastructure.sql some koha will have missing columns in aqbasketgroup
 # this attempts to fix that
 $DBversion = '3.03.00.023';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.05.002")) {
     my $sth = $dbh->prepare("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'aqbasketgroups' AND COLUMN_NAME = 'billingplace'");
     $sth->execute;
     $dbh->do("ALTER TABLE aqbasketgroups ADD billingplace VARCHAR(10)") if ! $sth->fetchrow_hashref;
@@ -4085,7 +4090,7 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 }
 
 $DBversion = '3.03.00.026';
-if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+if (C4::Context->preference("Version") < TransformToNum($DBversion) && $original_version < TransformToNum("3.02.05.003")) {
     $dbh->do("UPDATE `message_attributes` SET message_name='Item Due' WHERE message_attribute_id=1 AND message_name LIKE 'Item DUE'");
        print "Upgrade to $DBversion done ( fix capitalization in message type )\n";
     SetVersion ($DBversion);
@@ -4132,9 +4137,9 @@ $DBversion = '3.03.00.032';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('TraceSubjectSubdivisions', 1, 'Create searches on all subdivisions for subject tracings.','1','YesNo')");
     print "Upgrade to $DBversion done ( include subdivisions when generating subject tracing searches )\n";
-    SetVersion ($DBversion);
 }
 
+
 $DBversion = '3.03.00.033';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('StaffAuthorisedValueImages', '1', '', NULL, 'YesNo')");
@@ -4211,12 +4216,26 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 
 $DBversion = '3.03.00.042';
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
-    $dbh->do("ALTER TABLE `items` DROP INDEX `itemsstocknumberidx`;");
-    $dbh->do("ALTER TABLE items ADD INDEX itemstocknumberidx (stocknumber);");
-    print "Upgrade to $DBversion done (Change items.stocknumber to be not unique)\n";
+    stocknumber_checker();
+    print "Upgrade to $DBversion done (5860 Index itemstocknumber)\n";
     SetVersion ($DBversion);
 }
 
+sub stocknumber_checker { #code reused later on
+  my @row;
+  #drop the obsolete itemSStocknumber idx if it exists
+  @row = $dbh->selectrow_array("SHOW INDEXES FROM items WHERE key_name='itemsstocknumberidx'");
+  $dbh->do("ALTER TABLE `items` DROP INDEX `itemsstocknumberidx`;") if @row;
+
+  #check itemstocknumber idx; remove it if it is unique
+  @row = $dbh->selectrow_array("SHOW INDEXES FROM items WHERE key_name='itemstocknumberidx' AND non_unique=0");
+  $dbh->do("ALTER TABLE `items` DROP INDEX `itemstocknumberidx`;") if @row;
+
+  #add itemstocknumber index non-unique IF it still not exists
+  @row = $dbh->selectrow_array("SHOW INDEXES FROM items WHERE key_name='itemstocknumberidx'");
+  $dbh->do("ALTER TABLE items ADD INDEX itemstocknumberidx (stocknumber);") unless @row;
+}
+
 $DBversion = "3.03.00.043";
 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 
@@ -4304,6 +4323,228 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion ($DBversion);
 }
 
+$DBversion = "3.03.00.052";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('WaitingNotifyAtCheckin',0,'If ON, notify librarians of waiting holds for the patron whose items they are checking in.',NULL,'YesNo');");
+    print "Upgrade to $DBversion done (Add syspref WaitingNotifyAtCheckin)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.04.00.000";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    print "Upgrade to $DBversion done Koha 3.4.0 release \n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.05.00.001";
+if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
+    $dbh->do(qq{
+    INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('numSearchRSSResults',50,'Specify the maximum number of results to display on a RSS page of results',NULL,'Integer');
+    });
+    print "Upgrade to $DBversion done (Adds New System preference numSearchRSSResults)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = '3.05.00.002';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    #follow up fix 5860: some installs already past 3.3.0.42
+    stocknumber_checker();
+    print "Upgrade to $DBversion done (Fix for stocknumber index)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.05.00.003";
+if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
+    $dbh->do(qq{
+    INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('OpacRenewalBranch','checkoutbranch','Choose how the branch for an OPAC renewal is recorded in statistics','itemhomebranch|patronhomebranch|checkoutbranch|null','Choice');
+    });
+    print "Upgrade to $DBversion done (Adds New System preference OpacRenewalBranch)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.004";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('ShowReviewerPhoto',1,'If ON, photo of reviewer will be shown beside comments in OPAC',NULL,'YesNo');");
+    print "Upgrade to $DBversion done (Add syspref ShowReviewerPhoto)\n";
+    SetVersion($DBversion);    
+}
+    
+$DBversion = "3.05.00.005";
+if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES ('BasketConfirmations', '1', 'When closing or reopening a basket,', 'always ask for confirmation.|do not ask for confirmation.', 'Choice');");
+    print "Upgrade to $DBversion done (Adds pref BasketConfirmations)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.006"; 
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('MARCAuthorityControlField008', '|| aca||aabn           | a|a     d', NULL, NULL, 'Textarea')");
+    print "Upgrade to $DBversion done (Add syspref MARCAuthorityControlField008)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.05.00.007";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('OpenLibraryCovers',0,'If ON Openlibrary book covers will be show',NULL,'YesNo');");
+    print "Upgrade to $DBversion done (Add syspref OpenLibraryCovers)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.008";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("ALTER TABLE `cities` ADD `city_state` VARCHAR( 100 ) NULL DEFAULT NULL AFTER  `city_name`;");
+    $dbh->do("ALTER TABLE `cities` ADD `city_country` VARCHAR( 100 ) NULL DEFAULT NULL AFTER  `city_zipcode`;");
+    print "Add state and country to cities table corresponding to new columns in borrowers\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.009";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO old_issues (borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate)
+              SELECT borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate FROM issues WHERE borrowernumber IS NULL");
+    $dbh->do("DELETE FROM issues WHERE borrowernumber IS NULL");
+
+    $dbh->do("INSERT INTO old_issues (borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate)
+              SELECT borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate FROM issues WHERE itemnumber IS NULL");
+    $dbh->do("DELETE FROM issues WHERE itemnumber IS NULL");
+
+    $dbh->do("INSERT INTO old_issues (borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate)
+              SELECT borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate FROM issues WHERE NOT EXISTS (SELECT * FROM borrowers WHERE borrowernumber = issues.borrowernumber)");
+    $dbh->do("DELETE FROM issues WHERE NOT EXISTS (SELECT * FROM borrowers WHERE borrowernumber = issues.borrowernumber)");
+
+    $dbh->do("INSERT INTO old_issues (borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate)
+              SELECT borrowernumber, itemnumber, date_due, branchcode, issuingbranch, returndate, lastreneweddate, `return`, renewals, timestamp, issuedate FROM issues WHERE NOT EXISTS (SELECT * FROM items WHERE itemnumber = issues.itemnumber)");
+    $dbh->do("DELETE FROM issues WHERE NOT EXISTS (SELECT * FROM items WHERE itemnumber = issues.itemnumber)");
+
+    $dbh->do("ALTER TABLE issues DROP FOREIGN KEY `issues_ibfk_1`");
+    $dbh->do("ALTER TABLE issues DROP FOREIGN KEY `issues_ibfk_2`");
+    $dbh->do("ALTER TABLE issues ALTER COLUMN borrowernumber DROP DEFAULT");
+    $dbh->do("ALTER TABLE issues ALTER COLUMN itemnumber DROP DEFAULT");
+    $dbh->do("ALTER TABLE issues MODIFY COLUMN borrowernumber int(11) NOT NULL");
+    $dbh->do("ALTER TABLE issues MODIFY COLUMN itemnumber int(11) NOT NULL");
+    $dbh->do("ALTER TABLE issues DROP KEY `issuesitemidx`");
+    $dbh->do("ALTER TABLE issues ADD PRIMARY KEY (`itemnumber`)");
+    $dbh->do("ALTER TABLE issues ADD CONSTRAINT `issues_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE RESTRICT ON UPDATE CASCADE");
+    $dbh->do("ALTER TABLE issues ADD CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE RESTRICT ON UPDATE CASCADE");
+
+    print "Upgrade to $DBversion done (issues referential integrity)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.05.00.010";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("CREATE INDEX priorityfoundidx ON reserves (priority,found)");
+    print "Create an index on reserves to speed up holds awaiting pickup report bug 5866\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.011";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('OPACResultsSidebar','','Define HTML to be included on the search results page, underneath the facets sidebar','70|10','Textarea')");
+    print "Upgrade to $DBversion done (add OPACResultsSidebar syspref (enh 6165))\n";
+    SetVersion($DBversion);
+}
+    
+$DBversion = "3.05.00.012";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('RecordLocalUseOnReturn',0,'If ON, statistically record returns of unissued items as local use, instead of return',NULL,'YesNo')");
+    print "Upgrade to $DBversion done (add RecordLocalUseOnReturn syspref (enh 6403))\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.013";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do(qq|INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES ('OpacKohaUrl','0',"Show 'Powered by Koha' text on OPAC footer.",NULL,NULL)|);
+    print "Upgrade to $DBversion done (Add syspref 'OpacKohaUrl')\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.014";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("ALTER TABLE `borrowers` MODIFY `userid` VARCHAR(75)");
+    print "Modified userid column length into 75 in borrowers\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.015";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('NovelistSelectEnabled',0,'Enable Novelist Select content.  Requires Novelist Profile and Password',NULL,'YesNo')");
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('NovelistSelectProfile',NULL,'Novelist Select user Password',NULL,'free')");
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('NovelistSelectPassword',NULL,'Enable Novelist user Profile',NULL,'free')");
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('NovelistSelectView','tab','Where to display Novelist Select content','tab|above|below|right','Choice')");
+    print "Upgrade to $DBversion done (Add support for EBSCO's NoveList Select (enh 6902))\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = '3.05.00.016';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('EasyAnalyticalRecords','0','If on, display in the catalogue screens tools to easily setup analytical record relationships','','YesNo');");
+    print "Upgrade to $DBversion done (Add EasyAnalyticalRecords syspref)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = '3.05.00.017';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    if (C4::Context->preference("marcflavour") eq 'MARC21' ||
+        C4::Context->preference("marcflavour") eq 'NORMARC'){
+        $dbh->do("INSERT INTO `marc_subfield_structure` (`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value` , `authtypecode`, `value_builder`, `isurl`, `hidden`, `frameworkcode`, `seealso`, `link`, `defaultvalue`) VALUES ('773', '0', 'Host Biblionumber', 'Host Biblionumber', 0, 0, NULL, 7, NULL, NULL, '', NULL, -6, '', '', '', NULL)");
+        $dbh->do("INSERT INTO `marc_subfield_structure` (`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value` , `authtypecode`, `value_builder`, `isurl`, `hidden`, `frameworkcode`, `seealso`, `link`, `defaultvalue`) VALUES ('773', '9', 'Host Itemnumber', 'Host Itemnumber', 0, 0, NULL, 7, NULL, NULL, '', NULL, -6, '', '', '', NULL)");
+        print "Upgrade to $DBversion done (Add 773 subfield 9 and 0 to default framework)\n";
+        SetVersion ($DBversion);
+    } elsif (C4::Context->preference("marcflavour") eq 'UNIMARC'){
+        $dbh->do("INSERT INTO `marc_subfield_structure` (`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value` , `authtypecode`, `value_builder`, `isurl`, `hidden`, `frameworkcode`, `seealso`, `link`, `defaultvalue`) VALUES ('461', '9', 'Host Itemnumber', 'Host Itemnumber', 0, 0, NULL, 7, NULL, NULL, '', NULL, -6, '', '', '', NULL)");
+        print "Upgrade to $DBversion done (Add 461 subfield 9 to default framework)\n";
+        SetVersion ($DBversion);
+    }
+               
+}
+
+$DBversion = "3.05.00.018";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('OpacNavBottom','','Links after OpacNav links','70|10','Textarea')");
+    print "Upgrade to $DBversion done (add OpacNavBottom syspref (enh 6825): if appropriate, you can split OpacNav into OpacNav and OpacNavBottom)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.019";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("UPDATE itemtypes SET imageurl = 'vokal/Book.png' WHERE imageurl = 'vokal/BOOK.png'");
+    $dbh->do("UPDATE itemtypes SET imageurl = 'vokal/Book-32px.png' WHERE imageurl = 'vokal/BOOK-32px.png'");
+    $dbh->do("UPDATE authorised_values SET imageurl = 'vokal/Book.png' WHERE imageurl = 'vokal/BOOK.png'");
+    $dbh->do("UPDATE authorised_values SET imageurl = 'vokal/Book-32px.png' WHERE imageurl = 'vokal/BOOK-32px.png'");
+    print "Upgrade to $DBversion done (remove duplicate VOKAL Book icons, bug 6862)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.020";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,options,explanation,type) VALUES ('AcqViewBaskets','user','user|branch|all','Define which baskets a user is allowed to view: his own only, any within his branch or all','Choice')");
+    print "Upgrade to $DBversion done (Add syspref AcqViewBaskets)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.021";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("ALTER TABLE borrower_attribute_types ADD COLUMN display_checkout TINYINT(1) NOT NULL DEFAULT '0';");
+    print "Upgrade to $DBversion done (Added a display_checkout field in borrower_attribute_types table)\n"; 
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.022"; 
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("CREATE TABLE need_merge_authorities (id int NOT NULL auto_increment PRIMARY KEY, authid bigint NOT NULL, done tinyint DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8");
+    print "Upgrade to $DBversion done (6094: Fixing ModAuthority problems, add a need_merge_authorities table)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.05.00.XXX";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('OpacShowRecentComments',0,'If ON a link to recent comments will appear in the OPAC masthead',NULL,'YesNo');");
+    print "Upgrade to $DBversion done (Add syspref OpacShowRecentComments. When the preference is turned on a link to recent comments will appear in the OPAC masthead. )\n";
+    SetVersion($DBversion);
+}
+
+
 =head1 FUNCTIONS
 
 =head2 DropAllForeignKeys($table)