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:40:51 +08:00
parent ec62826d8d
commit e7c932da99
2 changed files with 8 additions and 3 deletions
+6 -1
View File
@@ -178,6 +178,9 @@ class upgradelib_test extends advanced_testcase {
$folderrecord = $selectargs;
$folderrecord['filepath'] = '/';
$folderrecord['filename'] = '.';
// Get previous folder record.
$oldrecord = $DB->get_record('files', $folderrecord);
$DB->delete_records('files', $folderrecord);
// Verify the folder record has been removed.
@@ -193,6 +196,8 @@ class upgradelib_test extends advanced_testcase {
$newareafilecount = $DB->count_records('files', $selectargs);
$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);
}
}
+2 -2
View File
@@ -2106,13 +2106,13 @@ function upgrade_fix_missing_root_folders() {
$rs = $DB->get_recordset_sql($sql);
$defaults = array('filepath' => '/',
'filename' => '.',
'userid' => $USER->id,
'userid' => 0, // Don't rely on any particular user for these system records.
'filesize' => 0,
'timecreated' => time(),
'timemodified' => time(),
'contenthash' => sha1(''));
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 +
array('pathnamehash' => $pathhash));
}