MDL-11137 Unable to locate API error - add check for empty array from DB interface...
[moodle.git] / lib / db / upgrade.php
CommitLineData
4e423cbf 1<?PHP //$Id$
2
3// This file keeps track of upgrades to Moodle.
4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
8//
9// The upgrade function in this file will attempt
10// to perform all the necessary actions to upgrade
11// your older installtion to the current version.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// The commands in here will all be database-neutral,
b1f93b15 17// using the methods of database_manager class
4e423cbf 18
19
20function xmldb_main_upgrade($oldversion=0) {
f33e1ed4 21 global $CFG, $THEME, $USER, $DB;
4e423cbf 22
23 $result = true;
24
46d318bc 25 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 26
1ea260d8 27 ////////////////////////////////////////
28 ///upgrade supported only from 1.9.x ///
29 ////////////////////////////////////////
da4aa3e4 30
a1f27717 31 if ($result && $oldversion < 2008030700) {
32
33 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
a8cb94f6 34 $table = new xmldb_table('grade_letters');
69b80cc2 35 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
a1f27717 36
37 /// Launch drop index contextid-lowerboundary
eee5d9bb 38 $dbman->drop_index($table, $index);
a1f27717 39
a1f27717 40 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
a8cb94f6 41 $table = new xmldb_table('grade_letters');
69b80cc2 42 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
a1f27717 43
44 /// Launch add index contextid-lowerboundary-letter
eee5d9bb 45 $dbman->add_index($table, $index);
a1f27717 46
47 /// Main savepoint reached
0f77b186 48 upgrade_main_savepoint($result, 2008030700);
49 }
a1f27717 50
1ea260d8 51 if ($result && $oldversion < 2008050100) {
52 // Update courses that used weekscss to weeks
a8cb94f6 53 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
1ea260d8 54 upgrade_main_savepoint($result, 2008050100);
55 }
56
0b51c247 57 if ($result && $oldversion < 2008050200) {
58 // remove unused config options
59 unset_config('statsrolesupgraded');
60 upgrade_main_savepoint($result, 2008050200);
61 }
62
994fbaab 63 if ($result && $oldversion < 2008050700) {
64 /// Fix minor problem caused by MDL-5482.
65 require_once($CFG->dirroot . '/question/upgrade.php');
66 $result = $result && question_fix_random_question_parents();
67 upgrade_main_savepoint($result, 2008050700);
68 }
69
ab37dc60 70 if ($result && $oldversion < 2008051200) {
71 // if guest role used as default user role unset it and force admin to choose new setting
72 if (!empty($CFG->defaultuserroleid)) {
910dd4dd 73 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
ab37dc60 74 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
75 if (isset($guestroles[$role->id])) {
76 set_config('defaultuserroleid', null);
77 notify('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
78 }
79 }
80 } else {
81 set_config('defaultuserroleid', null);
82 }
83 }
84 }
85
8b9cfac4 86 if ($result && $oldversion < 2008051201) {
87 notify('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
88
469dcbcc 89 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
90 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
eee5d9bb 91 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
469dcbcc 92 }
93
8b9cfac4 94 /// Define index idnumber (not unique) to be dropped form user
a8cb94f6 95 $table = new xmldb_table('user');
69b80cc2 96 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
8b9cfac4 97
98 /// Launch drop index idnumber
f33e1ed4 99 if ($dbman->index_exists($table, $index)) {
eee5d9bb 100 $dbman->drop_index($table, $index);
8b9cfac4 101 }
102
103 /// Changing precision of field idnumber on table user to (255)
a8cb94f6 104 $table = new xmldb_table('user');
69b80cc2 105 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'password');
8b9cfac4 106
107 /// Launch change of precision for field idnumber
eee5d9bb 108 $dbman->change_field_precision($table, $field);
8b9cfac4 109
110 /// Launch add index idnumber again
69b80cc2 111 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
eee5d9bb 112 $dbman->add_index($table, $index);
8b9cfac4 113
114 /// Main savepoint reached
115 upgrade_main_savepoint($result, 2008051201);
116 }
117
7ade55ad 118 if ($result && $oldversion < 2008051202) {
910dd4dd 119 $log_action = new object();
7ade55ad 120 $log_action->module = 'course';
121 $log_action->action = 'unenrol';
122 $log_action->mtable = 'course';
123 $log_action->field = 'fullname';
910dd4dd 124 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
125 $result = $result && $DB->insert_record('log_display', $log_action);
7ade55ad 126 }
127 upgrade_main_savepoint($result, 2008051202);
128 }
129
fa162144 130 if ($result && $oldversion < 2008051203) {
131 $table = new xmldb_table('mnet_enrol_course');
132 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', true, true, null, false, false, 0);
eee5d9bb 133 $dbman->change_field_precision($table, $field);
fa162144 134 upgrade_main_savepoint($result, 2008051203);
135 }
136
38fb8190 137 if ($result && $oldversion < 2008063001) {
138 // table to be modified
139 $table = new xmldb_table('tag_instance');
140 // add field
141 $field = new xmldb_field('tiuserid');
142 if (!$dbman->field_exists($table, $field)) {
143 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'itemid');
144 $dbman->add_field($table, $field);
145 }
146 // modify index
147 $index = new xmldb_index('itemtype-itemid-tagid');
148 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
149 $dbman->drop_index($table, $index);
150 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
151 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
152 $dbman->add_index($table, $index);
153
154 /// Main savepoint reached
155 upgrade_main_savepoint($result, 2008063001);
156 }
98978c2e 157 if ($result && $oldversion < 2008063002) {
158
159 /// Define table repository to be created
160 $table = new xmldb_table('repository');
161
162 /// Adding fields to table repository
163 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
164 $table->add_field('repositoryname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
165 $table->add_field('repositorytype', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
166 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
167 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
168 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
169 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
170 $table->add_field('data1', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
171 $table->add_field('data2', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
172 $table->add_field('data3', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
173 $table->add_field('data4', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
174 $table->add_field('data5', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
175 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
176 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
177
178 /// Adding keys to table repository
179 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
180
181 /// Conditionally launch create table for repository
182 if (!$dbman->table_exists($table)) {
183 $dbman->create_table($table);
184 }
38fb8190 185
98978c2e 186 /// Main savepoint reached
187 upgrade_main_savepoint($result, 2008063002);
188 }
38fb8190 189
a036d7f3 190 if ($result && $oldversion < 2008070300) {
191 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
192 upgrade_main_savepoint($result, 2008070300);
193 }
3b120e46 194
fe074586 195 if ($result && $oldversion < 2008070700) {
196 if (isset($CFG->defaultuserroleid) and isset($CFG->guestroleid) and $CFG->defaultuserroleid == $CFG->guestroleid) {
197 // guest can not be selected in defaultuserroleid!
198 unset_config('defaultuserroleid');
199 }
200 upgrade_main_savepoint($result, 2008070700);
201 }
202
ac5efef6 203 if ($result && $oldversion < 2008070701) {
204
205 /// Define table portfolio_instance to be created
206 $table = new xmldb_table('portfolio_instance');
207
208 /// Adding fields to table portfolio_instance
209 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
210 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, null, null);
211 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
212 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
213
214 /// Adding keys to table portfolio_instance
215 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
216
217 /// Conditionally launch create table for portfolio_instance
218 if (!$dbman->table_exists($table)) {
219 $dbman->create_table($table);
220 }
221 /// Define table portfolio_instance_config to be created
222 $table = new xmldb_table('portfolio_instance_config');
223
224 /// Adding fields to table portfolio_instance_config
225 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
226 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
227 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
228 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
229
230 /// Adding keys to table portfolio_instance_config
231 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
232 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
233
234 /// Adding indexes to table portfolio_instance_config
235 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
236
237 /// Conditionally launch create table for portfolio_instance_config
238 if (!$dbman->table_exists($table)) {
239 $dbman->create_table($table);
240 }
241
242 /// Define table portfolio_instance_user to be created
243 $table = new xmldb_table('portfolio_instance_user');
244
245 /// Adding fields to table portfolio_instance_user
246 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
247 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
248 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
249 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
250 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
251
252 /// Adding keys to table portfolio_instance_user
253 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
254 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
255 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
256
257 /// Conditionally launch create table for portfolio_instance_user
258 if (!$dbman->table_exists($table)) {
259 $dbman->create_table($table);
260 }
261
262 /// Main savepoint reached
263 upgrade_main_savepoint($result, 2008070701);
264 }
3b120e46 265
266 if ($result && $oldversion < 2008072400) {
267 /// Create the database tables for message_processors and message_providers
268 $table = new XMLDBTable('message_providers');
269 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
270 $table->addFieldInfo('modulename', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
271 $table->addFieldInfo('modulefile', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
272 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
273 $dbman->create_table($table);
274
275 $table = new XMLDBTable('message_processors');
276 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
277 $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
278 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
279 $dbman->create_table($table);
280
281
282 $provider = new object();
283 $provider->modulename = 'moodle';
284 $provider->modulefile = 'index.php';
285 $DB->insert_record('message_providers', $provider);
286
287 /// delete old and create new fields
288 $table = new XMLDBTable('message');
289 $field = new XMLDBField('messagetype');
290 $dbman->drop_field($table, $field);
291
292 /// fields to rename
293 $field = new XMLDBField('message');
294 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
295 $dbman->rename_field($table, $field, 'fullmessage');
296 $field = new XMLDBField('format');
297 $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0', null);
298 $dbman->rename_field($table, $field, 'fullmessageformat');
299
300 /// new message fields
301 $field = new XMLDBField('subject');
302 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
303 $dbman->add_field($table, $field);
304 $field = new XMLDBField('fullmessagehtml');
305 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, null);
306 $dbman->add_field($table, $field);
307 $field = new XMLDBField('smallmessage');
308 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
309 $dbman->add_field($table, $field);
310
311
312 $table = new XMLDBTable('message_read');
313 $field = new XMLDBField('messagetype');
314 $dbman->drop_field($table, $field);
315 $field = new XMLDBField('mailed');
316 $dbman->drop_field($table, $field);
317
318 /// fields to rename
319 $field = new XMLDBField('message');
320 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
321 $dbman->rename_field($table, $field, 'fullmessage');
322 $field = new XMLDBField('format');
323 $field->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0', null);
324 $dbman->rename_field($table, $field, 'fullmessageformat');
325
326
327 /// new message fields
328 $field = new XMLDBField('subject');
329 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
330 $dbman->add_field($table, $field);
331 $field = new XMLDBField('fullmessagehtml');
332 $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, null);
333 $dbman->add_field($table, $field);
334 $field = new XMLDBField('smallmessage');
335 $field->setAttributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
336 $dbman->add_field($table, $field);
337
338 /// new table
339 $table = new XMLDBTable('message_working');
340 $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
341 $table->addFieldInfo('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
342 $table->addFieldInfo('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
343 $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
344 $dbman->create_table($table);
345
346
347 upgrade_main_savepoint($result, 2008072400);
348 }
349
ac5efef6 350
f33e1ed4 351/*
352 * TODO:
eb636785 353 * drop adodb_logsql table and create a new general sql log table
f33e1ed4 354 *
355 */
356
ac9b0805 357 return $result;
4e423cbf 358}
271e6dec 359
360
4e423cbf 361?>