Commit cd9247b4 authored by Robert Lyon's avatar Robert Lyon

Bug 845948: Add primary key to view_access table for better indexing

Part of the change is to remove duplicate view access rows as there is
no need to have duplicates of the exact same access for a view.

Adding the id to view_access will allow us to have different view
access for a view in multiple collections in the future

behatnotneeded - should be covered by existing tests

Change-Id: I8d3d6f0a011d0ed01a5ff2931e8e16d3000f4f16
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
Signed-off-by: Aaron Wells's avatarAaron Wells <aaronw@catalyst.net.nz>
parent f802140f
......@@ -895,6 +895,7 @@
</TABLE>
<TABLE NAME="view_access">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="accesstype" TYPE="char" LENGTH="16" NOTNULL="false" ENUM="true" ENUMVALUES="'public', 'loggedin', 'friends'" />
<FIELD NAME="group" TYPE="int" LENGTH="10" NOTNULL="false" />
......@@ -910,6 +911,7 @@
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
<KEY NAME="groupfk" TYPE="foreign" FIELDS="group" REFTABLE="group" REFFIELDS="id" />
<KEY NAME="usrfk" TYPE="foreign" FIELDS="usr" REFTABLE="usr" REFFIELDS="id" />
......
......@@ -4501,7 +4501,7 @@ function xmldb_core_upgrade($oldversion=0) {
}
if ($oldversion < 2016062900) {
log_debug('Assign an istitution for each existing group that doesn\'t have one.');
log_debug('Assign an institution for each existing group that doesn\'t have one.');
$groups = execute_sql("UPDATE {group} SET institution = 'mahara'
WHERE (institution IS NULL OR institution = '') AND deleted = 0", array());
}
......@@ -4575,5 +4575,69 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2016072200) {
log_debug('Add primary key to view_access table');
// See if we need to add the id column
$table = new XMLDBTable('view_access');
$field = new XMLDBField('id');
if (!field_exists($table, $field)) {
log_debug('Making a temp copy and adding id column');
execute_sql('CREATE TEMPORARY TABLE {temp_view_access} AS SELECT DISTINCT * FROM {view_access}', array());
if (is_mysql()) {
// We've disabled the db_start() method for our MySQL driver, but since we're truncating view_access,
// we really should start a transaction manually at least.
execute_sql('START TRANSACTION');
}
execute_sql('TRUNCATE {view_access}', array());
if (is_mysql()) {
// MySQL requires the auto-increment column to be a primary key right away.
execute_sql('ALTER TABLE {view_access} ADD id BIGINT(10) NOT NULL auto_increment PRIMARY KEY FIRST');
}
else {
$field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
add_field($table, $field);
}
log_debug('Adding back in the view_access information');
// We will do in chuncks for large sites.
$count = 0;
$x = 0;
$limit = 1000;
$total = count_records('temp_view_access');
for ($i = 0; $i <= $total; $i += $limit) {
if (is_postgres()) {
$limitsql = ' OFFSET ' . $i . ' LIMIT ' . $limit;
}
else {
$limitsql = ' LIMIT ' . $i . ',' . $limit;
}
execute_sql('INSERT INTO {view_access} (view, accesstype, startdate, stopdate, allowcomments, approvecomments, "group", role, usr, token, visible, ctime, institution) SELECT view, accesstype, startdate, stopdate, allowcomments, approvecomments, "group", role, usr, token, visible, ctime, institution FROM {temp_view_access}' . $limitsql, array());
$count += $limit;
if (($count % ($limit *10)) == 0 || $count >= $total) {
if ($count > $total) {
$count = $total;
}
log_debug("$count/$total");
set_time_limit(30);
}
set_time_limit(30);
}
if (is_mysql()) {
execute_sql('COMMIT');
}
execute_sql('DROP TABLE {temp_view_access}', array());
if (!is_mysql()) {
log_debug('Adding primary key index to view_access.id column');
$key = new XMLDBKey('primary');
$key->setAttributes(XMLDB_KEY_PRIMARY, array('id'));
add_key($table, $key);
}
}
}
return $status;
}
......@@ -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 = 2016070801;
$config->version = 2016072200;
$config->series = '16.10';
$config->release = '16.10dev';
$config->minupgradefrom = 2012080604;
......
......@@ -1455,6 +1455,7 @@ class View {
foreach ($firstviewaccess as &$a) {
$a->view = $id;
$a->ctime = db_format_timestamp(time());
unset($a->id);
insert_record('view_access', $a);
}
}
......
Markdown is supported
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