Merged branch wip-MDL-30521 with conflict resolutions
[moodle.git] / lib / db / upgrade.php
CommitLineData
5b4a78e2 1<?php
4e423cbf 2
5b4a78e2 3// This file is part of Moodle - http://moodle.org/
4e423cbf 4//
5b4a78e2
PS
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.
4e423cbf 9//
5b4a78e2
PS
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.
4e423cbf 14//
5b4a78e2
PS
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/>.
17
18/**
19 * This file keeps track of upgrades to Moodle.
20 *
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
24 *
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
27 * your older installation to the current version.
28 *
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
31 *
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
34 *
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
37 *
38 * @package core
39 * @subpackage admin
40 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43
44defined('MOODLE_INTERNAL') || die();
4e423cbf 45
3406acde
SH
46/**
47 *
48 * @global stdClass $CFG
49 * @global stdClass $USER
50 * @global moodle_database $DB
51 * @global core_renderer $OUTPUT
52 * @param int $oldversion
5b4a78e2 53 * @return bool always true
3406acde 54 */
775f811a 55function xmldb_main_upgrade($oldversion) {
78946b9b 56 global $CFG, $USER, $DB, $OUTPUT;
4e423cbf 57
4f12838e 58 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
13a0d3d3 59
46d318bc 60 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 61
5c79b8ed
PS
62 if ($oldversion < 2011120500) {
63 // just in case somebody hacks upgrade scripts or env, we really can not continue
64 echo("You need to upgrade to 2.2.x first!\n");
65 exit(1);
ae95489d
EL
66 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks
67 upgrade_main_savepoint(true, 2011120500);
5c79b8ed
PS
68 }
69
1ea260d8 70 ////////////////////////////////////////
2e210163 71 ///upgrade supported only from 2.2.x ///
1ea260d8 72 ////////////////////////////////////////
da4aa3e4 73
43cf3165
EL
74 if ($oldversion < 2011120500.02) {
75
76 upgrade_set_timeout(60*20); // This may take a while
77 // MDL-28180. Some missing restrictions in certain backup & restore operations
78 // were causing incorrect duplicates in the course_completion_aggr_methd table.
79 // This upgrade step takes rid of them.
80 $sql = 'SELECT course, criteriatype, MIN(id) AS minid
81 FROM {course_completion_aggr_methd}
82 GROUP BY course, criteriatype
83 HAVING COUNT(*) > 1';
84 $duprs = $DB->get_recordset_sql($sql);
85 foreach ($duprs as $duprec) {
86 // We need to handle NULLs in criteriatype diferently
87 if (is_null($duprec->criteriatype)) {
88 $where = 'course = ? AND criteriatype IS NULL AND id > ?';
89 $params = array($duprec->course, $duprec->minid);
90 } else {
91 $where = 'course = ? AND criteriatype = ? AND id > ?';
92 $params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
93 }
94 $DB->delete_records_select('course_completion_aggr_methd', $where, $params);
95 }
96 $duprs->close();
97
98 // Main savepoint reached
99 upgrade_main_savepoint(true, 2011120500.02);
100 }
101
52a23063 102 if ($oldversion < 2011120500.03) {
8e54ce97
AD
103
104 // Changing precision of field value on table user_preferences to (1333)
105 $table = new xmldb_table('user_preferences');
106 $field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
107
108 // Launch change of precision for field value
109 $dbman->change_field_precision($table, $field);
110
111 // Main savepoint reached
52a23063 112 upgrade_main_savepoint(true, 2011120500.03);
8e54ce97
AD
113 }
114
91331b6b
MG
115 if ($oldversion < 2012020200.03) {
116
117 // Define index rolecontext (not unique) to be added to role_assignments
118 $table = new xmldb_table('role_assignments');
119 $index = new xmldb_index('rolecontext', XMLDB_INDEX_NOTUNIQUE, array('roleid', 'contextid'));
120
121 // Conditionally launch add index rolecontext
122 if (!$dbman->index_exists($table, $index)) {
123 $dbman->add_index($table, $index);
124 }
125
126 // Define index usercontextrole (not unique) to be added to role_assignments
127 $index = new xmldb_index('usercontextrole', XMLDB_INDEX_NOTUNIQUE, array('userid', 'contextid', 'roleid'));
128
129 // Conditionally launch add index usercontextrole
130 if (!$dbman->index_exists($table, $index)) {
131 $dbman->add_index($table, $index);
132 }
133
134 // Main savepoint reached
135 upgrade_main_savepoint(true, 2012020200.03);
136 }
137
8900213b
AD
138 if ($oldversion < 2012020200.06) {
139 // Previously we always allowed users to override their email address via the messaging system
140 // We have now added a setting to allow admins to turn this this ability on and off
141 // While this setting defaults to 0 (off) we're setting it to 1 (on) to maintain the behaviour for upgrading sites
142 set_config('messagingallowemailoverride', 1);
143
144 // Main savepoint reached
145 upgrade_main_savepoint(true, 2012020200.06);
146 }
147
bc5fbbb3 148 if ($oldversion < 2012021700.01) {
669bdcab
RT
149 // Changing precision of field uniquehash on table post to 255
150 $table = new xmldb_table('post');
151 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'content');
152
153 // Launch change of precision for field uniquehash
154 $dbman->change_field_precision($table, $field);
155
156 // Main savepoint reached
bc5fbbb3 157 upgrade_main_savepoint(true, 2012021700.01);
669bdcab
RT
158 }
159
a4cdd6d2 160 return true;
4e423cbf 161}
271e6dec 162