forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_importer.php
214 lines (200 loc) · 8.32 KB
/
database_importer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* General database importer class
*
* @package core_dtl
* @copyright 2008 Andrei Bautu
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Base class for database import operations. This class implements
* basic callbacks for import operations and defines the @see import_database
* method as a common method for all importers. In general, subclasses will
* override import_database and call other methods in appropriate moments.
* Between a single pair of calls to @see begin_database_import and
* @see finish_database_import, multiple non-overlapping pairs of calls may
* be made to @see begin_table_import and @see finish_database_import for
* different tables.
* Between one pair of calls to @see begin_table_import and
* @see finish_database_import multiple calls may be made to
* @see import_table_data for the same table.
* This class can be used directly, if the standard control flow (defined above)
* is respected.
*/
class database_importer {
/** @var moodle_database Connection to the target database (a @see moodle_database object). */
protected $mdb;
/** @var database_manager Database manager of the target database (a @see database_manager object). */
protected $manager;
/** @var xmldb_structure Target database schema in XMLDB format (a @see xmldb_structure object). */
protected $schema;
/**
* Boolean flag - whether or not to check that XML database schema matches
* the RDBMS database schema before importing (used by
* @see begin_database_import).
* @var bool
*/
protected $check_schema;
/** @var string How to use transactions. */
protected $transactionmode = 'allinone';
/** @var moodle_transaction Transaction object */
protected $transaction;
/**
* Object constructor.
*
* @param moodle_database $mdb Connection to the target database (a
* @see moodle_database object). Use null to use the current $DB connection.
* @param boolean $check_schema - whether or not to check that XML database
* schema matches the RDBMS database schema before importing (inside
* @see begin_database_import).
*/
public function __construct(moodle_database $mdb, $check_schema=true) {
$this->mdb = $mdb;
$this->manager = $mdb->get_manager();
$this->schema = $this->manager->get_install_xml_schema();
$this->check_schema = $check_schema;
}
/**
* How to use transactions during the import.
* @param string $mode 'pertable', 'allinone' or 'none'.
*/
public function set_transaction_mode($mode) {
if (!in_array($mode, array('pertable', 'allinone', 'none'))) {
throw new coding_exception('Unknown transaction mode', $mode);
}
$this->transactionmode = $mode;
}
/**
* Callback function. Should be called only once database per import
* operation, before any database changes are made. It will check the database
* schema if @see check_schema is true
*
* @throws dbtransfer_exception if any checking (e.g. database schema, Moodle
* version) fails
*
* @param float $version the version of the system which generated the data
* @param string $timestamp the timestamp of the data (in ISO 8601) format.
* @return void
*/
public function begin_database_import($version, $timestamp) {
global $CFG;
if (!$this->mdb->get_tables()) {
// No tables present yet, time to create all tables.
$this->manager->install_from_xmldb_structure($this->schema);
}
if (round($version, 2) !== round($CFG->version, 2)) { // version might be in decimal format too
$a = (object)array('schemaver'=>$version, 'currentver'=>$CFG->version);
throw new dbtransfer_exception('importversionmismatchexception', $a);
}
$options = array('changedcolumns' => false); // Column types may be fixed by transfer.
if ($this->check_schema and $errors = $this->manager->check_database_schema($this->schema, $options)) {
$details = '';
foreach ($errors as $table=>$items) {
$details .= '<div>'.get_string('table').' '.$table.':';
$details .= '<ul>';
foreach ($items as $item) {
$details .= '<li>'.$item.'</li>';
}
$details .= '</ul></div>';
}
throw new dbtransfer_exception('importschemaexception', $details);
}
if ($this->transactionmode == 'allinone') {
$this->transaction = $this->mdb->start_delegated_transaction();
}
}
/**
* Callback function. Should be called only once per table import operation,
* before any table changes are made. It will delete all table data.
*
* @throws dbtransfer_exception an unknown table import is attempted
* @throws ddl_table_missing_exception if the table is missing
*
* @param string $tablename - the name of the table that will be imported
* @param string $schemaHash - the hash of the xmldb_table schema of the table
* @return void
*/
public function begin_table_import($tablename, $schemaHash) {
if ($this->transactionmode == 'pertable') {
$this->transaction = $this->mdb->start_delegated_transaction();
}
if (!$table = $this->schema->getTable($tablename)) {
throw new dbtransfer_exception('unknowntableexception', $tablename);
}
if ($schemaHash != $table->getHash()) {
throw new dbtransfer_exception('differenttableexception', $tablename);
}
// this should not happen, unless someone drops tables after import started
if (!$this->manager->table_exists($table)) {
throw new ddl_table_missing_exception($tablename);
}
$this->mdb->delete_records($tablename);
}
/**
* Callback function. Should be called only once per table import operation,
* after all table changes are made. It will reset table sequences if any.
* @param string $tablename
* @return void
*/
public function finish_table_import($tablename) {
$table = $this->schema->getTable($tablename);
$fields = $table->getFields();
foreach ($fields as $field) {
if ($field->getSequence()) {
$this->manager->reset_sequence($tablename);
return;
}
}
if ($this->transactionmode == 'pertable') {
$this->transaction->allow_commit();
}
}
/**
* Callback function. Should be called only once database per import
* operation, after all database changes are made. It will commit changes.
* @return void
*/
public function finish_database_import() {
if ($this->transactionmode == 'allinone') {
$this->transaction->allow_commit();
}
}
/**
* Callback function. Should be called only once per record import operation, only
* between @see begin_table_import and @see finish_table_import calls.
* It will insert table data.
*
* @throws dml_exception if data insert operation failed
*
* @param string $tablename - the name of the table in which data will be
* imported
* @param object $data - data object (fields and values will be inserted
* into table)
* @return void
*/
public function import_table_data($tablename, $data) {
$this->mdb->import_record($tablename, $data);
}
/**
* Common import method
* @return void
*/
public function import_database() {
// implement in subclasses
}
}