3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * MySQL engine conversion tool.
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(dirname(dirname(dirname(__FILE__
))).'/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),
38 array('h'=>'help', 'l'=>'list'));
41 $unrecognized = implode("\n ", $unrecognized);
42 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
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.
53 --engine=ENGINE Convert MySQL tables to different engine
54 -l, --list Show table information
55 -h, --help Print out this help
58 \$sudo -u www-data /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB
61 if (!empty($options['engine'])) {
62 $engine = clean_param($options['engine'], PARAM_ALPHA
);
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);
71 foreach ($rs as $table) {
72 if ($table->engine
=== $engine) {
73 echo str_pad($table->name
, 40). " - NO CONVERSION NEEDED\n";
77 echo str_pad($table->name
, 40). " - ";
80 $DB->change_database_structure("ALTER TABLE {$table->name} ENGINE = $engine");
81 } catch (moodle_exception
$e) {
82 echo $e->getMessage()."\n";
90 echo "Converted: $converted, skipped: $skipped\n";
93 } else if (!empty($options['list'])) {
94 echo "List of tables for $CFG->wwwroot:\n";
95 $prefix = $DB->get_prefix();
96 $prefix = str_replace('_', '\\_', $prefix);
97 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'";
98 $rs = $DB->get_recordset_sql($sql);
100 foreach ($rs as $table) {
101 if (isset($counts[$table->engine
])) {
102 $counts[$table->engine
]++
;
104 $counts[$table->engine
] = 1;
106 echo str_pad($table->engine
, 10);
107 echo $table->name
. "\n";
112 echo "Table engines summary for $CFG->wwwroot:\n";
113 foreach ($counts as $engine => $count) {
114 echo "$engine: $count\n";