From 7c70eb8e89b2fe89425fd10ae69a995ca354c022 Mon Sep 17 00:00:00 2001
From: Leon Stringer <leon.stringer@ntlworld.com>
Date: Fri, 28 Jun 2024 16:00:06 +0100
Subject: [PATCH] MDL-78776 database: Remove MyISAM migration

Remove functionality to migrate from MyISAM, and remove tests of whether
the existing database is using this.

Support for MyISAM was dropped in Moodle 2.9 (MDL-46064) so it should
not be possible for any subsequent Moodle version to be using this.

The only remaining MyISAM-aware code is the environment check via
check_database_storage_engine() to prevent installation/upgrade if the
database is set to use this.
---
 .upgradenotes/MDL-78776-2024062711440326.yml  |  5 ++
 .../tool/innodb/classes/privacy/provider.php  | 46 ----------
 admin/tool/innodb/index.php                   | 84 -------------------
 admin/tool/innodb/lang/en/tool_innodb.php     | 27 ------
 admin/tool/innodb/settings.php                | 30 -------
 admin/tool/innodb/version.php                 | 30 -------
 config-dist.php                               |  2 +-
 lang/en/deprecated.txt                        |  1 +
 lang/en/error.php                             |  4 +-
 lib/db/upgrade.php                            | 11 +++
 lib/dml/mysqli_native_moodle_database.php     | 34 --------
 lib/dml/tests/dml_mysqli_read_slave_test.php  |  4 -
 lib/plugins.json                              |  2 +-
 13 files changed, 22 insertions(+), 258 deletions(-)
 create mode 100644 .upgradenotes/MDL-78776-2024062711440326.yml
 delete mode 100644 admin/tool/innodb/classes/privacy/provider.php
 delete mode 100644 admin/tool/innodb/index.php
 delete mode 100644 admin/tool/innodb/lang/en/tool_innodb.php
 delete mode 100644 admin/tool/innodb/settings.php
 delete mode 100644 admin/tool/innodb/version.php

