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/>.
20 * General database mover class
24 * @copyright 2008 Andrei Bautu
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 class database_mover extends database_exporter {
31 /** Importer object used to transfer data. */
38 * @param moodle_database $mdb Connection to the source database (a
39 * @see moodle_database object).
40 * @param moodle_database $mdb_target Connection to the target database (a
41 * @see moodle_database object).
42 * @param boolean $check_schema - whether or not to check that XML database
43 * schema matches the RDBMS database schema before exporting (used by
44 * @see export_database).
46 public function __construct(moodle_database $mdb_source, moodle_database $mdb_target,
47 $check_schema = true, progress_trace $feeback = null) {
48 if (empty($feeback)) {
49 $this->feeback = new null_progress_trace();
51 $this->feeback = $feeback;
54 $this->feeback->output(get_string('checkingsourcetables', 'dbtransfer'));
56 parent::__construct($mdb_source, $check_schema);
57 $this->feeback->output(get_string('creatingtargettables', 'dbtransfer'));
58 $this->importer = new database_importer($mdb_target, $check_schema);
62 * How to use transactions during the transfer.
63 * @param string $mode 'pertable', 'allinone' or 'none'.
65 public function set_transaction_mode($mode) {
66 $this->importer->set_transaction_mode($mode);
70 * Callback function. Calls importer's begin_database_import callback method.
72 * @param float $version the version of the system which generating the data
73 * @param string $timestamp the timestamp of the data (in ISO 8601) format.
74 * @param string $description a user description of the data.
77 public function begin_database_export($version, $release, $timestamp, $description) {
78 $this->feeback->output(get_string('copyingtables', 'dbtransfer'));
79 $this->importer->begin_database_import($version, $timestamp, $description);
83 * Callback function. Calls importer's begin_table_import callback method.
85 * @param xmldb_table $table - XMLDB object for the exported table
88 public function begin_table_export(xmldb_table $table) {
89 $this->feeback->output(get_string('copyingtable', 'dbtransfer', $table->getName()), 1);
90 $this->importer->begin_table_import($table->getName(), $table->getHash());
94 * Callback function. Calls importer's import_table_data callback method.
96 * @param xmldb_table $table - XMLDB object of the table from which data
98 * @param object $data - data object (fields and values from record)
101 public function export_table_data(xmldb_table $table, $data) {
102 $this->importer->import_table_data($table->getName(), $data);
106 * Callback function. Calls importer's finish_table_import callback method.
107 * @param xmldb_table $table - XMLDB object for the exported table
110 public function finish_table_export(xmldb_table $table) {
111 $this->feeback->output(get_string('done', 'dbtransfer', $table->getName()), 2);
112 $this->importer->finish_table_import($table->getName());
116 * Callback function. Calls importer's finish_database_import callback method.
119 public function finish_database_export() {
120 $this->importer->finish_database_import();