Permalink
Browse files

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).
  • Loading branch information...
danpoltawski committed Feb 5, 2014
1 parent b7f4097 commit 824fef689283b33cf00420d0700ff548d812b3a4
Showing with 8 additions and 3 deletions.
  1. +6 −1 lib/tests/upgradelib_test.php
  2. +2 −2 lib/upgradelib.php
@@ -178,6 +178,9 @@ public function test_upgrade_fix_missing_root_folders() {
$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 @@ public function test_upgrade_fix_missing_root_folders() {
$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);
}
}
View
@@ -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));
}

0 comments on commit 824fef6

Please sign in to comment.