Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
integration
prechecker
Commits
941de296
Commit
941de296
authored
Oct 16, 2014
by
Marina Glancy
Browse files
MDL-45538 files: restore missing records in draft areas
parent
32a69a7d
Changes
4
Hide whitespace changes
Inline
Side-by-side
lib/db/upgrade.php
View file @
941de296
...
...
@@ -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
;
}
lib/tests/upgradelib_test.php
View file @
941de296
...
...
@@ -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
...
...
lib/upgradelib.php
View file @
941de296
...
...
@@ -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
();
}
version.php
View file @
941de296
...
...
@@ -29,7 +29,7 @@
defined
(
'MOODLE_INTERNAL'
)
||
die
();
$version
=
2014102800.0
1
;
// YYYYMMDD = weekly release date of this DEV branch.
$version
=
2014102800.0
2
;
// YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment