Commit 4251b983 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1528351: Fixing block order drift in database



Due to a mistake in how blocks were ordered in the system

behatnotneeded - existing tests should be ok

Change-Id: Iac857c85d60e5948b6f95ddccee7a2e8cf43b1b4
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent ae5f773b
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -4075,5 +4075,42 @@ function xmldb_core_upgrade($oldversion=0) {
        }
    }

    if ($oldversion < 2015030423) {
        log_debug('Sorting out block_instance sort order drift');
        // There was an issue with the sorting of blocks (Bug #1523719) that existed since
        // Sept 2007, commit 02fb5d96 where the max order number does not equal the number
        // of blocks in the cell
        set_time_limit(120);
        if ($results = get_records_sql_array('SELECT b.id, b.view, b.row, b.column, b.order, maxorder, countorder
                                              FROM {block_instance} b
                                              JOIN (SELECT view AS sview, "row" AS srow, "column" AS scol, COUNT("order") AS countorder, MAX("order") AS maxorder
                                                  FROM {block_instance} GROUP BY view, "row", "column") AS myview
                                                ON myview.sview = b.VIEW AND myview.srow = b.row AND myview.scol = b.column
                                              WHERE maxorder != countorder
                                              ORDER BY b.view, b.row, b.column, b.order', array())) {
            // Structure the info into a more usable format
            $updates = array();
            foreach ($results as $r) {
                $updates[$r->view][$r->row][$r->column][] = array('order' => $r->order, 'id' => $r->id);
            }
            // Now deal with the results
            foreach ($updates as $view => $grid) {
                foreach ($grid as $row => $columns) {
                    foreach ($columns as $column => $blocks) {
                        foreach ($blocks as $key => $block) {
                            // First move them out of the way to avoid uniqueness clash
                            execute_sql('UPDATE {block_instance} SET "order" = ? WHERE id = ?', array(($block['order'] * -1), $block['id']));
                        }
                        foreach ($blocks as $key => $block) {
                            // Then update them with true order
                            execute_sql('UPDATE {block_instance} SET "order" = ? WHERE id = ?', array(($key + 1), $block['id']));
                        }
                    }
                }
                set_time_limit(30);
            }
        }
    }

    return $status;
}
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one.  On master, use the date.

$config->version = 2015030422;
$config->version = 2015030423;
$config->series = '15.04';
$config->release = '15.04.6testing';
$config->minupgradefrom = 2009022600;