Merge branch 'MDL-62945-master' of https://github.com/HuongNV13/moodle
[moodle.git] / admin / cli / mysql_engine.php
blobd392b0b6f71af3e5eaccebaaabbbe2042aea8513
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * MySQL engine conversion tool.
21 * @package core
22 * @subpackage cli
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define('CLI_SCRIPT', true);
29 require(__DIR__.'/../../config.php');
30 require_once($CFG->libdir.'/clilib.php'); // cli only functions
32 if ($DB->get_dbfamily() !== 'mysql') {
33 cli_error('This function is designed for MySQL databases only!');
36 // now get cli options
37 list($options, $unrecognized) = cli_get_params(array('help'=>false, 'list'=>false, 'engine'=>false, 'available'=>false),
38 array('h'=>'help', 'l'=>'list', 'a'=>'available'));
40 if ($unrecognized) {
41 $unrecognized = implode("\n ", $unrecognized);
42 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
45 $help =
46 "MySQL engine conversions script.
48 It is recommended to stop the web server before the conversion.
49 Do not use MyISAM if possible, because it is not ACID compliant
50 and does not support transactions.
52 Options:
53 --engine=ENGINE Convert MySQL tables to different engine
54 -l, --list Show table information
55 -a, --available Show list of available engines
56 -h, --help Print out this help
58 Example:
59 \$sudo -u www-data /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB
62 if (!empty($options['engine'])) {
63 $engines = mysql_get_engines();
64 $engine = clean_param($options['engine'], PARAM_ALPHA);
65 if (!isset($engines[strtoupper($engine)])) {
66 cli_error("Error: engine '$engine' is not available on this server!");
69 echo "Converting tables to '$engine' for $CFG->wwwroot:\n";
70 $prefix = $DB->get_prefix();
71 $prefix = str_replace('_', '\\_', $prefix);
72 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'";
73 $rs = $DB->get_recordset_sql($sql);
74 $converted = 0;
75 $skipped = 0;
76 $errors = 0;
77 foreach ($rs as $table) {
78 if (strtoupper($table->engine) === strtoupper($engine)) {
79 $newengine = mysql_get_table_engine($table->name);
80 echo str_pad($table->name, 40). " - NO CONVERSION NEEDED ($newengine)\n";
81 $skipped++;
82 continue;
84 echo str_pad($table->name, 40). " - ";
86 try {
87 $DB->change_database_structure("ALTER TABLE {$table->name} ENGINE = $engine");
88 $newengine = mysql_get_table_engine($table->name);
89 if (strtoupper($newengine) !== strtoupper($engine)) {
90 echo "ERROR ($newengine)\n";
91 $errors++;
92 continue;
94 echo "DONE ($newengine)\n";
95 $converted++;
96 } catch (moodle_exception $e) {
97 echo $e->getMessage()."\n";
98 $errors++;
99 continue;
102 $rs->close();
103 echo "Converted: $converted, skipped: $skipped, errors: $errors\n";
104 exit(0); // success
106 } else if (!empty($options['list'])) {
107 echo "List of tables for $CFG->wwwroot:\n";
108 $prefix = $DB->get_prefix();
109 $prefix = str_replace('_', '\\_', $prefix);
110 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'";
111 $rs = $DB->get_recordset_sql($sql);
112 $counts = array();
113 foreach ($rs as $table) {
114 if (isset($counts[$table->engine])) {
115 $counts[$table->engine]++;
116 } else {
117 $counts[$table->engine] = 1;
119 echo str_pad($table->engine, 10);
120 echo $table->name . "\n";
122 $rs->close();
124 echo "\n";
125 echo "Table engines summary for $CFG->wwwroot:\n";
126 foreach ($counts as $engine => $count) {
127 echo "$engine: $count\n";
129 exit(0); // success
131 } else if (!empty($options['available'])) {
132 echo "List of available MySQL engines for $CFG->wwwroot:\n";
133 $engines = mysql_get_engines();
134 foreach ($engines as $engine) {
135 echo " $engine\n";
137 die;
139 } else {
140 echo $help;
141 die;
146 // ========== Some functions ==============
148 function mysql_get_engines() {
149 global $DB;
151 $sql = "SHOW Engines";
152 $rs = $DB->get_recordset_sql($sql);
153 $engines = array();
154 foreach ($rs as $engine) {
155 if (strtoupper($engine->support) !== 'YES' and strtoupper($engine->support) !== 'DEFAULT') {
156 continue;
158 $engines[strtoupper($engine->engine)] = $engine->engine;
159 if (strtoupper($engine->support) === 'DEFAULT') {
160 $engines[strtoupper($engine->engine)] .= ' (default)';
163 $rs->close();
165 return $engines;
168 function mysql_get_table_engine($tablename) {
169 global $DB;
171 $engine = null;
172 $sql = "SHOW TABLE STATUS WHERE Name = '$tablename'"; // no special chars expected here
173 $rs = $DB->get_recordset_sql($sql);
174 if ($rs->valid()) {
175 $record = $rs->current();
176 $engine = $record->engine;
178 $rs->close();
179 return $engine;