=== modified file 'includes/actions.inc' --- includes/actions.inc 2008-12-20 05:20:20 +0000 +++ includes/actions.inc 2009-03-04 22:07:30 +0000 @@ -325,7 +325,7 @@ function actions_save($function, $type, // aid is the callback for singleton actions so we need to keep a separate // table for numeric aids. if (!$aid) { - $aid = db_insert('actions_aid')->useDefaults(array('aid'))->execute(); + $aid = db_next_id('actions'); } db_merge('actions') === modified file 'includes/database/database.inc' --- includes/database/database.inc 2009-01-25 12:19:31 +0000 +++ includes/database/database.inc 2009-03-04 22:12:53 +0000 @@ -1027,6 +1027,22 @@ abstract class DatabaseConnection extend public function commit() { throw new ExplicitTransactionsNotSupportedException(); } + + /** + * Retrieves an unique id from a given sequence. + * + * Use this function if for some reason you can't use a serial field. + * + * @param $sequence + * The name of the sequence. + * @return + * An integer number larger than any number returned by earlier calls + * to this function with the same sequence argument. + */ + public function nextId($sequence) { + // In the degenerate case every id comes from a single source. + return $this->insert('sequences')->useDefaults(array('sid'))->execute(); + } } /** @@ -2023,6 +2039,22 @@ function db_driver() { } /** + * Retrieves an unique id from a given sequence. Use db_last_insert_id is preferred. + * + * Use this function if for some reason you can't use a serial field, + * normally a serial field with db_last_insert_id is preferred. + * + * @param $sequence + * The name of the sequence. + * @return + * An integer number larger than any number returned before for this + * sequence. + */ +function db_next_id($sequence) { + return Database::getActiveConnection()->nextId($sequence); +} + +/** * @} End of "defgroup database". */ === added directory 'modules/queue' === modified file 'modules/simpletest/simpletest.install' --- modules/simpletest/simpletest.install 2009-02-22 17:55:28 +0000 +++ modules/simpletest/simpletest.install 2009-03-04 22:07:30 +0000 @@ -197,17 +197,5 @@ function simpletest_schema() { 'reporter' => array('test_class', 'message_id'), ), ); - $schema['simpletest_test_id'] = array( - 'description' => 'Stores simpletest test IDs, used to auto-incrament the test ID so that a fresh test ID is used.', - 'fields' => array( - 'test_id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'Primary Key: Unique simpletest ID used to group test results together. Each time a set of tests - are run a new test ID is used.', - ), - ), - 'primary key' => array('test_id'), - ); return $schema; } === modified file 'modules/simpletest/simpletest.module' --- modules/simpletest/simpletest.module 2009-02-22 17:55:28 +0000 +++ modules/simpletest/simpletest.module 2009-03-04 22:09:21 +0000 @@ -350,7 +350,7 @@ function simpletest_test_form_submit($fo */ function simpletest_run_tests($test_list, $reporter = 'drupal') { cache_clear_all(); - $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute(); + $test_id = db_next_id('simpletest'); // Get the info for the first test being run. $first_test = array_shift($test_list); @@ -524,8 +524,7 @@ function simpletest_clean_database() { $schema = drupal_get_schema_unprocessed('simpletest'); $ret = array(); foreach (array_diff_key($tables, $schema) as $table) { - // Strip the prefix and skip tables without digits following "simpletest", - // e.g. {simpletest_test_id}. + // Strip the prefix and skip tables without digits following "simpletest". if (preg_match('/simpletest\d+.*/', $table, $matches)) { db_drop_table($ret, $matches[0]); } @@ -572,21 +571,13 @@ function simpletest_clean_temporary_dire function simpletest_clean_results_table($test_id = NULL) { if (variable_get('simpletest_clear_results', TRUE)) { if ($test_id) { - $count = db_result(db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))); - - db_delete("simpletest") - ->condition('test_id', $test_id) - ->execute(); - db_delete("simpletest_test_id") + $count = db_delete("simpletest") ->condition('test_id', $test_id) ->execute(); } else { - $count = db_result(db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')); - // Clear test results. - db_delete("simpletest")->execute(); - db_delete("simpletest_test_id")->execute(); + $count = db_delete("simpletest")->execute(); } return $count; === modified file 'modules/simpletest/tests/database_test.test' --- modules/simpletest/tests/database_test.test 2009-02-26 07:30:26 +0000 +++ modules/simpletest/tests/database_test.test 2009-03-04 22:07:30 +0000 @@ -2544,3 +2544,25 @@ class DatabaseTransactionTestCase extend } } } + +/** + * Check the sequences API. + */ +class DatabaseNextIdCase extends DatabaseTestCase { + function getInfo() { + return array( + 'name' => t('Sequences API'), + 'description' => t('Test the secondary sequences API.'), + 'group' => t('Database'), + ); + } + + /** + * Test that the sequences API work. + */ + function testDbNextId() { + $first = db_next_id('test'); + $second = db_next_id('test'); + $this->assertTrue($first < $second, t('The second call from a sequence provides a larger number')); + } +} === modified file 'modules/system/system.install' --- modules/system/system.install 2009-03-01 09:32:16 +0000 +++ modules/system/system.install 2009-03-04 22:07:30 +0000 @@ -473,19 +473,6 @@ function system_schema() { 'primary key' => array('aid'), ); - $schema['actions_aid'] = array( - 'description' => 'Stores action IDs for non-default actions.', - 'fields' => array( - 'aid' => array( - 'description' => 'Primary Key: Unique actions ID.', - 'type' => 'serial', - 'unsigned' => TRUE, - 'not null' => TRUE, - ), - ), - 'primary key' => array('aid'), - ); - $schema['batch'] = array( 'description' => 'Stores details about batches (processes that run in multiple HTTP requests).', 'fields' => array( @@ -1110,6 +1097,19 @@ function system_schema() { 'primary key' => array('filename'), ); + $schema['sequences'] = array( + 'description' => 'Stores IDs.', + 'fields' => array( + 'sid' => array( + 'description' => 'Primary Key: Unique ID.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + ), + 'primary key' => array('sid'), + ); + $schema['sessions'] = array( 'description' => "Drupal's session handlers read and write into the sessions table. Each record represents a user session, either anonymous or authenticated.", 'fields' => array( @@ -3237,6 +3237,16 @@ function system_update_7020() { } /** + * Reuse the actions_aid table as sequences. + */ +function system_update_7018() { + $ret = array(); + db_drop_primary_key($ret, 'actions_aid'); + db_change_field($ret, 'actions_aid', 'aid', 'sid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('sid'))); + db_rename_table($ret, 'actions_aid', 'sequences'); +} + +/** * @} End of "defgroup updates-6.x-to-7.x" * The next series of updates should start at 8000. */ === modified file 'scripts/run-tests.sh' --- scripts/run-tests.sh 2009-02-20 03:37:58 +0000 +++ scripts/run-tests.sh 2009-03-04 22:07:30 +0000 @@ -72,7 +72,7 @@ if (!ini_get('safe_mode')) { simpletest_script_reporter_init(); // Setup database for test results. -$test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute(); +$test_id = db_next_id('simpletest'); // Execute tests. simpletest_script_command($args['concurrency'], $test_id, implode(",", $test_list));