Bug 9580: DBRev 3.19.00.019
[koha_ffzg] / installer / data / mysql / updatedatabase.pl
index 5dab69d..0592a94 100755 (executable)
@@ -4941,10 +4941,52 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
 
 $DBversion = "3.07.00.029";
 if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
-    my $installer = C4::Installer->new();
-    my $full_path = C4::Context->config('intranetdir') . "/installer/data/$installer->{dbms}/atomicupdate/oai_sets.sql";
-    my $error     = $installer->load_sql($full_path);
-    warn $error if $error;
+    $dbh->do(q{DROP TABLE IF EXISTS `oai_sets_descriptions`;});
+    $dbh->do(q{DROP TABLE IF EXISTS `oai_sets_mappings`;});
+    $dbh->do(q{DROP TABLE IF EXISTS `oai_sets_biblios`;});
+    $dbh->do(q{DROP TABLE IF EXISTS `oai_sets`;});
+
+    $dbh->do(q{
+        CREATE TABLE `oai_sets` (
+          `id` int(11) NOT NULL auto_increment,
+          `spec` varchar(80) NOT NULL UNIQUE,
+          `name` varchar(80) NOT NULL,
+          PRIMARY KEY (`id`)
+        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+    });
+
+    $dbh->do(q{
+        CREATE TABLE `oai_sets_descriptions` (
+          `set_id` int(11) NOT NULL,
+          `description` varchar(255) NOT NULL,
+          CONSTRAINT `oai_sets_descriptions_ibfk_1` FOREIGN KEY (`set_id`) REFERENCES `oai_sets` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
+        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+    });
+
+    $dbh->do(q{
+        CREATE TABLE `oai_sets_mappings` (
+          `set_id` int(11) NOT NULL,
+          `marcfield` char(3) NOT NULL,
+          `marcsubfield` char(1) NOT NULL,
+          `marcvalue` varchar(80) NOT NULL,
+          CONSTRAINT `oai_sets_mappings_ibfk_1` FOREIGN KEY (`set_id`) REFERENCES `oai_sets` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
+        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+    });
+
+    $dbh->do(q{
+        CREATE TABLE `oai_sets_biblios` (
+          `biblionumber` int(11) NOT NULL,
+          `set_id` int(11) NOT NULL,
+          PRIMARY KEY (`biblionumber`, `set_id`),
+          CONSTRAINT `oai_sets_biblios_ibfk_1` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`) ON DELETE CASCADE ON UPDATE CASCADE,
+          CONSTRAINT `oai_sets_biblios_ibfk_2` FOREIGN KEY (`set_id`) REFERENCES `oai_sets` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
+        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+    });
+
+    $dbh->do(q{
+        INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('OAI-PMH:AutoUpdateSets','0','Automatically update OAI sets when a bibliographic record is created or updated','','YesNo');
+    });
+
     print "Upgrade to $DBversion done (Atomic update for OAI-PMH sets management)\n";
     SetVersion($DBversion);
 }
@@ -8922,7 +8964,7 @@ if ( CheckVersion($DBversion) ) {
 $DBversion = "3.17.00.040";
 if ( CheckVersion($DBversion) ) {
     my $opac_theme = C4::Context->preference( 'opacthemes' );
-    if ( $opac_theme eq 'prog' || $opac_theme eq 'ccsr' ) {
+    if ( !defined $opac_theme || $opac_theme eq 'prog' || $opac_theme eq 'ccsr' ) {
         $dbh->do("UPDATE systempreferences SET value='bootstrap' WHERE variable='opacthemes'");
     }
     print "Upgrade to $DBversion done (Bug 12223: 'prog' and 'ccsr' themes removed)\n";
@@ -9576,7 +9618,7 @@ if ( CheckVersion($DBversion) ) {
 $DBversion = "3.18.00.000";
 if ( CheckVersion($DBversion) ) {
     print "Upgrade to $DBversion done (3.18.0 release)\n";
-    SetVersion ($DBversion);
+    SetVersion($DBversion);
 }
 
 $DBversion = "3.19.00.000";
@@ -9628,6 +9670,321 @@ if ( CheckVersion($DBversion) ) {
     SetVersion($DBversion);
 }
 
+$DBversion = "3.19.00.004";
+if ( CheckVersion($DBversion) ) {
+    my $pref_value = C4::Context->preference('OpacExportOptions');
+    $pref_value =~ s/\|/,/g; # multiple is separated by ,
+    $dbh->do(q{
+        UPDATE systempreferences
+            SET value = ?,
+                type = 'multiple'
+        WHERE variable = 'OpacExportOptions'
+    }, {}, $pref_value );
+    print "Upgrade to $DBversion done (Bug 13346: OpacExportOptions is now multiple)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.19.00.005";
+if(CheckVersion($DBversion)) {
+    $dbh->do(q{
+        ALTER TABLE authorised_values MODIFY COLUMN category VARCHAR(32) NOT NULL DEFAULT ''
+    });
+
+    $dbh->do(q{
+        ALTER TABLE borrower_attribute_types MODIFY COLUMN authorised_value_category VARCHAR(32) DEFAULT NULL
+    });
+
+    print "Upgrade to $DBversion done (Bug 13379 - Modify authorised_values.category to varchar(32))\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.006";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|SET foreign_key_checks = 0|);
+    my $sth = $dbh->table_info( '','','','TABLE' );
+    my ( $cat, $schema, $name, $type, $remarks );
+    while ( ( $cat, $schema, $name, $type, $remarks ) = $sth->fetchrow_array ) {
+        my $table_sth = $dbh->prepare(qq|SHOW CREATE TABLE $name|);
+        $table_sth->execute;
+        my @table = $table_sth->fetchrow_array;
+        unless ( $table[1] =~ /COLLATE=utf8mb4_unicode_ci/ ) { #catches utf8mb4 collated tables
+            $dbh->do(qq|ALTER TABLE $name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci|);
+        }
+    }
+    $dbh->do(q|SET foreign_key_checks = 1|);;
+
+    print "Upgrade to $DBversion done (Bug 11944 - Convert DB tables to utf8_unicode_ci)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.007";
+if ( CheckVersion($DBversion) ) {
+    my $orphan_budgets = $dbh->selectall_arrayref(q|
+        SELECT budget_id, budget_name, budget_code
+        FROM aqbudgets
+        WHERE   budget_parent_id IS NOT NULL
+            AND budget_parent_id NOT IN (
+                SELECT DISTINCT budget_id FROM aqbudgets
+            )
+    |, { Slice => {} } );
+
+    if ( @$orphan_budgets ) {
+        for my $b ( @$orphan_budgets ) {
+            print "Fund $b->{budget_name} (code:$b->{budget_code}, id:$b->{budget_id}) does not have a parent, it may cause problem\n";
+        }
+        print "Upgrade to $DBversion done (Bug 12905: Check budget integrity: FAIL)\n";
+    } else {
+        print "Upgrade to $DBversion done (Bug 12905: Check budget integrity: OK)\n";
+    }
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.008";
+if ( CheckVersion($DBversion) ) {
+    my $number_of_orders_not_linked = $dbh->selectcol_arrayref(q|
+        SELECT COUNT(*)
+        FROM aqorders o
+        WHERE NOT EXISTS (
+            SELECT NULL
+            FROM aqbudgets b
+            WHERE b.budget_id = o.budget_id
+        );
+    |);
+
+    if ( $number_of_orders_not_linked->[0] > 0 ) {
+        $dbh->do(q|
+            INSERT INTO aqbudgetperiods(budget_period_startdate, budget_period_enddate, budget_period_active, budget_period_description, budget_period_total) VALUES ( CAST(NOW() AS date), CAST(NOW() AS date), 0, "WARNING: This budget has been automatically created by the updatedatabase script, please see bug 12601 for more information", 0)
+        |);
+        my $budget_period_id = $dbh->last_insert_id( undef, undef, 'aqbudgetperiods', undef );
+        $dbh->do(qq|
+            INSERT INTO aqbudgets(budget_code, budget_name, budget_amount, budget_period_id) VALUES ( "BACKUP_TMP", "WARNING: fund created by the updatedatabase script, please see bug 12601", 0, $budget_period_id );
+        |);
+        my $budget_id = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef );
+        $dbh->do(qq|
+            UPDATE aqorders o
+            SET budget_id = $budget_id
+            WHERE NOT EXISTS (
+                SELECT NULL
+                FROM aqbudgets b
+                WHERE b.budget_id = o.budget_id
+            )
+        |);
+    }
+
+    $dbh->do(q|
+        ALTER TABLE aqorders
+        ADD CONSTRAINT aqorders_budget_id_fk FOREIGN KEY (budget_id) REFERENCES aqbudgets(budget_id) ON DELETE CASCADE ON UPDATE CASCADE
+    |);
+
+    print "Upgrade to $DBversion done (Bug 12601 - Add new foreign key aqorders.budget_id" . ( ( $number_of_orders_not_linked->[0] > 0 )  ? ' WARNING: temporary budget and fund have been created (search for "BACKUP_TMP"). At least one of your order was not linked to a budget' : '' ) . ")\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.009";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|
+        UPDATE suggestions s SET s.budgetid = NULL
+        WHERE NOT EXISTS (
+            SELECT NULL
+            FROM aqbudgets b
+            WHERE b.budget_id = s.budgetid
+        );
+    |);
+
+    $dbh->do(q|
+        ALTER TABLE suggestions
+        ADD CONSTRAINT suggestions_budget_id_fk FOREIGN KEY (budgetid) REFERENCES aqbudgets(budget_id) ON DELETE SET NULL ON UPDATE CASCADE
+    |);
+
+    print "Upgrade to $DBversion done (Bug 13007 - Add new foreign key suggestions.budgetid)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.010";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('SessionRestrictionByIP','1','Check for Change in  Remote IP address for Session Security. Disable when remote ip address changes frequently.','','YesNo')");
+    print "Upgrade to $DBversion done (Bug 5511 - SessionRestrictionByIP)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.19.00.011";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|
+        INSERT INTO userflags (bit, flag, flagdesc, defaulton) VALUES
+        (20, 'lists', 'Lists', 0)
+    |);
+    $dbh->do(q|
+        INSERT INTO permissions (module_bit, code, description) VALUES
+        (20, 'delete_public_lists', 'Delete public lists')
+    |);
+    print "Upgrade to $DBversion done (Bug 13417: Add permission to delete public lists)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.19.00.012";
+if(CheckVersion($DBversion)) {
+    $dbh->do(q{
+        ALTER TABLE biblioitems MODIFY COLUMN marcxml longtext
+    });
+
+    $dbh->do(q{
+        ALTER TABLE deletedbiblioitems MODIFY COLUMN marcxml longtext
+    });
+
+    print "Upgrade to $DBversion done (Bug 13523 - Remove NOT NULL restriction on field marcxml due to mysql STRICT_TRANS_TABLES)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.013";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|
+        INSERT INTO permissions (module_bit, code, description) VALUES
+          (13, 'records_batchmod', 'Perform batch modification of records (biblios or authorities)')
+    |);
+    print "Upgrade to $DBversion done (Bug 11395: Add permission tools_records_batchmod)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.014";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|
+        CREATE TABLE aqorder_users (
+            ordernumber int(11) NOT NULL,
+            borrowernumber int(11) NOT NULL,
+            PRIMARY KEY (ordernumber, borrowernumber),
+            CONSTRAINT aqorder_users_ibfk_1 FOREIGN KEY (ordernumber) REFERENCES aqorders (ordernumber) ON DELETE CASCADE ON UPDATE CASCADE,
+            CONSTRAINT aqorder_users_ibfk_2 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE
+        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+    |);
+
+    $dbh->do(q|
+        INSERT INTO letter(module, code, branchcode, name, title, content, message_transport_type)
+        VALUES ('acquisition', 'ACQ_NOTIF_ON_RECEIV', '', 'Notification on receiving', 'Order received', 'Dear <<borrowers.firstname>> <<borrowers.surname>>,\n\n The order <<aqorders.ordernumber>> (<<biblio.title>>) has been received.\n\nYour library.', 'email')
+    |);
+    print "Upgrade to $DBversion done (Bug 12648: Add letter ACQ_NOTIF_ON_RECEIV )\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.19.00.015";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|
+        ALTER TABLE search_history ADD COLUMN id INT(11) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY(id);
+    |);
+    print "Upgrade to $DBversion done (Bug 11430 - Add primary key for search_history)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = "3.19.00.016";
+if(CheckVersion($DBversion)) {
+    $dbh->do(q{
+        INSERT INTO authorised_values (category, authorised_value, lib) VALUES
+         ('ORDER_CANCELLATION_REASON', 0, 'No reason provided'),
+         ('ORDER_CANCELLATION_REASON', 1, 'Out of stock'),
+         ('ORDER_CANCELLATION_REASON', 2, 'Restocking')
+    });
+
+    my $already_existing_reasons = $dbh->selectcol_arrayref(q{
+        SELECT DISTINCT( cancellationreason )
+        FROM aqorders;
+    }, { Slice => {} });
+
+    my $update_orders_sth = $dbh->prepare(q{
+        UPDATE aqorders
+        SET cancellationreason = ?
+        WHERE cancellationreason = ?
+    });
+
+    my $insert_av_sth = $dbh->prepare(q{
+        INSERT INTO authorised_values (category, authorised_value, lib) VALUES
+         ('ORDER_CANCELLATION_REASON', ?, ?)
+    });
+    my $i = 3;
+    for my $reason ( @$already_existing_reasons ) {
+        next unless $reason;
+        $insert_av_sth->execute( $i, $reason );
+        $update_orders_sth->execute( $i, $reason );
+        $i++;
+    }
+    print "Upgrade to $DBversion done (Bug 13380: Add the ORDER_CANCELLATION_REASON authorised value)\n";
+    SetVersion($DBversion);
+}
+
+$DBversion = '3.19.00.017';
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    # First create the column
+    $dbh->do("ALTER TABLE issuingrules ADD onshelfholds tinyint(1) default 0 NOT NULL");
+    # Now update the column
+    if (C4::Context->preference("AllowOnShelfHolds")){
+        # Pref is on, set allow for all rules
+        $dbh->do("UPDATE issuingrules SET onshelfholds=1");
+    } else {
+        # If the preference is not set, leave off
+        $dbh->do("UPDATE issuingrules SET onshelfholds=0");
+    }
+    # Remove from the systempreferences table
+    $dbh->do("DELETE FROM systempreferences WHERE variable = 'AllowOnShelfHolds'");
+
+    # First create the column
+    $dbh->do("ALTER TABLE issuingrules ADD opacitemholds char(1) DEFAULT 'N' NOT NULL");
+    # Now update the column
+    my $opacitemholds = C4::Context->preference("OPACItemHolds") || '';
+    if (lc ($opacitemholds) eq 'force') {
+        $opacitemholds = 'F';
+    }
+    else {
+        $opacitemholds = $opacitemholds ? 'Y' : 'N';
+    }
+    # Set allow for all rules
+    $dbh->do("UPDATE issuingrules SET opacitemholds='$opacitemholds'");
+
+    # Remove from the systempreferences table
+    $dbh->do("DELETE FROM systempreferences WHERE variable = 'OPACItemHolds'");
+
+    print "Upgrade to $DBversion done (Bug 5786 - Move AllowOnShelfHolds to circulation matrix; Move OPACItemHolds system preference to circulation matrix)\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.19.00.018";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q|
+        UPDATE systempreferences set variable="OpacAdditionalStylesheet" WHERE variable="opaccolorstylesheet"
+    |);
+    print "Upgrade to $DBversion done (Bug 10328: Rename opaccolorstylesheet to OpacAdditionalStylesheet\n";
+    SetVersion ($DBversion);
+}
+
+$DBversion = "3.19.00.019";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type)
+        VALUES('Coce','0', 'If on, enables cover retrieval from the configured Coce server', NULL, 'YesNo')
+    });
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type)
+        VALUES('CoceHost', NULL, 'Coce server URL', NULL,'Free')
+    });
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type)
+        VALUES('CoceProviders', NULL, 'Coce providers', 'aws,gb,ol', 'multiple')
+    });
+    print "Upgrade to $DBversion done (Bug 9580: Cover image from Coce, a remote image URL cache)\n";
+    SetVersion($DBversion);
+}
+
+
+# DEVELOPER PROCESS, search for anything to execute in the db_update directory
+# SEE bug 13068
+# if there is anything in the atomicupdate, read and execute it.
+
+my $update_dir = C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/';
+opendir( my $dirh, $update_dir );
+while ( my $file = readdir $dirh ) {
+    next unless $file =~ /\.sql$/;    # skip non SQL files
+    print "DEV atomic update : $file \n";
+    my $installer = C4::Installer->new();
+    my $rv = $installer->load_sql( $update_dir . $file ) ? 0 : 1;
+}
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)