$dbman->drop_key($table, $key);
}
- public function testChangeFieldEnum() {
+ /**
+ * Test behaviour of drop_enum_from_field() and related functions (find_check_constraint_name
+ * and check_constraint_exists). Needed to be able to drop existing "enum" fields in the upgrade
+ * from 1.9 to 2.0, will be completely deleted for Moodle 2.1
+ *
+ * Because we already have dropped support for creation of enum fields in 2.0, we are going to
+ * create them here "manually" (hardcoded DB-dependent SQL). Just to be able to test the
+ * find and drop functions properly.
+ *
+ * TODO: Drop this tests completely from Moodle 2.1
+ */
+ public function test_drop_enum_from_field() {
$DB = $this->tdb; // do not use global $DB!
$dbman = $this->tdb->get_manager();
+ // Create normal table, no enums.
$table = new xmldb_table('test_table_cust0');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general');
+ $field = new xmldb_field('type');
+ $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general');
+ $table->addField($field);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
- // Removing an enum value
- $field = new xmldb_field('type');
- $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null);
- $dbman->drop_enum_from_field($table, $field);
+ $this->assertTrue($dbman->table_exists($table));
+ $this->assertTrue($dbman->field_exists($table, $field));
+
+ // Check table hasn't enums at all
+ $this->assertFalse($dbman->check_constraint_exists($table, $field));
+ $this->assertFalse($dbman->find_check_constraint_name($table, $field));
+ ob_start();
+ $this->assertFalse($dbman->drop_enum_from_field($table, $field)); // This just outputs debug warning if field hasn't enums
+ ob_end_clean();
+ // Insert some info
$record = new object();
$record->course = 666;
$record->type = 'qanda';
$this->assertTrue($DB->insert_record('test_table_cust0', $record, false));
- // Adding an enum value
- $field = new xmldb_field('type');
- $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course');
- $dbman->drop_enum_from_field($table, $field);
+ // Hackery starts here, depending of the db family we are testing... execute
+ // the needed SQL statements to get the "type" field defined as enum
+ $stmt = '';
+ switch ($DB->get_dbfamily()) {
+ case 'mysql': // It's ENUM field for mysql
+ $stmt = "ALTER TABLE {$DB->get_prefix()}test_table_cust0 MODIFY type ENUM ('general', 'qanda', 'moodle') NOT NULL DEFAULT 'general'";
+ break;
+ default: // It's check constraint for "normal" DBs
+ $stmt = "ALTER TABLE {$DB->get_prefix()}test_table_cust0 ADD CONSTRAINT ttcu0_ck CHECK (type IN ('general', 'qanda', 'moodle'))";
+ }
+ $DB->change_database_structure($stmt);
- $record = new object();
- $record->course = 666;
- $record->type = 'nothing';
+ // Check table has enums now
+ $this->assertTrue($dbman->check_constraint_exists($table, $field));
+ $this->assertTrue($dbman->find_check_constraint_name($table, $field));
- ob_start(); // hide debug warning
- try {
- $result = $DB->insert_record('test_table_cust0', $record, false);
- } catch (dml_write_exception $e) {
- $result = false;
- }
- ob_end_clean();
- $this->assertFalse($result);
+ // Removing an enum value
+ $dbman->drop_enum_from_field($table, $field);
+
+ // Chech table hasn't enum anymore
+ $this->assertFalse($dbman->check_constraint_exists($table, $field));
+ $this->assertFalse($dbman->find_check_constraint_name($table, $field));
$dbman->drop_table($table);
}
// Skipping: this is just a test of find_index_name
}
- public function testFindCheckConstraintName() {
- $dbman = $this->tdb->get_manager();
-
- $table = $this->create_deftable('test_table0');
- $field = $table->getField('type');
- $result = $dbman->find_check_constraint_name($table, $field);
- $this->assertTrue(!empty($result));
- }
-
- public function testCheckConstraintExists() {
- $dbman = $this->tdb->get_manager();
-
- $table = $this->create_deftable('test_table0');
- $field = $table->getField('type');
- $this->assertTrue($dbman->check_constraint_exists($table, $field), 'type');
- }
-
public function testFindKeyName() {
$dbman = $this->tdb->get_manager();