Commit a5722727 authored by Marina Glancy's avatar Marina Glancy
Browse files

MDL-50770 blocks: remove orphaned block positions

parent 34c9d28e
......@@ -2525,5 +2525,16 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2017020200.02);
}
if ($oldversion < 2017020901.00) {
// Delete "orphaned" block positions. Note, the query does not use indexes (because there are none),
// if it runs too long during upgrade you can comment this line - it will leave orphaned records
// in the database but they won't bother you.
upgrade_block_positions();
// Main savepoint reached.
upgrade_main_savepoint(true, 2017020901.00);
}
return true;
}
......@@ -527,3 +527,18 @@ function upgrade_standardise_score($rawgrade, $sourcemin, $sourcemax, $targetmin
$standardisedvalue = $factor * $diff + $targetmin;
return $standardisedvalue;
}
/**
* Delete orphaned records in block_positions
*/
function upgrade_block_positions() {
global $DB;
$id = 'id';
if ($DB->get_dbfamily() !== 'mysql') {
// Field block_positions.subpage has type 'char', it can not be compared to int in db engines except for mysql.
$id = $DB->sql_concat('?', 'id');
}
$sql = "DELETE FROM {block_positions}
WHERE pagetype IN ('my-index', 'user-profile') AND subpage NOT IN (SELECT $id FROM {my_pages})";
$DB->execute($sql, ['']);
}
......@@ -882,4 +882,47 @@ class core_upgradelib_testcase extends advanced_testcase {
$this->assertNull(check_libcurl_version($result));
}
}
/**
* Create two pages with blocks, delete one page and make sure upgrade script deletes orphaned blocks
*/
public function test_delete_block_positions() {
global $DB, $CFG;
require_once($CFG->dirroot . '/my/lib.php');
$this->resetAfterTest();
// Make sure each block on system dashboard page has a position.
$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => MY_PAGE_PRIVATE));
$systemcontext = context_system::instance();
$blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
'pagetypepattern' => 'my-index', 'subpagepattern' => $systempage->id));
$this->assertNotEmpty($blockinstances);
foreach ($blockinstances as $bi) {
$DB->insert_record('block_positions', ['subpage' => $systempage->id, 'pagetype' => 'my-index', 'contextid' => $systemcontext->id,
'blockinstanceid' => $bi->id, 'visible' => 1, 'weight' => $bi->defaultweight]);
}
// Create two users and make two copies of the system dashboard.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$page1 = my_copy_page($user1->id, MY_PAGE_PRIVATE, 'my-index');
$page2 = my_copy_page($user2->id, MY_PAGE_PRIVATE, 'my-index');
$context1 = context_user::instance($user1->id);
$context2 = context_user::instance($user2->id);
// Delete second page without deleting block positions.
$DB->delete_records('my_pages', ['id' => $page2->id]);
// Blocks are still here.
$this->assertEquals(count($blockinstances), $DB->count_records('block_positions', ['subpage' => $page1->id, 'pagetype' => 'my-index', 'contextid' => $context1->id]));
$this->assertEquals(count($blockinstances), $DB->count_records('block_positions', ['subpage' => $page2->id, 'pagetype' => 'my-index', 'contextid' => $context2->id]));
// Run upgrade script that should delete orphaned block_positions.
upgrade_block_positions();
// First user still has all his block_positions, second user does not.
$this->assertEquals(count($blockinstances), $DB->count_records('block_positions', ['subpage' => $page1->id, 'pagetype' => 'my-index', 'contextid' => $context1->id]));
$this->assertEquals(0, $DB->count_records('block_positions', ['subpage' => $page2->id, 'pagetype' => 'my-index']));
}
}
......@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2017020900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2017020901.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .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