Commit 941de296 authored by Marina Glancy's avatar Marina Glancy
Browse files

MDL-45538 files: restore missing records in draft areas

parent 32a69a7d
...@@ -4047,5 +4047,13 @@ function xmldb_main_upgrade($oldversion) { ...@@ -4047,5 +4047,13 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014102000.00); upgrade_main_savepoint(true, 2014102000.00);
} }
if ($oldversion < 2014102800.02) {
// Run script restoring missing folder records for draft file areas.
upgrade_fix_missing_root_folders_draft();
// Main savepoint reached.
upgrade_main_savepoint(true, 2014102800.02);
}
return true; return true;
} }
...@@ -201,6 +201,50 @@ class core_upgradelib_testcase extends advanced_testcase { ...@@ -201,6 +201,50 @@ class core_upgradelib_testcase extends advanced_testcase {
$this->assertSame($oldrecord->pathnamehash, $newrecord->pathnamehash); $this->assertSame($oldrecord->pathnamehash, $newrecord->pathnamehash);
} }
public function test_upgrade_fix_missing_root_folders_draft() {
global $DB, $SITE;
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$usercontext = context_user::instance($user->id);
$this->setUser($user);
$resource1 = $this->getDataGenerator()->get_plugin_generator('mod_resource')
->create_instance(array('course' => $SITE->id));
$context = context_module::instance($resource1->cmid);
$draftitemid = 0;
file_prepare_draft_area($draftitemid, $context->id, 'mod_resource', 'content', 0);
$queryparams = array(
'component' => 'user',
'contextid' => $usercontext->id,
'filearea' => 'draft',
'itemid' => $draftitemid,
);
// Make sure there are two records in files for the draft file area and one of them has filename '.'.
$records = $DB->get_records_menu('files', $queryparams, '', 'id, filename');
$this->assertEquals(2, count($records));
$this->assertTrue(in_array('.', $records));
$originalhash = $DB->get_field('files', 'pathnamehash', $queryparams + array('filename' => '.'));
// Delete record with filename '.' and make sure it does not exist any more.
$DB->delete_records('files', $queryparams + array('filename' => '.'));
$records = $DB->get_records_menu('files', $queryparams, '', 'id, filename');
$this->assertEquals(1, count($records));
$this->assertFalse(in_array('.', $records));
// Run upgrade script and make sure the record is restored.
upgrade_fix_missing_root_folders_draft();
$records = $DB->get_records_menu('files', $queryparams, '', 'id, filename');
$this->assertEquals(2, count($records));
$this->assertTrue(in_array('.', $records));
$newhash = $DB->get_field('files', 'pathnamehash', $queryparams + array('filename' => '.'));
$this->assertEquals($originalhash, $newhash);
}
/** /**
* Tests the upgrade of an individual course-module or section from the * Tests the upgrade of an individual course-module or section from the
* old to new availability system. (This test does not use the database * old to new availability system. (This test does not use the database
......
...@@ -2173,3 +2173,33 @@ function upgrade_fix_missing_root_folders() { ...@@ -2173,3 +2173,33 @@ function upgrade_fix_missing_root_folders() {
$rs->close(); $rs->close();
$transaction->allow_commit(); $transaction->allow_commit();
} }
/**
* Detect draft file areas with missing root directory records and add them.
*/
function upgrade_fix_missing_root_folders_draft() {
global $DB;
$transaction = $DB->start_delegated_transaction();
$sql = "SELECT contextid, itemid, MAX(timecreated) AS timecreated, MAX(timemodified) AS timemodified
FROM {files}
WHERE (component = 'user' AND filearea = 'draft')
GROUP BY contextid, itemid
HAVING MAX(CASE WHEN filename = '.' AND filepath = '/' THEN 1 ELSE 0 END) = 0";
$rs = $DB->get_recordset_sql($sql);
$defaults = array('component' => 'user',
'filearea' => 'draft',
'filepath' => '/',
'filename' => '.',
'userid' => 0, // Don't rely on any particular user for these system records.
'filesize' => 0,
'contenthash' => sha1(''));
foreach ($rs as $r) {
$r->pathnamehash = sha1("/$r->contextid/user/draft/$r->itemid/.");
$DB->insert_record('files', (array)$r + $defaults);
}
$rs->close();
$transaction->allow_commit();
}
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$version = 2014102800.01; // YYYYMMDD = weekly release date of this DEV branch. $version = 2014102800.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches. // RR = release increments - 00 in DEV branches.
// .XX = incremental changes. // .XX = incremental changes.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment