From 941de29601e5193599098d373fb9a5fde11ee6f9 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 16 Oct 2014 17:10:51 +0800 Subject: [PATCH] MDL-45538 files: restore missing records in draft areas --- lib/db/upgrade.php | 8 +++++++ lib/tests/upgradelib_test.php | 44 +++++++++++++++++++++++++++++++++++ lib/upgradelib.php | 30 ++++++++++++++++++++++++ version.php | 2 +- 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 62bcd6e3f4e..14f0d06a73d 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -4047,5 +4047,13 @@ function xmldb_main_upgrade($oldversion) { 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; } diff --git a/lib/tests/upgradelib_test.php b/lib/tests/upgradelib_test.php index 10df83586e1..b4019108433 100644 --- a/lib/tests/upgradelib_test.php +++ b/lib/tests/upgradelib_test.php @@ -201,6 +201,50 @@ class core_upgradelib_testcase extends advanced_testcase { $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 * old to new availability system. (This test does not use the database diff --git a/lib/upgradelib.php b/lib/upgradelib.php index 9d3f707f555..492a62865fd 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -2173,3 +2173,33 @@ function upgrade_fix_missing_root_folders() { $rs->close(); $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(); +} diff --git a/version.php b/version.php index 66a0b4cb398..0863cecbf2d 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ 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. // .XX = incremental changes.