diff --git a/.upgradenotes/MDL-78776-2024062711440326.yml b/.upgradenotes/MDL-78776-2024062711440326.yml
new file mode 100644
index 00000000000..8371a602636
--- /dev/null
+++ b/.upgradenotes/MDL-78776-2024062711440326.yml
@@ -0,0 +1,5 @@
+issueNumber: MDL-78776
+notes:
+  tool:
+    - message: The Convert to InnoDB plugin (tool_innodb) has been completely removed.
+      type: removed
diff --git a/admin/tool/innodb/classes/privacy/provider.php b/admin/tool/innodb/classes/privacy/provider.php
deleted file mode 100644
index a356d6c817d..00000000000
--- a/admin/tool/innodb/classes/privacy/provider.php
+++ /dev/null
@@ -1,46 +0,0 @@
-<?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/>.
-
-/**
- * Privacy Subsystem implementation for tool_innodb.
- *
- * @package    tool_innodb
- * @copyright  2018 Zig Tan <zig@moodle.com>
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-namespace tool_innodb\privacy;
-
-defined('MOODLE_INTERNAL') || die();
-
-/**
- * Privacy Subsystem for tool_innodb implementing null_provider.
- *
- * @copyright  2018 Zig Tan <zig@moodle.com>
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class provider implements \core_privacy\local\metadata\null_provider {
-
-    /**
-     * Get the language string identifier with the component's language
-     * file to explain why this plugin stores no data.
-     *
-     * @return  string
-     */
-    public static function get_reason(): string {
-        return 'privacy:metadata';
-    }
-}
\ No newline at end of file
diff --git a/admin/tool/innodb/index.php b/admin/tool/innodb/index.php
deleted file mode 100644
index d17c47665bf..00000000000
--- a/admin/tool/innodb/index.php
+++ /dev/null
@@ -1,84 +0,0 @@
-<?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/>.
-
-/**
- * InnoDB conversion tool.
- *
- * @package    tool
- * @subpackage innodb
- * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-define('NO_OUTPUT_BUFFERING', true);
-
-require_once('../../../config.php');
-require_once($CFG->libdir.'/adminlib.php');
-
-admin_externalpage_setup('toolinnodb');
-
-$confirm = optional_param('confirm', 0, PARAM_BOOL);
-
-
-echo $OUTPUT->header();
-echo $OUTPUT->heading('Convert all MySQL tables from MYISAM to InnoDB');
-
-if ($DB->get_dbfamily() != 'mysql') {
-    notice('This function is for MySQL databases only!', new moodle_url('/admin/'));
-}
-
-$prefix = str_replace('_', '\\_', $DB->get_prefix()).'%';
-$sql = "SHOW TABLE STATUS WHERE Name LIKE ? AND Engine <> 'InnoDB'";
-$rs = $DB->get_recordset_sql($sql, array($prefix));
-if (!$rs->valid()) {
-    $rs->close();
-    echo $OUTPUT->box('<p>All tables are already using InnoDB database engine.</p>');
-    echo $OUTPUT->continue_button('/admin/');
-    echo $OUTPUT->footer();
-    die;
-}
-
-if (data_submitted() and $confirm and confirm_sesskey()) {
-
-    echo $OUTPUT->notification('Please be patient and wait for this to complete...', 'notifysuccess');
-
-    core_php_time_limit::raise();
-
-    foreach ($rs as $table) {
-        $DB->set_debug(true);
-        $fulltable = $table->name;
-        try {
-            $DB->change_database_structure("ALTER TABLE $fulltable ENGINE=INNODB");
-        } catch (moodle_exception $e) {
-            echo $OUTPUT->notification(s($e->getMessage()).'<br />'.s($e->debuginfo));
-        }
-        $DB->set_debug(false);
-    }
-    $rs->close();
-    echo $OUTPUT->notification('... done.', 'notifysuccess');
-    echo $OUTPUT->continue_button(new moodle_url('/admin/'));
-    echo $OUTPUT->footer();
-
-} else {
-    $rs->close();
-    $optionsyes = array('confirm'=>'1', 'sesskey'=>sesskey());
-    $formcontinue = new single_button(new moodle_url('/admin/tool/innodb/index.php', $optionsyes), get_string('yes'));
-    $formcancel = new single_button(new moodle_url('/admin/'), get_string('no'), 'get');
-    echo $OUTPUT->confirm('Are you sure you want convert all your tables to the InnoDB format?', $formcontinue, $formcancel);
-    echo $OUTPUT->footer();
-}
-
-
diff --git a/admin/tool/innodb/lang/en/tool_innodb.php b/admin/tool/innodb/lang/en/tool_innodb.php
deleted file mode 100644
index de5e9ef5e72..00000000000
--- a/admin/tool/innodb/lang/en/tool_innodb.php
+++ /dev/null
@@ -1,27 +0,0 @@
-<?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/>.
-
-/**
- * Strings for component 'tool_generator', language 'en', branch 'MOODLE_22_STABLE'
- *
- * @package    tool
- * @subpackage innodb
- * @copyright  2011 Petr Skoda
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-$string['pluginname'] = 'Convert to InnoDB';
-$string['privacy:metadata'] = 'The Convert to InnoDB plugin does not store any personal data.';
diff --git a/admin/tool/innodb/settings.php b/admin/tool/innodb/settings.php
deleted file mode 100644
index 254f8f041ad..00000000000
--- a/admin/tool/innodb/settings.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?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/>.
-
-/**
- * Link to InnoDB conversion tool
- *
- * @package    tool
- * @subpackage innodb
- * @copyright  2010 Petr Skoda {@link http://skodak.org}
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-defined('MOODLE_INTERNAL') || die;
-
-if ($hassiteconfig) {
-    $ADMIN->add('unsupported', new admin_externalpage('toolinnodb', 'Convert to InnoDB', $CFG->wwwroot.'/'.$CFG->admin.'/tool/innodb/index.php', 'moodle/site:config', true));
-}
diff --git a/admin/tool/innodb/version.php b/admin/tool/innodb/version.php
deleted file mode 100644
index 2a967deb99b..00000000000
--- a/admin/tool/innodb/version.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?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/>.
-
-/**
- * Version details.
- *
- * @package    tool
- * @subpackage innodb
- * @copyright  2011 Petr Skoda
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-defined('MOODLE_INTERNAL') || die();
-
-$plugin->version   = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
-$plugin->requires  = 2024041600; // Requires this Moodle version.
-$plugin->component = 'tool_innodb'; // Full name of the plugin (used for diagnostics)
diff --git a/config-dist.php b/config-dist.php
index ec2708cde80..8ed93fcfc29 100644
--- a/config-dist.php
+++ b/config-dist.php
@@ -328,7 +328,7 @@ $CFG->admin = 'admin';
 //
 //
 // Following settings may be used to select session driver, uncomment only one of the handlers.
-//   Database session handler (not compatible with MyISAM):
+//   Database session handler:
 //      $CFG->session_handler_class = '\core\session\database';
 //      $CFG->session_database_acquire_lock_timeout = 120;
 //
diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt
index 31c9828e288..16687fa6685 100644
--- a/lang/en/deprecated.txt
+++ b/lang/en/deprecated.txt
@@ -114,3 +114,4 @@ blocknotexist,core_debug
 modulenotexist,core_debug
 coursecalendar,core_calendar
 datechanged,core
+myisamproblem,core_error
diff --git a/lang/en/error.php b/lang/en/error.php
index 09764f33a61..5e7adbf0206 100644
--- a/lang/en/error.php
+++ b/lang/en/error.php
@@ -439,7 +439,6 @@ $string['multiplerecordsfound'] = 'Multiple records found, only one record expec
 $string['multiplerestorenotallow'] = 'Multiple restore execution not allowed!';
 $string['mustbeloggedin'] = 'You must be logged in to do this';
 $string['mustbeteacher'] = 'You must be a teacher to look at this page';
-$string['myisamproblem'] = 'Database tables are using MyISAM database engine, it is recommended to use ACID compliant engine with full transaction support such as InnoDB.';
 $string['needcopy'] = 'You need to copy something first!';
 $string['needcoursecategroyid'] = 'Either course id or category must be specified';
 $string['needphpext'] = 'You need to add {$a} support to your PHP installation';
@@ -658,3 +657,6 @@ $string['prefixtoolong'] = '<p>Error: database table prefix is too long ({$a->db
 // Deprecated since Moodle 4.4.
 $string['cannotmarktopic'] = 'Could not mark that section for this course';
 $string['unknownhelp'] = 'Unknown help section {$a}';
+
+// Deprecated since Moodle 4.5.
+$string['myisamproblem'] = 'Database tables are using MyISAM database engine, it is recommended to use ACID compliant engine with full transaction support such as InnoDB.';
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index 22b4c9bdb8d..51d3639a7e0 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -1170,5 +1170,16 @@ function xmldb_main_upgrade($oldversion) {
     // Automatically generated Moodle v4.4.0 release upgrade line.
     // Put any upgrade step following this.
 
+    if ($oldversion < 2024062000.00) {
+        // If tool_innodb is no longer present, remove it.
+        if (!file_exists($CFG->dirroot . '/admin/tool/innodb/version.php')) {
+            // Delete tool_innodb.
+            uninstall_plugin('tool', 'innodb');
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2024062000.00);
+    }
+
     return true;
 }
diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php
index 1fca4f71b2a..9f0aad559ea 100644
--- a/lib/dml/mysqli_native_moodle_database.php
+++ b/lib/dml/mysqli_native_moodle_database.php
@@ -500,40 +500,6 @@ class mysqli_native_moodle_database extends moodle_database {
         return get_string('nativemysqlihelp', 'install');
     }
 
-    /**
-     * Diagnose database and tables, this function is used
-     * to verify database and driver settings, db engine types, etc.
-     *
-     * @return string null means everything ok, string means problem found.
-     */
-    public function diagnose() {
-        $sloppymyisamfound = false;
-        $prefix = str_replace('_', '\\_', $this->prefix);
-        $sql = "SELECT COUNT('x')
-                  FROM INFORMATION_SCHEMA.TABLES
-                 WHERE table_schema = DATABASE()
-                       AND table_name LIKE BINARY '$prefix%'
-                       AND Engine = 'MyISAM'";
-        $this->query_start($sql, null, SQL_QUERY_AUX);
-        $result = $this->mysqli->query($sql);
-        $this->query_end($result);
-        if ($result) {
-            if ($arr = $result->fetch_assoc()) {
-                $count = reset($arr);
-                if ($count) {
-                    $sloppymyisamfound = true;
-                }
-            }
-            $result->close();
-        }
-
-        if ($sloppymyisamfound) {
-            return get_string('myisamproblem', 'error');
-        } else {
-            return null;
-        }
-    }
-
     /**
      * Connect to db
      * @param string $dbhost The database host.
diff --git a/lib/dml/tests/dml_mysqli_read_slave_test.php b/lib/dml/tests/dml_mysqli_read_slave_test.php
index 681fca72d11..23fced0719a 100644
--- a/lib/dml/tests/dml_mysqli_read_slave_test.php
+++ b/lib/dml/tests/dml_mysqli_read_slave_test.php
@@ -126,10 +126,6 @@ class dml_mysqli_read_slave_test extends \base_testcase {
         $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads());
         $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave());
 
-        $db2->diagnose();
-        $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads());
-        $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave());
-
         $db2->get_row_format('course');
         $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads());
         $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave());
diff --git a/lib/plugins.json b/lib/plugins.json
index af427242c78..b98a2b52ba9 100644
--- a/lib/plugins.json
+++ b/lib/plugins.json
@@ -510,7 +510,6 @@
             "filetypes",
             "generator",
             "httpsreplace",
-            "innodb",
             "installaddon",
             "langimport",
             "licensemanager",
@@ -674,6 +673,7 @@
             "assignmentupgrade",
             "bloglevelupgrade",
             "health",
+            "innodb",
             "qeupgradehelper",
             "timezoneimport"
         ],