From c35e04dd110419a5733905230892ed48cad7cb3e Mon Sep 17 00:00:00 2001
From: Renaud Lemaire <rlemaire@cblue.be>
Date: Fri, 12 Feb 2021 15:17:36 +0100
Subject: [PATCH] #314689 add indexes to simpleseach db

---
 search/engine/simpledb/db/install.xml |  5 ++-
 search/engine/simpledb/db/upgrade.php | 57 +++++++++++++++++++++++++++
 search/engine/simpledb/version.php    |  2 +-
 3 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 search/engine/simpledb/db/upgrade.php

diff --git a/search/engine/simpledb/db/install.xml b/search/engine/simpledb/db/install.xml
index 21393311b31..75f3e206844 100644
--- a/search/engine/simpledb/db/install.xml
+++ b/search/engine/simpledb/db/install.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="search/engine/simpledb/db" VERSION="20160408" COMMENT="XMLDB file for Moodle search/engine/simpledb"
+<XMLDB PATH="search/engine/simpledb/db" VERSION="20210201" COMMENT="XMLDB file for Moodle search/engine/simpledb"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
 >
@@ -27,6 +27,9 @@
       </KEYS>
       <INDEXES>
         <INDEX NAME="owneruserid-contextid" UNIQUE="false" FIELDS="owneruserid, contextid" COMMENT="Query filters if no extra search filters are applied"/>
+        <INDEX NAME="contextid" UNIQUE="false" FIELDS="contextid"/>
+        <INDEX NAME="courseid" UNIQUE="false" FIELDS="courseid"/>
+        <INDEX NAME="areaid" UNIQUE="false" FIELDS="areaid"/>
       </INDEXES>
     </TABLE>
   </TABLES>
diff --git a/search/engine/simpledb/db/upgrade.php b/search/engine/simpledb/db/upgrade.php
new file mode 100644
index 00000000000..60b8b63d57d
--- /dev/null
+++ b/search/engine/simpledb/db/upgrade.php
@@ -0,0 +1,57 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+function xmldb_search_simpledb_upgrade($oldversion = 0) {
+    global $CFG, $DB, $OUTPUT;
+
+    $dbman = $DB->get_manager();
+
+    if ($oldversion < 2020061501) {
+
+        $table = new xmldb_table('search_simpledb_index');
+
+        // Define index areaid (not unique) to be added to search_simpledb_index.
+        $index = new xmldb_index('contextid', XMLDB_INDEX_NOTUNIQUE, ['contextid']);
+
+        // Conditionally launch add index contextid.
+        if (!$dbman->index_exists($table, $index)) {
+            $dbman->add_index($table, $index);
+        }
+
+        // Define index courseid (not unique) to be added to search_simpledb_index.
+        $index = new xmldb_index('courseid', XMLDB_INDEX_NOTUNIQUE, ['courseid']);
+
+        // Conditionally launch add index courseid.
+        if (!$dbman->index_exists($table, $index)) {
+            $dbman->add_index($table, $index);
+        }
+
+        // Define index areaid (not unique) to be added to search_simpledb_index.
+        $index = new xmldb_index('areaid', XMLDB_INDEX_NOTUNIQUE, ['areaid']);
+
+        // Conditionally launch add index areaid.
+        if (!$dbman->index_exists($table, $index)) {
+            $dbman->add_index($table, $index);
+        }
+
+
+        // Simpledb savepoint reached.
+        upgrade_plugin_savepoint(true, 2020061501, 'search', 'simpledb');
+    }
+
+
+    return true;
+}
diff --git a/search/engine/simpledb/version.php b/search/engine/simpledb/version.php
index 82ab0dd74fb..126c54d3c68 100644
--- a/search/engine/simpledb/version.php
+++ b/search/engine/simpledb/version.php
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2020061500;
+$plugin->version = 2020061501;
 $plugin->requires = 2020060900;
 $plugin->component = 'search_simpledb';
-- 
2.25.1

