prevent NULL contents before field change (old servers can have this wrongly defined...
[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,
17// using the functions defined in lib/ddllib.php
18
19
20function xmldb_main_upgrade($oldversion=0) {
21
8a45fcdf 22 global $CFG, $THEME, $USER, $db;
4e423cbf 23
24 $result = true;
25
1ea260d8 26 ////////////////////////////////////////
27 ///upgrade supported only from 1.9.x ///
28 ////////////////////////////////////////
da4aa3e4 29
a1f27717 30 if ($result && $oldversion < 2008030700) {
31
32 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
33 $table = new XMLDBTable('grade_letters');
34 $index = new XMLDBIndex('contextid-lowerboundary');
35 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
36
37 /// Launch drop index contextid-lowerboundary
38 $result = $result && drop_index($table, $index);
39
a1f27717 40 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
41 $table = new XMLDBTable('grade_letters');
42 $index = new XMLDBIndex('contextid-lowerboundary-letter');
43 $index->setAttributes(XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
44
45 /// Launch add index contextid-lowerboundary-letter
46 $result = $result && add_index($table, $index);
47
48 /// Main savepoint reached
0f77b186 49 upgrade_main_savepoint($result, 2008030700);
50 }
a1f27717 51
1ea260d8 52 if ($result && $oldversion < 2008050100) {
53 // Update courses that used weekscss to weeks
54 $result = set_field('course', 'format', 'weeks', 'format', 'weekscss');
55 upgrade_main_savepoint($result, 2008050100);
56 }
57
0b51c247 58 if ($result && $oldversion < 2008050200) {
59 // remove unused config options
60 unset_config('statsrolesupgraded');
61 upgrade_main_savepoint($result, 2008050200);
62 }
63
994fbaab 64 if ($result && $oldversion < 2008050700) {
65 /// Fix minor problem caused by MDL-5482.
66 require_once($CFG->dirroot . '/question/upgrade.php');
67 $result = $result && question_fix_random_question_parents();
68 upgrade_main_savepoint($result, 2008050700);
69 }
70
ab37dc60 71 if ($result && $oldversion < 2008051200) {
72 // if guest role used as default user role unset it and force admin to choose new setting
73 if (!empty($CFG->defaultuserroleid)) {
74 if ($role = get_record('role', 'id', $CFG->defaultuserroleid)) {
75 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
76 if (isset($guestroles[$role->id])) {
77 set_config('defaultuserroleid', null);
78 notify('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
79 }
80 }
81 } else {
82 set_config('defaultuserroleid', null);
83 }
84 }
85 }
86
8b9cfac4 87 if ($result && $oldversion < 2008051201) {
88 notify('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
89
469dcbcc 90 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
91 if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') {
92 execute_sql("UPDATE {$CFG->prefix}user SET idnumber = '' WHERE idnumber IS NULL", true);
93 }
94
8b9cfac4 95 /// Define index idnumber (not unique) to be dropped form user
96 $table = new XMLDBTable('user');
97 $index = new XMLDBIndex('idnumber');
98 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
99
100 /// Launch drop index idnumber
101 if (index_exists($table, $index)) {
102 $result = $result && drop_index($table, $index);
103 }
104
105 /// Changing precision of field idnumber on table user to (255)
106 $table = new XMLDBTable('user');
107 $field = new XMLDBField('idnumber');
108 $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'password');
109
110 /// Launch change of precision for field idnumber
111 $result = $result && change_field_precision($table, $field);
112
113 /// Launch add index idnumber again
114 $index = new XMLDBIndex('idnumber');
115 $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
116 $result = $result && add_index($table, $index);
117
118 /// Main savepoint reached
119 upgrade_main_savepoint($result, 2008051201);
120 }
121
ac9b0805 122 return $result;
4e423cbf 123}
271e6dec 124
125
4e423cbf 126?>