Permalink
Browse files

MDL-25948 upgrade: Check that field does not match defaults before ed…

…iting.
  • Loading branch information...
markn86 committed Nov 9, 2012
1 parent 4326153 commit 7ea26a73c44195cf16da2640009059bcb56ed316
Showing with 58 additions and 35 deletions.
  1. +58 −35 lib/db/upgrade.php
View
@@ -6828,53 +6828,76 @@ function xmldb_main_upgrade($oldversion) {
}
if ($oldversion < 2011070108.08) {
- // Issue with Moodle version < 1.7 postgres having null values in the user table.
+ // Issue with Moodle version < 1.7 using postgres having null values in the user table.
if ($DB->get_dbfamily() === 'postgres') {
- // Update picture field to 0.
- $DB->execute("UPDATE {user} SET picture = '0' WHERE picture IS NULL");
- // Update the other fields to empty string.
- $arrfields = array('idnumber', 'icq', 'skype', 'yahoo', 'aim', 'msn',
- 'phone1', 'phone2', 'institution', 'department',
- 'address', 'city', 'country', 'lastip', 'secret',
- 'url');
- foreach ($arrfields as $field) {
- $DB->execute("UPDATE {user} SET $field = '' WHERE $field IS NULL");
+ // Array to store columns we may need to change.
+ $arrcolumns = array('idnumber', 'icq', 'skype', 'yahoo', 'aim', 'msn',
+ 'phone1', 'phone2', 'institution', 'department',
+ 'address', 'city', 'country', 'lastip', 'secret',
+ 'picture', 'url');
+ // OK, now we want to remove the columns that already have the default values before editing.
+ $columns = $DB->get_columns('user');
+ foreach ($columns as $c) {
+ if (in_array($c->name, $arrcolumns)) {
+ if ($c->has_default && $c->not_null) {
+ $arrcolumns = array_diff($arrcolumns, array($c->name));
+ } else {
+ if ($c->name == 'picture') {
+ $DB->execute("UPDATE {user} SET picture = '0' WHERE picture IS NULL");
+ } else {
+ $DB->execute("UPDATE {user} SET $c->name = '' WHERE $c->name IS NULL");
+ }
+ }
+ }
}
+ // Create user table object.
$table = new xmldb_table('user');
- // Need to remove indexes in order to edit some fields.
+ // Create array to store the indexes in the user table.
$arrindexes = array();
- $arrindexes[] = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
- $arrindexes[] = new xmldb_index('city', XMLDB_INDEX_NOTUNIQUE, array('city'));
- $arrindexes[] = new xmldb_index('country', XMLDB_INDEX_NOTUNIQUE, array('country'));
+ $arrindexes['idnumber'] = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
+ $arrindexes['city'] = new xmldb_index('city', XMLDB_INDEX_NOTUNIQUE, array('city'));
+ $arrindexes['country'] = new xmldb_index('country', XMLDB_INDEX_NOTUNIQUE, array('country'));
+ // Loop through the indexes.
+ foreach ($arrindexes as $key => $index) {
+ // Check if it is not in the columns array and remove as it does not need editing.
+ if (!in_array($key, $arrcolumns)) {
+ unset($arrindexes[$key]);
+ }
+ }
+ // Remove the indexes that need to be removed.
foreach ($arrindexes as $index) {
if ($dbman->index_exists($table, $index)) {
$dbman->drop_index($table, $index);
}
}
+ // Create list of fields that potentially need changed.
$arrfields = array();
- $arrfields[] = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, '', 'password');
- $arrfields[] = new xmldb_field('icq', XMLDB_TYPE_CHAR, '15', null, XMLDB_NOTNULL, null, '', 'emailstop');
- $arrfields[] = new xmldb_field('skype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, '', 'icq');
- $arrfields[] = new xmldb_field('yahoo', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, '', 'skype');
- $arrfields[] = new xmldb_field('aim', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, '', 'yahoo');
- $arrfields[] = new xmldb_field('msn', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, '', 'aim');
- $arrfields[] = new xmldb_field('phone1', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, '', 'msn');
- $arrfields[] = new xmldb_field('phone2', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, '', 'phone1');
- $arrfields[] = new xmldb_field('institution', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, '', 'phone2');
- $arrfields[] = new xmldb_field('department', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, '', 'institution');
- $arrfields[] = new xmldb_field('address', XMLDB_TYPE_CHAR, '70', null, XMLDB_NOTNULL, null, '', 'department');
- $arrfields[] = new xmldb_field('city', XMLDB_TYPE_CHAR, '120', null, XMLDB_NOTNULL, null, '', 'address');
- $arrfields[] = new xmldb_field('country', XMLDB_TYPE_CHAR, '2', null, XMLDB_NOTNULL, null, '', 'city');
- $arrfields[] = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, '', 'country');
- $arrfields[] = new xmldb_field('secret', XMLDB_TYPE_CHAR, '15', null, XMLDB_NOTNULL, null, '', 'lastip');
- $arrfields[] = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'secret');
- $arrfields[] = new xmldb_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, '', 'picture');
- foreach ($arrfields as $field) {
- if ($dbman->field_exists($table, $field)) {
- $dbman->change_field_notnull($table, $field);
+ $arrfields['idnumber'] = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
+ $arrfields['icq'] = new xmldb_field('icq', XMLDB_TYPE_CHAR, '15', null, XMLDB_NOTNULL, null, null, 'emailstop');
+ $arrfields['skype'] = new xmldb_field('skype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'icq');
+ $arrfields['yahoo'] = new xmldb_field('yahoo', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'skype');
+ $arrfields['aim'] = new xmldb_field('aim', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'yahoo');
+ $arrfields['msn'] = new xmldb_field('msn', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'aim');
+ $arrfields['phone1'] = new xmldb_field('phone1', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'msn');
+ $arrfields['phone2'] = new xmldb_field('phone2', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'phone1');
+ $arrfields['institution'] = new xmldb_field('institution', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'phone2');
+ $arrfields['department'] = new xmldb_field('department', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'institution');
+ $arrfields['address'] = new xmldb_field('address', XMLDB_TYPE_CHAR, '70', null, XMLDB_NOTNULL, null, null, 'department');
+ $arrfields['city'] = new xmldb_field('city', XMLDB_TYPE_CHAR, '120', null, XMLDB_NOTNULL, null, null, 'address');
+ $arrfields['country'] = new xmldb_field('country', XMLDB_TYPE_CHAR, '2', null, XMLDB_NOTNULL, null, null, 'city');
+ $arrfields['lastip'] = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'country');
+ $arrfields['secret'] = new xmldb_field('secret', XMLDB_TYPE_CHAR, '15', null, XMLDB_NOTNULL, null, null, 'lastip');
+ $arrfields['picture'] = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'secret');
+ $arrfields['url'] = new xmldb_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'picture');
+ foreach ($arrfields as $key => $field) {
+ // If it is in the columns array it needs editing.
+ if (in_array($key, $arrcolumns)) {
+ if ($dbman->field_exists($table, $field)) {
+ $dbman->change_field_notnull($table, $field);
+ }
}
}
- // Re-add indexes.
+ // Re-add the indexes that were removed.
foreach ($arrindexes as $index) {
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);

1 comment on commit 7ea26a7

@erd0s

This comment has been minimized.

Show comment Hide comment
@erd0s

erd0s Nov 11, 2012

Do you fuck as well as you code?

erd0s commented on 7ea26a7 Nov 11, 2012

Do you fuck as well as you code?

Please sign in to comment.