diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 15aad8923a2..4209b7c7401 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1047,6 +1047,8 @@ function xmldb_main_upgrade($oldversion) { } if ($oldversion < 2009010602) { + // Issue with field in the user table being null in postgres. + $DB->execute("UPDATE {user} SET lastip = '' WHERE lastip IS NULL"); /// Changing precision of field lastip on table user to (45) $table = new xmldb_table('user'); @@ -4736,6 +4738,10 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); if ($dbman->index_exists($table, $index)) { $dbman->drop_index($table, $index); } + + // Issue with field in the user table being null in postgres. + $DB->execute("UPDATE {user} SET city = '' WHERE city IS NULL"); + /// Changing precision of field city on table user to (120) $field = new xmldb_field('city', XMLDB_TYPE_CHAR, '120', null, XMLDB_NOTNULL, null, null, 'address'); @@ -6821,5 +6827,87 @@ FROM upgrade_main_savepoint(true,2011070106.03); } + if ($oldversion < 2011070108.08) { + // Issue with Moodle version < 1.7 using postgres having null values in the user table. + if ($DB->get_dbfamily() === 'postgres') { + // 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'); + // Create array to store the indexes in the user table. + $arrindexes = array(); + $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['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 the indexes that were removed. + foreach ($arrindexes as $index) { + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + } + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2011070108.08); + } + return true; }