Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Validate that the current db structure matches the install.xml files.
  19   *
  20   * @package   core
  21   * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @author    Petr Skoda <petr.skoda@totaralms.com>
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__ . '/../../config.php');
  29  require_once($CFG->libdir.'/clilib.php');
  30  
  31  $help = "Validate database structure
  32  
  33  Options:
  34  -h, --help            Print out this help.
  35  
  36  Example:
  37  \$ sudo -u www-data /usr/bin/php admin/cli/check_database_schema.php
  38  ";
  39  
  40  list($options, $unrecognized) = cli_get_params(
  41      array(
  42          'help' => false,
  43      ),
  44      array(
  45          'h' => 'help',
  46      )
  47  );
  48  
  49  if ($options['help']) {
  50      echo $help;
  51      exit(0);
  52  }
  53  
  54  if (empty($CFG->version)) {
  55      echo "Database is not yet installed.\n";
  56      exit(2);
  57  }
  58  
  59  $dbmanager = $DB->get_manager();
  60  $schema = $dbmanager->get_install_xml_schema();
  61  
  62  if (!$errors = $dbmanager->check_database_schema($schema)) {
  63      echo "Database structure is ok.\n";
  64      exit(0);
  65  }
  66  
  67  foreach ($errors as $table => $items) {
  68      cli_separator();
  69      echo "$table\n";
  70      foreach ($items as $item) {
  71          echo " * $item\n";
  72      }
  73  }
  74  cli_separator();
  75  
  76  exit(1);