MDL-42882 upgrade: Improvements to upgrade code

Suggested by Petr:
* Don't rely on single user for upgrade
* Make the pathnamehash generation a bit clearer (and add
  a test to verify its correctly formed).
This commit is contained in:
Dan Poltawski 2014-02-05 14:38:39 +08:00
parent 40c761e01a
commit 6c704b1534
2 changed files with 8 additions and 3 deletions

View File

@ -178,6 +178,9 @@ class core_upgradelib_testcase extends advanced_testcase {
$folderrecord = $selectargs; $folderrecord = $selectargs;
$folderrecord['filepath'] = '/'; $folderrecord['filepath'] = '/';
$folderrecord['filename'] = '.'; $folderrecord['filename'] = '.';
// Get previous folder record.
$oldrecord = $DB->get_record('files', $folderrecord);
$DB->delete_records('files', $folderrecord); $DB->delete_records('files', $folderrecord);
// Verify the folder record has been removed. // Verify the folder record has been removed.
@ -193,6 +196,8 @@ class core_upgradelib_testcase extends advanced_testcase {
$newareafilecount = $DB->count_records('files', $selectargs); $newareafilecount = $DB->count_records('files', $selectargs);
$this->assertSame($newareafilecount, $areafilecount); $this->assertSame($newareafilecount, $areafilecount);
$this->assertTrue($DB->record_exists('files', $folderrecord)); $newrecord = $DB->get_record('files', $folderrecord, '*', MUST_EXIST);
// Verify the hash is correctly created.
$this->assertSame($oldrecord->pathnamehash, $newrecord->pathnamehash);
} }
} }

View File

@ -2107,13 +2107,13 @@ function upgrade_fix_missing_root_folders() {
$rs = $DB->get_recordset_sql($sql); $rs = $DB->get_recordset_sql($sql);
$defaults = array('filepath' => '/', $defaults = array('filepath' => '/',
'filename' => '.', 'filename' => '.',
'userid' => $USER->id, 'userid' => 0, // Don't rely on any particular user for these system records.
'filesize' => 0, 'filesize' => 0,
'timecreated' => time(), 'timecreated' => time(),
'timemodified' => time(), 'timemodified' => time(),
'contenthash' => sha1('')); 'contenthash' => sha1(''));
foreach ($rs as $r) { foreach ($rs as $r) {
$pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid".'/.'); $pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid/.");
$DB->insert_record('files', (array)$r + $defaults + $DB->insert_record('files', (array)$r + $defaults +
array('pathnamehash' => $pathhash)); array('pathnamehash' => $pathhash));
} }