function transform_textfield($table, $field_name, $max_length) { //$table = 'content_type_' . $content_type; $column = 'field_' . $field_name . '_value'; var_dump($table); var_dump($column); // Update field type $new_column = $column; $new_definition = array('type' => 'varchar', 'length' => $max_length); db_change_field($ret, $table, $column, $new_column, $new_definition); // Fetch the current field configuration $query_results = db_query("SELECT global_settings FROM {content_node_field} WHERE field_name = 'field_%s'", $field_name); // We assume that there is only one entry in the table for each field $global_settings = db_result($query_results); var_dump($global_settings); // This is a serialized PHP array (i.e. stored as a string), so unserialize it $data = unserialize($global_settings); var_dump($data); // Update the max_length entry $data['max_length'] = $max_length; // Update the field config var_dump(serialize($data)); db_query("UPDATE {content_node_field} SET global_settings = '%s' WHERE field_name = 'field_%s'", serialize($data), $field_name); }
/** * Asserts that a field can be changed from one spec to another. * * @param $old_spec * The beginning field specification. * @param $new_spec * The ending field specification. */ protected function assertFieldChange($old_spec, $new_spec, $test_data = NULL) { $table_name = 'test_table_' . $this->counter++; $table_spec = array('fields' => array('serial_column' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), 'test_field' => $old_spec), 'primary key' => array('serial_column')); db_create_table($table_name, $table_spec); $this->pass(format_string('Table %table created.', array('%table' => $table_name))); // Check the characteristics of the field. $this->assertFieldCharacteristics($table_name, 'test_field', $old_spec); // Remove inserted rows. db_truncate($table_name)->execute(); if ($test_data) { $id = db_insert($table_name)->fields(['test_field'], [$test_data])->execute(); } // Change the field. db_change_field($table_name, 'test_field', 'test_field', $new_spec); if ($test_data) { $field_value = db_select($table_name)->fields($table_name, ['test_field'])->condition('serial_column', $id)->execute()->fetchField(); $this->assertIdentical($field_value, $test_data); } // Check the field was changed. $this->assertFieldCharacteristics($table_name, 'test_field', $new_spec); // Clean-up. db_drop_table($table_name); }
/** * Asserts that a field can be changed from one spec to another. * * @param $old_spec * The beginning field specification. * @param $new_spec * The ending field specification. */ protected function assertFieldChange($old_spec, $new_spec) { $table_name = 'test_table_' . $this->counter++; $table_spec = array('fields' => array('serial_column' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), 'test_field' => $old_spec), 'primary key' => array('serial_column')); db_create_table($table_name, $table_spec); $this->pass(format_string('Table %table created.', array('%table' => $table_name))); // Check the characteristics of the field. $this->assertFieldCharacteristics($table_name, 'test_field', $old_spec); // Remove inserted rows. db_truncate($table_name)->execute(); // Change the field. db_change_field($table_name, 'test_field', 'test_field', $new_spec); // Check the field was changed. $this->assertFieldCharacteristics($table_name, 'test_field', $new_spec); // Clean-up. db_drop_table($table_name); }
function elysia_cron_check_version_update() { $ver = variable_get('elysia_cron_version', 0); if ($ver < 20111012) { $ver = _ec_variable_get('elysia_cron_version', 0); } if (!$ver || $ver < 20090218) { $unchanged = array('elysia_cron_last_context', 'elysia_cron_last_run', 'elysia_cron_disabled', 'elysia_cron_semaphore', 'elysia_cron_key', 'elysia_cron_allowed_hosts', 'elysia_cron_default_rule', 'elysia_cron_script', 'elysia_cron_runtime_replacement', 'elysia_cron_version'); $rs = db_query('select * from {variable} where name like "elysia_cron_%%"'); while ($v = db_fetch_object($rs)) { if (!in_array($v->name, $unchanged)) { $vn = false; if (preg_match('/^elysia_cron_ctx_(.*)_(running|disabled|last_run|last_aborted|abort_count|execution_count|last_execution_time|avg_execution_time|max_execution_time|last_shutdown_time|last_abort_function)/', $v->name, $r)) { switch ($r[2]) { case 'running': $vn = 'ecc_' . _ec_get_name($r[1]) . '_r'; break; case 'disabled': $vn = 'ecc_' . _ec_get_name($r[1]) . '_d'; break; case 'last_run': $vn = 'ecc_' . _ec_get_name($r[1]) . '_lr'; break; case 'last_aborted': $vn = 'ecc_' . _ec_get_name($r[1]) . '_la'; break; case 'abort_count': $vn = 'ecc_' . _ec_get_name($r[1]) . '_ac'; break; case 'execution_count': $vn = 'ecc_' . _ec_get_name($r[1]) . '_ec'; break; case 'last_execution_time': $vn = 'ecc_' . _ec_get_name($r[1]) . '_let'; break; case 'avg_execution_time': $vn = 'ecc_' . _ec_get_name($r[1]) . '_aet'; break; case 'max_execution_time': $vn = 'ecc_' . _ec_get_name($r[1]) . '_met'; break; case 'last_shutdown_time': $vn = 'ecc_' . _ec_get_name($r[1]) . '_lst'; break; case 'last_abort_function': $vn = 'ecc_' . _ec_get_name($r[1]) . '_laf'; break; } } elseif (preg_match('/^elysia_cron_(.*)_(rule|disabled|context|running|last_run|last_execution_time|execution_count|avg_execution_time|max_execution_time)/', $v->name, $r)) { switch ($r[2]) { case 'rule': $vn = 'ec_' . _ec_get_name($r[1]) . '_rul'; break; case 'disabled': $vn = 'ec_' . _ec_get_name($r[1]) . '_d'; break; case 'context': $vn = 'ec_' . _ec_get_name($r[1]) . '_c'; break; case 'running': $vn = 'ec_' . _ec_get_name($r[1]) . '_r'; break; case 'last_run': $vn = 'ec_' . _ec_get_name($r[1]) . '_lr'; break; case 'last_execution_time': $vn = 'ec_' . _ec_get_name($r[1]) . '_let'; break; case 'execution_count': $vn = 'ec_' . _ec_get_name($r[1]) . '_ec'; break; case 'avg_execution_time': $vn = 'ec_' . _ec_get_name($r[1]) . '_aet'; break; case 'max_execution_time': $vn = 'ec_' . _ec_get_name($r[1]) . '_met'; break; } } if ($vn) { variable_set($vn, unserialize($v->value)); } else { _dco_watchdog('cron', 'Error in update, cant convert %name (value: %value)', array('%name' => $v->name, '%value' => $v->value), WATCHDOG_ERROR); } variable_del($v->name); } } variable_set('elysia_cron_version', 20090218); } if ($ver < 20090920) { variable_set('elysia_cron_version', 20090920); } if ($ver < 20100507) { if (EC_DRUPAL_VERSION >= 6) { // D6 drupal_install_schema('elysia_cron'); // In ver 20111020 disabled has been renamed to disable, revert it now if (EC_DRUPAL_VERSION >= 7) { // D7 // Must use "$v" for PHP5.3 running D6 version (detect the error even if it doesn't pass here) db_change_field($v = 'elysia_cron', 'disable', 'disabled', array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE)); } elseif (EC_DRUPAL_VERSION >= 6) { // D6 $ret = array(); db_change_field($ret, 'elysia_cron', 'disable', 'disabled', array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE)); } } else { // D5 switch ($GLOBALS['db_type']) { case 'mysqli': case 'mysql': db_query("create table if not exists {elysia_cron} (\r\n name varchar(120) not null,\r\n disabled tinyint(1) not null default '0',\r\n rule varchar(32),\r\n weight int(11) not null default '0',\r\n context varchar(32),\r\n running int(11) not null default '0',\r\n last_run int(11) not null default '0',\r\n last_aborted tinyint(1) not null default '0',\r\n abort_count int(11) not null default '0',\r\n last_abort_function varchar(32),\r\n last_execution_time int(11) not null default '0',\r\n execution_count int(11) not null default '0',\r\n avg_execution_time float(5,2) not null default '0',\r\n max_execution_time int(11) not null default '0',\r\n last_shutdown_time int(11) not null default '0',\r\n primary key (name)\r\n )"); break; case 'pgsql': db_query("create table {elysia_cron} (\r\n name varchar(120) not null,\r\n disabled smallint not null default '0',\r\n rule varchar(32),\r\n weight integer not null default '0',\r\n context varchar(32),\r\n running int not null default '0',\r\n last_run integer not null default '0',\r\n last_aborted smallint not null default '0',\r\n abort_count integer not null default '0',\r\n last_abort_function varchar(32),\r\n last_execution_time integer not null default '0',\r\n execution_count integer not null default '0',\r\n avg_execution_time float not null default '0',\r\n max_execution_time integer not null default '0',\r\n last_shutdown_time integer not null default '0',\r\n primary key (name)\r\n )"); } } $rs = db_query('select * from {variable} where name like "ec_%%" or name like "ecc_%%"'); $data = array(); $todelete = array(); while ($v = db_fetch_object($rs)) { $name = false; if (preg_match('/^ecc_(.+)_(r|d|lr|la|ac|ec|let|aet|met|lst|laf)/', $v->name, $r)) { $name = ':' . $r[1]; } elseif (preg_match('/^ec_(.+)_(rul|d|c|w|r|lr|let|ec|aet|met)/', $v->name, $r)) { $name = $r[1]; } if ($name) { if (!isset($data[$name])) { $data[$name] = array('name' => $name); } switch ($r[2]) { case 'r': $f = 'running'; break; case 'd': $f = 'disabled'; break; case 'rul': $f = 'rule'; break; case 'w': $f = 'weight'; break; case 'c': $f = 'context'; break; case 'lr': $f = 'last_run'; break; case 'la': $f = 'last_aborted'; break; case 'ac': $f = 'abort_count'; break; case 'laf': $f = 'last_abort_function'; break; case 'let': $f = 'last_execution_time'; break; case 'ec': $f = 'execution_count'; break; case 'aet': $f = 'avg_execution_time'; break; case 'met': $f = 'max_execution_time'; break; case 'lst': $f = 'last_shutdown_time'; break; } $data[$name][$f] = unserialize($v->value); $todelete[] = $v->name; } } $ifields = array('disabled', 'weight', 'running', 'last_run', 'last_aborted', 'abort_count', 'last_execution_time', 'execution_count', 'avg_execution_time', 'max_execution_time', 'last_shutdown_time'); foreach ($data as $v) { foreach ($ifields as $f) { if (empty($v[$f])) { $v[$f] = 0; } } db_query("insert into {elysia_cron} (name, disabled, rule, weight, context, running, last_run, last_aborted, abort_count, last_abort_function, last_execution_time, execution_count, avg_execution_time, max_execution_time, last_shutdown_time)\r\n values ('%s', %d, '%s', %d, '%s', %d, %d, %d, %d, '%s', %d, %d, %f, %d, %d)", $v['name'], $v['disabled'], $v['rule'], $v['weight'], $v['context'], $v['running'], $v['last_run'], $v['last_aborted'], $v['abort_count'], $v['last_abort_function'], $v['last_execution_time'], $v['execution_count'], $v['avg_execution_time'], $v['max_execution_time'], $v['last_shutdown_time']); } db_query("update {elysia_cron} set context = null where context = ''"); db_query("update {elysia_cron} set rule = null where rule = ''"); foreach ($todelete as $v) { variable_del($v); db_query("DELETE FROM {variable} WHERE name = '%s'", $v); } variable_set('elysia_cron_version', 20100507); unset($GLOBALS['_ec_variables']); } // D7 VERSION FROM NOW ON... if ($ver < 20110323) { if (EC_DRUPAL_VERSION >= 7) { // D7 // Must use "$v" for PHP5.3 running D6 version (detect the error even if it doesn't pass here) db_change_field($v = 'elysia_cron', 'weight', 'weight', array('type' => 'int', 'not null' => FALSE)); } elseif (EC_DRUPAL_VERSION >= 6) { // D6 $ret = array(); db_change_field($ret, 'elysia_cron', 'weight', 'weight', array('type' => 'int', 'not null' => FALSE)); } else { // D5 db_query("alter table {elysia_cron} change weight weight int(11)"); } variable_set('elysia_cron_version', 20110323); } if ($ver < 20111007) { $default_rules = variable_get('elysia_cron_default_rules', $GLOBALS['elysia_cron_default_rules']); if (!empty($default_rules['*/6 * * * *']) && $default_rules['*/6 * * * *'] == 'Every 6 hours') { unset($default_rules['*/6 * * * *']); $default_rules['0 */6 * * *'] = 'Every 6 hours'; variable_set('elysia_cron_default_rules', $default_rules); } variable_set('elysia_cron_version', 20111007); } if ($ver < 20111012) { // I only need to rebuild variable cache, so i just set the new version variable_set('elysia_cron_version', 20111012); } if ($ver < 20111020) { if (EC_DRUPAL_VERSION >= 7) { // D7 // Must use "$v" for PHP5.3 running D6 version (detect the error even if it doesn't pass here) db_change_field($v = 'elysia_cron', 'disabled', 'disable', array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE)); } elseif (EC_DRUPAL_VERSION >= 6) { // D6 $ret = array(); db_change_field($ret, 'elysia_cron', 'disabled', 'disable', array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE)); } else { // D5 db_query("alter table {elysia_cron} change disabled disable tinyint(1)"); } db_query("update {elysia_cron} set disable = NULL where disable = 0"); variable_set('elysia_cron_version', 20111020); } }
<?php // from http://drupal.stackexchange.com/questions/79378/changing-a-field-type-from-integer-to-decimal // Change this to your field name, obvs. $field = 'field_height'; // Update the storage tables $tables = array('field_data', 'field_revision'); foreach ($tables as $table) { $tablename = $table . '_' . $field; $fieldname = $field . '_value'; db_change_field($tablename, $fieldname, $fieldname, array('type' => 'numeric', 'precision' => 10, 'scale' => 1, 'not null' => FALSE)); } // Fetch the current field configuration $field_config = db_query("SELECT data FROM {field_config} WHERE field_name = :field_name", array(':field_name' => $field))->fetchObject(); $data = unserialize($field_config->data); // Update the settings entry $data['settings'] = array('precision' => 10, 'scale' => 1, 'decimal_separator' => '.'); // Store the new field config, update the field type at the same time db_update('field_config')->fields(array('data' => serialize($data), 'type' => 'number_decimal'))->condition('field_name', $field)->execute(); // If you are confident about what bundles have instances of this field you can // go straight to the db_query with a hardcoded entity_type / bundle. $instances = field_info_field_map(); foreach ($instances[$field]['bundles'] as $entity_type => $bundles) { foreach ($bundles as $bundle) { // Fetch the field instance data $field_config_instance = db_query("SELECT data FROM {field_config_instance}\n WHERE field_name = :field_name\n AND entity_type = :entity_type\n AND bundle = :bundle", array(':field_name' => $field, ':entity_type' => $entity_type, ':bundle' => $bundle))->fetchObject(); $data = unserialize($field_config_instance->data); // Update it with the new display type $data['display']['default']['type'] = 'number_decimal'; // Store it back to the database db_update('field_config_instance')->fields(array('data' => serialize($data)))->condition('field_name', $field)->condition('entity_type', $entity_type)->condition('bundle', $bundle)->execute();
/** * Change the machine name of an existing field. * * @param $newName string * * NOTE: This might need additional adjustments for contrib modules * that store field_names (ie. views, context, cck_blocks). */ public function rename($newName) { $o = $this->field_name; $n = $newName; db_query("UPDATE field_config SET field_name='{$n}' WHERE field_name='{$o}'"); db_query("UPDATE field_config_instance SET field_name='{$n}' WHERE field_name='{$o}'"); db_query("RENAME TABLE `field_data_{$o}` TO `field_data_{$n}`;"); db_query("RENAME TABLE `field_revision_{$o}` TO `field_revision_{$n}`;"); \module_load_install($this->module); $function = $this->module . '_field_schema'; $schema = $function(array('type' => $this->type)); foreach ($schema['columns'] as $column => $specs) { db_change_field("field_data_{$n}", "{$o}_{$column}", "{$n}_{$column}", $specs); db_change_field("field_revision_{$n}", "{$o}_{$column}", "{$n}_{$column}", $specs); } $this->field_name = $n; }
public static function changeSchema(array &$field, array $column_renames = array()) { // Update the field schema $old_schema = array_intersect_key($field, array('columns' => '', 'indexes' => '', 'foreign keys' => '')); module_load_install($field['module']); $new_schema = (array) module_invoke($field['module'], 'field_schema', $field); $new_schema += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); $field['data']['columns'] = $new_schema['columns']; $field['data']['indexes'] = $new_schema['indexes']; $field['data']['foreign keys'] = $new_schema['foreign keys']; $data_table = _field_sql_storage_tablename($field); $revision_table = _field_sql_storage_revision_tablename($field); // Validate that all the columns described in the existing schema actually exist. foreach (array_keys($old_schema['columns']) as $old_column) { $old_column_name = _field_sql_storage_columnname($field['field_name'], $old_column); if (!db_field_exists($data_table, $old_column_name)) { throw new Exception(); } if (!db_field_exists($revision_table, $old_column_name)) { throw new Exception(); } // Attempt to re-use any columns that have the same name. // This can be skipped by setting $column_renames['column-name'] = FALSE; if (!empty($new_schema['columns'][$old_column]) && !isset($column_renames[$old_column])) { $column_renames[$old_column] = $old_column; } } // Validate that any columns to be renamed actually exist. foreach ($column_renames as $old_column => $new_column) { if (!isset($old_schema['columns'][$old_column])) { throw new Exception("Cannot rename field {$field['field_name']} column {$old_column} because it does not exist in the old schema."); } if (!isset($new_schema['columns'][$new_column])) { throw new Exception("Cannot rename field {$field['field_name']} column {$old_column} to {$new_column} because it does not exist in the new schema."); } } // Remove all existing indexes. foreach ($old_schema['indexes'] as $index => $index_fields) { $index_name = _field_sql_storage_indexname($field['field_name'], $index); if (db_index_exists($data_table, $index_name)) { watchdog('helper', "Dropped index {$data_table}.{$index_name}"); db_drop_index($data_table, $index_name); } if (db_index_exists($revision_table, $index_name)) { watchdog('helper', "Dropped index {$revision_table}.{$index_name}"); db_drop_index($revision_table, $index_name); } } // Rename any columns. foreach ($column_renames as $old_column => $new_column) { $old_column_name = _field_sql_storage_columnname($field['field_name'], $old_column); if ($new_column === FALSE) { db_drop_field($data_table, $old_column_name); watchdog('helper', "Dropped column {$data_table}.{$old_column_name}"); db_drop_field($revision_table, $old_column_name); watchdog('helper', "Dropped column {$revision_table}.{$old_column_name}"); unset($old_schema['columns'][$old_column]); } else { $new_column_name = _field_sql_storage_columnname($field['field_name'], $new_column); db_change_field($data_table, $old_column_name, $new_column_name, $new_schema['columns'][$new_column]); watchdog('helper', "Changed column {$data_table}.{$old_column_name}<br/><pre>" . print_r($new_schema['columns'][$new_column], TRUE) . '</pre>'); db_change_field($revision_table, $old_column_name, $new_column_name, $new_schema['columns'][$new_column]); watchdog('helper', "Changed column {$revision_table}.{$old_column_name}<br/><pre>" . print_r($new_schema['columns'][$new_column], TRUE) . '</pre>'); // Remove these fields so they aren't removed or added in the code below. unset($new_schema['columns'][$new_column]); unset($old_schema['columns'][$old_column]); } } // Remove any old columns. $old_columns = array_diff_key($old_schema['columns'], $new_schema['columns']); foreach (array_keys($old_columns) as $old_column) { $old_column_name = _field_sql_storage_columnname($field['field_name'], $old_column); db_drop_field($data_table, $old_column_name); watchdog('helper', "Dropped column {$data_table}.{$old_column_name}"); db_drop_field($revision_table, $old_column_name); watchdog('helper', "Dropped column {$revision_table}.{$old_column_name}"); } // Add any new columns. $new_columns = array_diff_key($new_schema['columns'], $old_schema['columns']); foreach (array_keys($new_columns) as $new_column) { $new_column_name = _field_sql_storage_columnname($field['field_name'], $new_column); db_add_field($data_table, $new_column_name, $new_schema['columns'][$new_column]); watchdog('helper', "Added column {$data_table}.{$new_column_name}"); db_add_field($revision_table, $new_column_name, $new_schema['columns'][$new_column]); watchdog('helper', "Added column {$revision_table}.{$new_column_name}"); } // Re-add indexes. foreach ($new_schema['indexes'] as $index => $index_fields) { foreach ($index_fields as &$index_field) { if (is_array($index_field)) { $index_field[0] = _field_sql_storage_columnname($field['field_name'], $index_field[0]); } else { $index_field = _field_sql_storage_columnname($field['field_name'], $index_field); } } $index_name = _field_sql_storage_indexname($field['field_name'], $index); db_add_index($data_table, $index_name, $index_fields); watchdog('helper', "Added index {$data_table}.{$index_name}<br/><pre>" . print_r($index_fields, TRUE) . '</pre>'); db_add_index($revision_table, $index_name, $index_fields); watchdog('helper', "Added index {$revision_table}.{$index_name}<br/><pre>" . print_r($index_fields, TRUE) . '</pre>'); } }
/** * Tests database interactions. */ function testSchema() { // Try creating a table. $table_specification = array('description' => 'Schema table description may contain "quotes" and could be long—very long indeed.', 'fields' => array('id' => array('type' => 'int', 'default' => NULL), 'test_field' => array('type' => 'int', 'not null' => TRUE, 'description' => 'Schema table description may contain "quotes" and could be long—very long indeed. There could be "multiple quoted regions".'), 'test_field_string' => array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => "'\"funky default'\"", 'description' => 'Schema column description for string.'))); db_create_table('test_table', $table_specification); // Assert that the table exists. $this->assertTrue(db_table_exists('test_table'), 'The table exists.'); // Assert that the table comment has been set. $this->checkSchemaComment($table_specification['description'], 'test_table'); // Assert that the column comment has been set. $this->checkSchemaComment($table_specification['fields']['test_field']['description'], 'test_table', 'test_field'); // An insert without a value for the column 'test_table' should fail. $this->assertFalse($this->tryInsert(), 'Insert without a default failed.'); // Add a default value to the column. db_field_set_default('test_table', 'test_field', 0); // The insert should now succeed. $this->assertTrue($this->tryInsert(), 'Insert with a default succeeded.'); // Remove the default. db_field_set_no_default('test_table', 'test_field'); // The insert should fail again. $this->assertFalse($this->tryInsert(), 'Insert without a default failed.'); // Test for fake index and test for the boolean result of indexExists(). $index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field'); $this->assertIdentical($index_exists, FALSE, 'Fake index does not exists'); // Add index. db_add_index('test_table', 'test_field', array('test_field')); // Test for created index and test for the boolean result of indexExists(). $index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field'); $this->assertIdentical($index_exists, TRUE, 'Index created.'); // Rename the table. db_rename_table('test_table', 'test_table2'); // Index should be renamed. $index_exists = Database::getConnection()->schema()->indexExists('test_table2', 'test_field'); $this->assertTrue($index_exists, 'Index was renamed.'); // Copy the schema of the table. db_copy_table_schema('test_table2', 'test_table3'); // Index should be copied. $index_exists = Database::getConnection()->schema()->indexExists('test_table3', 'test_field'); $this->assertTrue($index_exists, 'Index was copied.'); // Data should still exist on the old table but not on the new one. $count = db_select('test_table2')->countQuery()->execute()->fetchField(); $this->assertEqual($count, 1, 'The old table still has its content.'); $count = db_select('test_table3')->countQuery()->execute()->fetchField(); $this->assertEqual($count, 0, 'The new table has no content.'); // Ensure that the proper exceptions are thrown for db_copy_table_schema(). $fail = FALSE; try { db_copy_table_schema('test_table4', 'test_table5'); } catch (SchemaObjectDoesNotExistException $e) { $fail = TRUE; } $this->assertTrue($fail, 'Ensure that db_copy_table_schema() throws an exception when the source table does not exist.'); $fail = FALSE; try { db_copy_table_schema('test_table2', 'test_table3'); } catch (SchemaObjectExistsException $e) { $fail = TRUE; } $this->assertTrue($fail, 'Ensure that db_copy_table_schema() throws an exception when the destination table already exists.'); // We need the default so that we can insert after the rename. db_field_set_default('test_table2', 'test_field', 0); $this->assertFalse($this->tryInsert(), 'Insert into the old table failed.'); $this->assertTrue($this->tryInsert('test_table2'), 'Insert into the new table succeeded.'); // We should have successfully inserted exactly two rows. $count = db_query('SELECT COUNT(*) FROM {test_table2}')->fetchField(); $this->assertEqual($count, 2, 'Two fields were successfully inserted.'); // Try to drop the table. db_drop_table('test_table2'); $this->assertFalse(db_table_exists('test_table2'), 'The dropped table does not exist.'); // Recreate the table. db_create_table('test_table', $table_specification); db_field_set_default('test_table', 'test_field', 0); db_add_field('test_table', 'test_serial', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.')); // Assert that the column comment has been set. $this->checkSchemaComment('Added column description.', 'test_table', 'test_serial'); // Change the new field to a serial column. db_change_field('test_table', 'test_serial', 'test_serial', array('type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'), array('primary key' => array('test_serial'))); // Assert that the column comment has been set. $this->checkSchemaComment('Changed column description.', 'test_table', 'test_serial'); $this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.'); $max1 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField(); $this->assertTrue($this->tryInsert(), 'Insert with a serial succeeded.'); $max2 = db_query('SELECT MAX(test_serial) FROM {test_table}')->fetchField(); $this->assertTrue($max2 > $max1, 'The serial is monotone.'); $count = db_query('SELECT COUNT(*) FROM {test_table}')->fetchField(); $this->assertEqual($count, 2, 'There were two rows.'); // Use database specific data type and ensure that table is created. $table_specification = array('description' => 'Schema table description.', 'fields' => array('timestamp' => array('mysql_type' => 'timestamp', 'pgsql_type' => 'timestamp', 'sqlite_type' => 'datetime', 'not null' => FALSE, 'default' => NULL))); try { db_create_table('test_timestamp', $table_specification); } catch (\Exception $e) { } $this->assertTrue(db_table_exists('test_timestamp'), 'Table with database specific datatype was created.'); }