Commit 1a1aac1f authored by Yuliya Bozhko's avatar Yuliya Bozhko Committed by Gerrit Code Review
Browse files

Make add_key() and drop_key() consistent with other DDL functions by checking...


Make add_key() and drop_key() consistent with other DDL functions by checking if the key does or does not exist respectively (Bug #1330277)

Change-Id: Ifd1ff659dd27e312108641bb49a599ffa9d4c9ff
Signed-off-by: default avatarYuliya Bozhko <yuliya.bozhko@totaralms.com>
parent a789090c
......@@ -3134,9 +3134,7 @@ function xmldb_core_upgrade($oldversion=0) {
$table = new XMLDBTable('usr');
$field = new XMLDBField('probation');
$field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, 0);
if (!field_exists($table, $field)) {
add_field($table, $field);
}
add_field($table, $field);
}
if ($oldversion < 2014032600) {
......@@ -3210,15 +3208,11 @@ function xmldb_core_upgrade($oldversion=0) {
$field = new XMLDBField('allownonemethod');
$field->setAttributes(XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null, null, null, 1, 'delay');
if (!field_exists($table, $field)) {
add_field($table, $field);
}
add_field($table, $field);
$field = new XMLDBField('defaultmethod');
$field->setAttributes(XMLDB_TYPE_CHAR, 255, null, null, null, null, null, 'email', 'allownonemethod');
if (!field_exists($table, $field)) {
add_field($table, $field);
}
add_field($table, $field);
// Allow null method in usr_activity_preference.
// Null indicates "none", no record indicates "not yet set" so use the default.
......@@ -3261,9 +3255,7 @@ function xmldb_core_upgrade($oldversion=0) {
// First drop artefact_parent_cache table.
$table = new XMLDBTable('artefact_parent_cache');
if (table_exists($table)) {
drop_table($table, true);
}
drop_table($table, true);
// Remove cron jobs from DB.
delete_records('cron', 'callfunction', 'rebuild_artefact_parent_cache_dirty');
......@@ -3273,10 +3265,8 @@ function xmldb_core_upgrade($oldversion=0) {
$table = new XMLDBTable('artefact');
$field = new XMLDBField('path');
$field->setAttributes(XMLDB_TYPE_CHAR, '1024', null, null, null, null, null);
add_field($table, $field);
if (!field_exists($table, $field)) {
add_field($table, $field);
}
// Fill the new field with path data.
$artefacts = get_records_array('artefact', '', '', '', 'id, parent');
$artefact_relations = get_records_menu('artefact', '', '', '', 'id, parent');
......@@ -3308,9 +3298,7 @@ function xmldb_core_upgrade($oldversion=0) {
$table->addKeyInfo('resolverfk', XMLDB_KEY_FOREIGN, array('resolvedby'), 'usr', array('id'));
$table->addIndexInfo('objectix', XMLDB_INDEX_NOTUNIQUE, array('objectid', 'objecttype'));
if (!table_exists($table)) {
create_table($table);
}
create_table($table);
// Migrate data to a new format.
// Since we don't have report or name of the user, use root ID.
......@@ -3354,7 +3342,6 @@ function xmldb_core_upgrade($oldversion=0) {
change_field_enum($table, $field);
}
// Activity records, artefact access and artefact watchlist tables.
if ($oldversion < 2014060500) {
// Add artefact_access table.
$table = new XMLDBTable('artefact_access');
......@@ -3370,9 +3357,7 @@ function xmldb_core_upgrade($oldversion=0) {
$table->addKeyInfo('institutionfk', XMLDB_KEY_FOREIGN, array('institution'), 'institution', array('name'));
$table->addIndexInfo('accesstypeix', XMLDB_INDEX_NOTUNIQUE, array('accesstype'));
if (!table_exists($table)) {
create_table($table);
}
create_table($table);
}
if ($oldversion < 2014061100) {
......
......@@ -757,7 +757,8 @@ function install_from_xmldb_file($file) {
/**
* This function will create the table passed as argument with all its
* fields/keys/indexes/sequences, everything based in the XMLDB object
* fields/keys/indexes/sequences, everything based in the XMLDB object.
* Before creating the table, the function will check it doesn't exist.
*
* @uses $CFG, $db
* @param XMLDBTable table object (full specs are required)
......@@ -792,6 +793,7 @@ function create_table($table, $continue=true, $feedback=true) {
* This function will drop the table passed as argument
* and all the associated objects (keys, indexes, constaints, sequences, triggers)
* will be dropped too.
* Before dropping the table, the function will check it exists.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
......@@ -811,8 +813,8 @@ function drop_table($table, $continue=true, $feedback=true) {
/// Check table exists
if (!table_exists($table)) {
debugging('Table ' . $table->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
return true; //Table don't exist, nothing to do
debugging('Table ' . $table->getName() . ' does not exist. Delete skipped', DEBUG_DEVELOPER);
return true; // Table doesn't exist, nothing to do.
}
if(!$sqlarr = $table->getDropTableSQL($CFG->dbtype, $CFG->prefix, false)) {
......@@ -912,7 +914,8 @@ function rename_table($table, $newname, $continue=true, $feedback=true) {
}
/**
* This function will add the field to the table passed as arguments
* This function will add the field to the table passed as arguments.
* Before creating the field, the function will check it doesn't exist.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
......@@ -948,7 +951,8 @@ function add_field($table, $field, $continue=true, $feedback=true) {
}
/**
* This function will drop the field from the table passed as arguments
* This function will drop the field from the table passed as arguments.
* Before dropping the field, the function will check it exists.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
......@@ -972,7 +976,7 @@ function drop_field($table, $field, $continue=true, $feedback=true) {
/// Check the field exists
if (!field_exists($table, $field)) {
debugging('Field ' . $field->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
debugging('Field ' . $field->getName() . ' does not exist. Delete skipped', DEBUG_DEVELOPER);
return true;
}
......@@ -1172,7 +1176,8 @@ function rename_field($table, $field, $newname, $continue=true, $feedback=true)
}
/**
* This function will create the key in the table passed as arguments
* This function will create the key in the table passed as arguments.
* Before creating the key, the function will check it doesn't exist.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
......@@ -1193,9 +1198,11 @@ function add_key($table, $key, $continue=true, $feedback=true) {
if (strtolower(get_class($key)) != 'xmldbkey') {
return false;
}
if ($key->getType() == XMLDB_KEY_PRIMARY) { // Prevent PRIMARY to be added (only in create table, being serious :-P)
//debugging('Primary Keys can be added at table create time only', DEBUG_DEVELOPER);
//return true;
// Check key doesn't exist.
if (db_key_exists($table, $key)) {
debugging('Key ' . $key->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
return true; // Key exists, nothing to do.
}
if(!$sqlarr = $table->getAddKeySQL($CFG->dbtype, $CFG->prefix, $key, false)) {
......@@ -1206,7 +1213,8 @@ function add_key($table, $key, $continue=true, $feedback=true) {
}
/**
* This function will drop the key in the table passed as arguments
* This function will drop the key in the table passed as arguments.
* Before dropping the key, the function will check it exists.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
......@@ -1227,9 +1235,11 @@ function drop_key($table, $key, $continue=true, $feedback=true) {
if (strtolower(get_class($key)) != 'xmldbkey') {
return false;
}
if ($key->getType() == XMLDB_KEY_PRIMARY) { // Prevent PRIMARY to be dropped (only in drop table, being serious :-P)
// debugging('Primary Keys can be deleted at table drop time only', DEBUG_DEVELOPER);
// return true;
// Check key exists.
if (!db_key_exists($table, $key)) {
debugging('Key ' . $key->getName() . ' does not exist. Delete skipped', DEBUG_DEVELOPER);
return true; // Key doesn't exist, nothing to do.
}
if(!$sqlarr = $table->getDropKeySQL($CFG->dbtype, $CFG->prefix, $key, false)) {
......@@ -1282,7 +1292,7 @@ function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
/**
* This function will create the index in the table passed as arguments
* Before creating the index, the function will check it doesn't exists
* Before creating the index, the function will check it doesn't exist.
*
* @uses $CFG, $db
* @param XMLDBTable table object (just the name is mandatory)
......@@ -1341,9 +1351,9 @@ function drop_index($table, $index, $continue=true, $feedback=true) {
return false;
}
/// Check index exists
// Check index exists.
if (!index_exists($table, $index)) {
debugging('Index ' . $index->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
debugging('Index ' . $index->getName() . ' does not exist. Delete skipped', DEBUG_DEVELOPER);
return true; //Index doesn't exist, nothing to do
}
......
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