MDL-32572 fix notice when changing internal auth_db passwords
[moodle.git] / admin / cli / mysql_engine.php
blob3b51f41344600529ef64e8db3bdaf80afeeebdb5
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(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'));
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 -h, --help Print out this help
57 Example:
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);
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;
77 echo str_pad($table->name, 40). " - ";
79 try {
80 $DB->change_database_structure("ALTER TABLE {$table->name} ENGINE = $engine");
81 } catch (moodle_exception $e) {
82 echo $e->getMessage()."\n";
83 $skipped++;
84 continue;
86 echo "DONE\n";
87 $converted++;
89 $rs->close();
90 echo "Converted: $converted, skipped: $skipped\n";
91 exit(0); // success
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);
99 $counts = array();
100 foreach ($rs as $table) {
101 if (isset($counts[$table->engine])) {
102 $counts[$table->engine]++;
103 } else {
104 $counts[$table->engine] = 1;
106 echo str_pad($table->engine, 10);
107 echo $table->name . "\n";
109 $rs->close();
111 echo "\n";
112 echo "Table engines summary for $CFG->wwwroot:\n";
113 foreach ($counts as $engine => $count) {
114 echo "$engine: $count\n";
116 exit(0); // success
118 } else {
119 echo $help;
120 die;