$tests['rename key (experimental. DO NOT USE IT)'] = $test;
}
+ /// 39th test. Renaming one field
+ if ($test->status && 1==2) {
+ /// Get SQL code and execute it
+ $test = new stdClass;
+ $field = new XMLDBField('type');
+ $field->setAttributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM, array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
+
+ $test->sql = $table->getRenameFieldSQL($CFG->dbtype, $CFG->prefix, $field, 'newnameforthefield', true);
+ $test->status = rename_field($table, $field, 'newnameforthefield', false, false);
+ if (!$test->status) {
+ $test->error = $db->ErrorMsg();
+ }
+ $tests['rename field'] = $test;
+ }
+
+ /// 40th test. Renaming one table
+ if ($test->status) {
+ /// Get SQL code and execute it
+ $test = new stdClass;
+
+ $test->sql = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, 'newnameforthetable', true);
+ $db->debug = true;
+ $test->status = rename_table($table, 'newnameforthetable', false, false);
+ $db->debug = false;
+ if (!$test->status) {
+ $test->error = $db->ErrorMsg();
+ }
+ $tests['rename table'] = $test;
+ }
+
/// TODO: Check here values of the inserted records to see that everything ha the correct value
/// Check table doesn't exist
if (table_exists($table)) {
- debugging('Table ' . $table->getName() . ' exists. Skipping its creation', DEBUG_DEVELOPER);
+ debugging('Table ' . $table->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
return true; //Table exists, nothing to do
}
/// Check table exists
if (!table_exists($table)) {
- debugging('Table ' . $table->getName() . ' don not exist. Skipping its deletion', DEBUG_DEVELOPER);
+ debugging('Table ' . $table->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
return true; //Table don't exist, nothing to do
}
return execute_sql_arr($sqlarr, $continue, $feedback);
}
+/**
+ * This function will rename the table passed as argument
+ * Before renaming the index, the function will check it exists
+ *
+ * @uses $CFG, $db
+ * @param XMLDBTable table object (just the name is mandatory)
+ * @param string new name of the index
+ * @param boolean continue to specify if must continue on error (true) or stop (false)
+ * @param boolean feedback to specify to show status info (true) or not (false)
+ * @return boolean true on success, false on error
+ */
+function rename_table($table, $newname, $continue=true, $feedback=true) {
+
+ global $CFG, $db;
+
+ $status = true;
+
+ if (strtolower(get_class($table)) != 'xmldbtable') {
+ return false;
+ }
+
+/// Check table exists
+ if (!table_exists($table)) {
+ debugging('Table ' . $table->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER);
+ return true; //Table doesn't exist, nothing to do
+ }
+
+/// Check newname isn't empty
+ if (!$newname) {
+ debugging('New name for table ' . $index->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
+ return true; //Table doesn't exist, nothing to do
+ }
+
+ if(!$sqlarr = $table->getRenameTableSQL($CFG->dbtype, $CFG->prefix, $newname, false)) {
+ return true; //Empty array = nothing to do = no error
+ }
+
+ return execute_sql_arr($sqlarr, $continue, $feedback);
+}
+
/**
* This function will add the field to the table passed as arguments
*
/// Check the field doesn't exist
if (field_exists($table, $field)) {
- debugging('Field ' . $field->getName() . ' exists. Skipping its creation', DEBUG_DEVELOPER);
+ debugging('Field ' . $field->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
return true;
}
/// Check the field exists
if (!field_exists($table, $field)) {
- debugging('Field ' . $field->getName() . ' do not exist. Skipping its deletion', DEBUG_DEVELOPER);
+ debugging('Field ' . $field->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
return true;
}
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 creation time only', DEBUG_DEVELOPER);
+ debugging('Primary Keys can be added at table create time only', DEBUG_DEVELOPER);
return true;
}
/// Check newname isn't empty
if (!$newname) {
- debugging('New name for key ' . $key->getName() . ' is empty! Skipping its renaming', DEBUG_DEVELOPER);
+ debugging('New name for key ' . $key->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
return true; //Key doesn't exist, nothing to do
}
$key->setName($newname);
if(!$sqlarr = $table->getRenameKeySQL($CFG->dbtype, $CFG->prefix, $key, false)) {
- debugging('Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Skipping its renaming', DEBUG_DEVELOPER);
+ debugging('Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Rename skipped', DEBUG_DEVELOPER);
return true; //Empty array = nothing to do = no error
}
/// Check index doesn't exist
if (index_exists($table, $index)) {
- debugging('Index ' . $index->getName() . ' exists. Skipping its creation', DEBUG_DEVELOPER);
+ debugging('Index ' . $index->getName() . ' exists. Create skipped', DEBUG_DEVELOPER);
return true; //Index exists, nothing to do
}
/// Check index exists
if (!index_exists($table, $index)) {
- debugging('Index ' . $index->getName() . ' do not exist. Skipping its deletion', DEBUG_DEVELOPER);
+ debugging('Index ' . $index->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER);
return true; //Index doesn't exist, nothing to do
}
/// Check index exists
if (!index_exists($table, $index)) {
- debugging('Index ' . $index->getName() . ' do not exist. Skipping its renaming', DEBUG_DEVELOPER);
+ debugging('Index ' . $index->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER);
return true; //Index doesn't exist, nothing to do
}
/// Check newname isn't empty
if (!$newname) {
- debugging('New name for index ' . $index->getName() . ' is empty! Skipping its renaming', DEBUG_DEVELOPER);
+ debugging('New name for index ' . $index->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER);
return true; //Index doesn't exist, nothing to do
}
$index->setName($newname);
if(!$sqlarr = $table->getRenameIndexSQL($CFG->dbtype, $CFG->prefix, $index, false)) {
- debugging('Some DBs do not support index renaming (MySQL). Skipping its renaming', DEBUG_DEVELOPER);
+ debugging('Some DBs do not support index renaming (MySQL). Rename skipped', DEBUG_DEVELOPER);
return true; //Empty array = nothing to do = no error
}