MDL-31165 fix InnoDB conversion query to use ENGINE parameter instead of legacy TYPE
[moodle.git] / admin / cli / mysql_engine.php
CommitLineData
90f38825
PS
1<?php
2
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.
14//
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/>.
17
18/**
19 * MySQL engine conversion tool.
20 *
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
25 */
26
27define('CLI_SCRIPT', true);
28
29require(dirname(dirname(dirname(__FILE__))).'/config.php');
30require_once($CFG->libdir.'/clilib.php'); // cli only functions
31
32if ($DB->get_dbfamily() !== 'mysql') {
33 cli_error('This function is designed for MySQL databases only!');
34}
35
36// now get cli options
37list($options, $unrecognized) = cli_get_params(array('help'=>false, 'list'=>false, 'engine'=>false),
38 array('h'=>'help', 'l'=>'list'));
39
40if ($unrecognized) {
41 $unrecognized = implode("\n ", $unrecognized);
42 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
43}
44
45$help =
6c2c2b53 46"MySQL engine conversions script.
90f38825
PS
47
48It is recommended to stop the web server before the conversion.
6c2c2b53
PS
49Do not use MyISAM if possible, because it is not ACID compliant
50and does not support transactions.
90f38825
PS
51
52Options:
53--engine=ENGINE Convert MySQL tables to different engine
54-l, --list Show table information
55-h, --help Print out this help
56
57Example:
58\$sudo -u www-data /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB
59";
60
61if (!empty($options['engine'])) {
62 $engine = clean_param($options['engine'], PARAM_ALPHA);
63
64 echo "Converting tables to '$engine' for $CFG->wwwroot:\n";
65 $prefix = $DB->get_prefix();
66 $prefix = str_replace('_', '\\_', $prefix);
67 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'";
68 $rs = $DB->get_recordset_sql($sql);
69 $converted = 0;
70 $skipped = 0;
71 foreach ($rs as $table) {
72 if ($table->engine === $engine) {
73 echo str_pad($table->name, 40). " - NO CONVERSION NEEDED\n";
74 $skipped++;
75 continue;
76 }
77 echo str_pad($table->name, 40). " - ";
78
5f0456ef 79 $DB->change_database_structure("ALTER TABLE {$table->name} ENGINE = $engine");
90f38825
PS
80 echo "DONE\n";
81 $converted++;
82 }
83 $rs->close();
84 echo "Converted: $converted, skipped: $skipped\n";
85 exit(0); // success
86
87} else if (!empty($options['list'])) {
88 echo "List of tables for $CFG->wwwroot:\n";
89 $prefix = $DB->get_prefix();
90 $prefix = str_replace('_', '\\_', $prefix);
91 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'";
92 $rs = $DB->get_recordset_sql($sql);
93 $counts = array();
94 foreach ($rs as $table) {
95 if (isset($counts[$table->engine])) {
96 $counts[$table->engine]++;
97 } else {
98 $counts[$table->engine] = 1;
99 }
100 echo str_pad($table->engine, 10);
101 echo $table->name . "\n";
102 }
103 $rs->close();
104
105 echo "\n";
106 echo "Table engines summary for $CFG->wwwroot:\n";
107 foreach ($counts as $engine => $count) {
108 echo "$engine: $count\n";
109 }
110 exit(0); // success
111
112} else {
113 echo $help;
114 die;
115}