Skip to content

Commit

Permalink
fixup! MDL-83153 core: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Sep 18, 2024
1 parent d1bcfa4 commit 88cf422
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
28 changes: 23 additions & 5 deletions lib/db/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1877,12 +1877,30 @@ function upgrade_change_binary_column_to_int(
$field = new xmldb_field($fieldname, XMLDB_TYPE_INTEGER, '1', null, $notnull, null, '0', $previous);
$dbman->add_field($table, $field);

// Copy the values from the old field to the new field.
$sql = 'UPDATE {' . $tablename . '}
SET ' . $fieldname . ' = 1
WHERE ' . $DB->sql_compare_text($tmpfieldname) . ' = :truefieldvalue';
$DB->execute($sql, ['truefieldvalue' => 1]);
// Copy the 'true' values from the old field to the new field.
if ($DB->get_dbfamily() === 'oracle') {
// It's tricky to use the SQL UPDATE statement while having the binary column in the WHERE clause in Oracle DBs.
// Let's go updating the records one by one. It's nasty, but it's only done for instances with Oracle DBs.
// The normal SQL UPDATE statement will be used for other DBs.
$columns = implode(', ', ['id', $tmpfieldname, $fieldname]);
$records = $DB->get_recordset($tablename, null, '', $columns);
if ($records->valid()) {
foreach ($records as $record) {
if (!$record->$tmpfieldname) {
continue;
}
$DB->set_field($tablename, $fieldname, 1);
}
}
$records->close();
} else {
$sql = 'UPDATE {' . $tablename . '}
SET ' . $fieldname . ' = 1
WHERE ' . $tmpfieldname . ' = ?';
$DB->execute($sql, [true]);
}

print_object($DB->get_records($tablename));
// Drop the old field.
$oldfield = new xmldb_field($tmpfieldname);
$dbman->drop_field($table, $oldfield);
Expand Down
7 changes: 4 additions & 3 deletions lib/tests/upgradelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1724,12 +1724,13 @@ public function test_upgrade_change_binary_column_to_int(
$field = $columns[$fieldname];
$this->assertFalse($field->binary);

// Verify that the renamed old field has now been removed.
$this->assertArrayNotHasKey("tmp$fieldname", $columns);

// Confirm that the values for the converted column ar ethe same.
$records = $DB->get_fieldset($tmptablename, 'id', [$fieldname => 1]);
print_object($records);
$this->assertEqualsCanonicalizing($ones, $records);

// Verify that the renamed old field has now been removed.
$this->assertArrayNotHasKey("tmp$fieldname", $columns);
}

// Cleanup.
Expand Down

0 comments on commit 88cf422

Please sign in to comment.