Commit 0c9774b9 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add foreign key constraint on profileicon field of usr table



Bug #805742

Also ensures the profileicon field is cleared when the artefact
it points to gets deleted.

Change-Id: Idfda6111b3d653e26225bff90500706ec5dd66a7
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 174400aa
......@@ -627,6 +627,7 @@ abstract class ArtefactType {
delete_records_select('artefact_tag', "artefact IN $idstr");
delete_records_select('artefact_access_role', "artefact IN $idstr");
delete_records_select('artefact_access_usr', "artefact IN $idstr");
execute_sql("UPDATE {usr} SET profileicon = NULL WHERE profileicon IN $idstr");
delete_records_select('artefact', "id IN $idstr");
......
......@@ -101,7 +101,8 @@
<FIELD NAME="email" TYPE="char" LENGTH="255" NOTNULL="false"/>
<!-- This field should be foreign keyed to the artefact table, however the
artefact table already has a key to this table, and XMLDB isn't smart enough
to create the tables first, then the keys. -->
to create the tables first, then the keys. The foreign key will be created
during the postinst phase. -->
<FIELD NAME="profileicon" TYPE="int" LENGTH="10" NOTNULL="false"/>
<FIELD NAME="suspendedctime" TYPE="datetime" NOTNULL="false"/>
<FIELD NAME="suspendedreason" TYPE="text" NOTNULL="false"/>
......
......@@ -2472,5 +2472,16 @@ function xmldb_core_upgrade($oldversion=0) {
set_config('dropdownmenu', 0);
}
if ($oldversion < 2011070500) {
// Add profileicon foreign key to artefact table, first clearing any bad profileicon
// values out of usr.
execute_sql("UPDATE {usr} SET profileicon = NULL WHERE NOT profileicon IN (SELECT id FROM {artefact})");
$table = new XMLDBTable('usr');
$key = new XMLDBKey('profileiconfk');
$key->setAttributes(XMLDB_KEY_FOREIGN, array('profileicon'), 'artefact', array('id'));
add_key($table, $key);
}
return $status;
}
......@@ -580,6 +580,15 @@ function core_postinst() {
set_config('smtppass', '');
set_config('smtpsecure', '');
// XMLDB adds a table's keys immediately after creating the table. Some
// foreign keys therefore cannot be created during the XMLDB installation,
// because they refer to tables created later in the installation. These
// missing keys can be created now that all the core tables exist.
$table = new XMLDBTable('usr');
$key = new XMLDBKey('profileiconfk');
$key->setAttributes(XMLDB_KEY_FOREIGN, array('profileicon'), 'artefact', array('id'));
add_key($table, $key);
// PostgreSQL supports indexes over functions of columns, MySQL does not.
// We make use if this if we can
if (is_postgres()) {
......
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2011062700;
$config->version = 2011070500;
$config->release = '1.5.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
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