Skip to content

Commit

Permalink
MDL-50770 blocks: remove orphaned block positions
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Feb 13, 2017
1 parent 34c9d28 commit a572272
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions lib/db/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, ['']);
}
43 changes: 43 additions & 0 deletions lib/tests/upgradelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,47 @@ public function test_check_libcurl_version() {
$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']));
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit a572272

Please sign in to comment.