- Patch #570900 by Crell | asimmonds: Changed Destroy remnants of update_sql().

merge-requests/26/head
Dries Buytaert 2009-09-29 15:13:57 +00:00
parent 0a0b067c24
commit cef1089389
34 changed files with 602 additions and 770 deletions

View File

@ -911,7 +911,7 @@ function _drupal_decode_exception($exception) {
// The first element in the stack is the call, the second element gives us the caller. // The first element in the stack is the call, the second element gives us the caller.
// We skip calls that occurred in one of the classes of the database layer // We skip calls that occurred in one of the classes of the database layer
// or in one of its global functions. // or in one of its global functions.
$db_functions = array('db_query', 'db_query_range', 'update_sql'); $db_functions = array('db_query', 'db_query_range');
while (!empty($backtrace[1]) && ($caller = $backtrace[1]) && while (!empty($backtrace[1]) && ($caller = $backtrace[1]) &&
((isset($caller['class']) && (strpos($caller['class'], 'Query') !== FALSE || strpos($caller['class'], 'Database') !== FALSE || strpos($caller['class'], 'PDO') !== FALSE)) || ((isset($caller['class']) && (strpos($caller['class'], 'Query') !== FALSE || strpos($caller['class'], 'Database') !== FALSE || strpos($caller['class'], 'PDO') !== FALSE)) ||
in_array($caller['function'], $db_functions))) { in_array($caller['function'], $db_functions))) {
@ -4790,11 +4790,9 @@ function drupal_install_schema($module) {
$schema = drupal_get_schema_unprocessed($module); $schema = drupal_get_schema_unprocessed($module);
_drupal_schema_initialize($module, $schema); _drupal_schema_initialize($module, $schema);
$ret = array();
foreach ($schema as $name => $table) { foreach ($schema as $name => $table) {
db_create_table($ret, $name, $table); db_create_table($name, $table);
} }
return $ret;
} }
/** /**
@ -4815,13 +4813,11 @@ function drupal_uninstall_schema($module) {
$schema = drupal_get_schema_unprocessed($module); $schema = drupal_get_schema_unprocessed($module);
_drupal_schema_initialize($module, $schema); _drupal_schema_initialize($module, $schema);
$ret = array();
foreach ($schema as $table) { foreach ($schema as $table) {
if (db_table_exists($table['name'])) { if (db_table_exists($table['name'])) {
db_drop_table($ret, $table['name']); db_drop_table($table['name']);
} }
} }
return $ret;
} }
/** /**

View File

@ -2041,22 +2041,6 @@ function db_escape_table($table) {
return Database::getConnection()->escapeTable($table); return Database::getConnection()->escapeTable($table);
} }
/**
* Perform an SQL query and return success or failure.
*
* @param $sql
* A string containing a complete SQL query. %-substitution
* parameters are not supported.
* @return
* An array containing the keys:
* success: a boolean indicating whether the query succeeded
* query: the SQL query executed, passed through check_plain()
*/
function update_sql($sql) {
$result = Database::getConnection()->query($sql);
return array('success' => $result !== FALSE, 'query' => check_plain($sql));
}
/** /**
* Retrieve the name of the currently active database driver, such as * Retrieve the name of the currently active database driver, such as
* "mysql" or "pgsql". * "mysql" or "pgsql".
@ -2095,16 +2079,14 @@ function db_close(array $options = array()) {
/** /**
* Create a new table from a Drupal table definition. * Create a new table from a Drupal table definition.
* *
* @param $ret
* Array to which query results will be added.
* @param $name * @param $name
* The name of the table to create. * The name of the table to create.
* @param $table * @param $table
* A Schema API table definition array. * A Schema API table definition array.
*/ */
function db_create_table(&$ret, $name, $table) { function db_create_table($name, $table) {
if (!db_table_exists($name)) { if (!db_table_exists($name)) {
return Database::getConnection()->schema()->createTable($ret, $name, $table); return Database::getConnection()->schema()->createTable($name, $table);
} }
} }
@ -2165,34 +2147,28 @@ function db_type_map() {
/** /**
* Rename a table. * Rename a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be renamed. * The table to be renamed.
* @param $new_name * @param $new_name
* The new name for the table. * The new name for the table.
*/ */
function db_rename_table(&$ret, $table, $new_name) { function db_rename_table($table, $new_name) {
return Database::getConnection()->schema()->renameTable($ret, $table, $new_name); return Database::getConnection()->schema()->renameTable($table, $new_name);
} }
/** /**
* Drop a table. * Drop a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be dropped. * The table to be dropped.
*/ */
function db_drop_table(&$ret, $table) { function db_drop_table($table) {
return Database::getConnection()->schema()->dropTable($ret, $table); return Database::getConnection()->schema()->dropTable($table);
} }
/** /**
* Add a new field to a table. * Add a new field to a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table to be altered. * Name of the table to be altered.
* @param $field * @param $field
@ -2212,29 +2188,25 @@ function db_drop_table(&$ret, $table) {
* explanation why. * explanation why.
* @see db_change_field() * @see db_change_field()
*/ */
function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) { function db_add_field($table, $field, $spec, $keys_new = array()) {
return Database::getConnection()->schema()->addField($ret, $table, $field, $spec, $keys_new); return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
} }
/** /**
* Drop a field. * Drop a field.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be dropped. * The field to be dropped.
*/ */
function db_drop_field(&$ret, $table, $field) { function db_drop_field($table, $field) {
return Database::getConnection()->schema()->dropField($ret, $table, $field); return Database::getConnection()->schema()->dropField($table, $field);
} }
/** /**
* Set the default value for a field. * Set the default value for a field.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
@ -2242,55 +2214,47 @@ function db_drop_field(&$ret, $table, $field) {
* @param $default * @param $default
* Default value to be set. NULL for 'default NULL'. * Default value to be set. NULL for 'default NULL'.
*/ */
function db_field_set_default(&$ret, $table, $field, $default) { function db_field_set_default($table, $field, $default) {
return Database::getConnection()->schema()->fieldSetDefault($ret, $table, $field, $default); return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
} }
/** /**
* Set a field to have no default value. * Set a field to have no default value.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be altered. * The field to be altered.
*/ */
function db_field_set_no_default(&$ret, $table, $field) { function db_field_set_no_default($table, $field) {
return Database::getConnection()->schema()->fieldSetNoDefault($ret, $table, $field); return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
} }
/** /**
* Add a primary key. * Add a primary key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $fields * @param $fields
* Fields for the primary key. * Fields for the primary key.
*/ */
function db_add_primary_key(&$ret, $table, $fields) { function db_add_primary_key($table, $fields) {
return Database::getConnection()->schema()->addPrimaryKey($ret, $table, $fields); return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
} }
/** /**
* Drop the primary key. * Drop the primary key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
*/ */
function db_drop_primary_key(&$ret, $table) { function db_drop_primary_key($table) {
return Database::getConnection()->schema()->dropPrimaryKey($ret, $table); return Database::getConnection()->schema()->dropPrimaryKey($table);
} }
/** /**
* Add a unique key. * Add a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -2298,29 +2262,25 @@ function db_drop_primary_key(&$ret, $table) {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
function db_add_unique_key(&$ret, $table, $name, $fields) { function db_add_unique_key($table, $name, $fields) {
return Database::getConnection()->schema()->addUniqueKey($ret, $table, $name, $fields); return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
} }
/** /**
* Drop a unique key. * Drop a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the key. * The name of the key.
*/ */
function db_drop_unique_key(&$ret, $table, $name) { function db_drop_unique_key($table, $name) {
return Database::getConnection()->schema()->dropUniqueKey($ret, $table, $name); return Database::getConnection()->schema()->dropUniqueKey($table, $name);
} }
/** /**
* Add an index. * Add an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -2328,22 +2288,20 @@ function db_drop_unique_key(&$ret, $table, $name) {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
function db_add_index(&$ret, $table, $name, $fields) { function db_add_index($table, $name, $fields) {
return Database::getConnection()->schema()->addIndex($ret, $table, $name, $fields); return Database::getConnection()->schema()->addIndex($table, $name, $fields);
} }
/** /**
* Drop an index. * Drop an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the index. * The name of the index.
*/ */
function db_drop_index(&$ret, $table, $name) { function db_drop_index($table, $name) {
return Database::getConnection()->schema()->dropIndex($ret, $table, $name); return Database::getConnection()->schema()->dropIndex($table, $name);
} }
/** /**
@ -2369,8 +2327,8 @@ function db_drop_index(&$ret, $table, $name) {
* and you want to change foo.bar to be type serial, leaving it as the * and you want to change foo.bar to be type serial, leaving it as the
* primary key. The correct sequence is: * primary key. The correct sequence is:
* @code * @code
* db_drop_primary_key($ret, 'foo'); * db_drop_primary_key('foo');
* db_change_field($ret, 'foo', 'bar', 'bar', * db_change_field('foo', 'bar', 'bar',
* array('type' => 'serial', 'not null' => TRUE), * array('type' => 'serial', 'not null' => TRUE),
* array('primary key' => array('bar'))); * array('primary key' => array('bar')));
* @endcode * @endcode
@ -2393,8 +2351,6 @@ function db_drop_index(&$ret, $table, $name) {
* unless you are converting a field to be type serial. You can use * unless you are converting a field to be type serial. You can use
* the $keys_new argument in all cases. * the $keys_new argument in all cases.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table. * Name of the table.
* @param $field * @param $field
@ -2408,9 +2364,8 @@ function db_drop_index(&$ret, $table, $name) {
* table along with changing the field. The format is the same as a * table along with changing the field. The format is the same as a
* table specification but without the 'fields' element. * table specification but without the 'fields' element.
*/ */
function db_change_field($table, $field, $field_new, $spec, $keys_new = array()) {
function db_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) { return Database::getConnection()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
return Database::getConnection()->schema()->changeField($ret, $table, $field, $field_new, $spec, $keys_new);
} }
/** /**

View File

@ -243,40 +243,40 @@ class DatabaseSchema_mysql extends DatabaseSchema {
} }
protected function createKeySql($fields) { protected function createKeySql($fields) {
$ret = array(); $return = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = '`' . $field[0] . '`(' . $field[1] . ')'; $return[] = '`' . $field[0] . '`(' . $field[1] . ')';
} }
else { else {
$ret[] = '`' . $field . '`'; $return[] = '`' . $field . '`';
} }
} }
return implode(', ', $ret); return implode(', ', $return);
} }
protected function createKeysSqlHelper($fields) { protected function createKeysSqlHelper($fields) {
$ret = array(); $return = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = '`' . $field[0] . '`(' . $field[1] . ')'; $return[] = '`' . $field[0] . '`(' . $field[1] . ')';
} }
else { else {
$ret[] = '`' . $field . '`'; $return[] = '`' . $field . '`';
} }
} }
return implode(', ', $ret); return implode(', ', $return);
} }
public function renameTable(&$ret, $table, $new_name) { public function renameTable($table, $new_name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
} }
public function dropTable(&$ret, $table) { public function dropTable($table) {
$ret[] = update_sql('DROP TABLE {' . $table . '}'); $this->connection->query('DROP TABLE {' . $table . '}');
} }
public function addField(&$ret, $table, $field, $spec, $keys_new = array()) { public function addField($table, $field, $spec, $keys_new = array()) {
$fixnull = FALSE; $fixnull = FALSE;
if (!empty($spec['not null']) && !isset($spec['default'])) { if (!empty($spec['not null']) && !isset($spec['default'])) {
$fixnull = TRUE; $fixnull = TRUE;
@ -287,24 +287,23 @@ class DatabaseSchema_mysql extends DatabaseSchema {
if (count($keys_new)) { if (count($keys_new)) {
$query .= ', ADD ' . implode(', ADD ', $this->createKeysSql($keys_new)); $query .= ', ADD ' . implode(', ADD ', $this->createKeysSql($keys_new));
} }
$ret[] = update_sql($query); $this->connection->query($query);
if (isset($spec['initial'])) { if (isset($spec['initial'])) {
// All this because update_sql does not support %-placeholders. $this->connection->update($table)
$sql = 'UPDATE {' . $table . '} SET ' . $field . ' = :value'; ->fields(array($field, $spec['initial']))
$result = db_query($sql, array(':value' => $spec['initial'])); ->execute();
$ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql . ' (' . $spec['initial'] . ')'));
} }
if ($fixnull) { if ($fixnull) {
$spec['not null'] = TRUE; $spec['not null'] = TRUE;
$this->changeField($ret, $table, $field, $field, $spec); $this->changeField($table, $field, $field, $spec);
} }
} }
public function dropField(&$ret, $table, $field) { public function dropField($table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP `' . $field . '`'); $this->connection->query('ALTER TABLE {' . $table . '} DROP `' . $field . '`');
} }
public function fieldSetDefault(&$ret, $table, $field, $default) { public function fieldSetDefault($table, $field, $default) {
if (is_null($default)) { if (is_null($default)) {
$default = 'NULL'; $default = 'NULL';
} }
@ -312,44 +311,43 @@ class DatabaseSchema_mysql extends DatabaseSchema {
$default = is_string($default) ? "'$default'" : $default; $default = is_string($default) ? "'$default'" : $default;
} }
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default); $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default);
} }
public function fieldSetNoDefault(&$ret, $table, $field) { public function fieldSetNoDefault($table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` DROP DEFAULT'); $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` DROP DEFAULT');
} }
public function addPrimaryKey(&$ret, $table, $fields) { public function addPrimaryKey($table, $fields) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->createKeySql($fields) . ')'); $this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->createKeySql($fields) . ')');
} }
public function dropPrimaryKey(&$ret, $table) { public function dropPrimaryKey($table) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP PRIMARY KEY'); $this->connection->query('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
} }
public function addUniqueKey(&$ret, $table, $name, $fields) { public function addUniqueKey($table, $name, $fields) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')'); $this->connection->query('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')');
} }
public function dropUniqueKey(&$ret, $table, $name) { public function dropUniqueKey($table, $name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`'); $this->connection->query('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`');
} }
public function addIndex(&$ret, $table, $name, $fields) { public function addIndex($table, $name, $fields) {
$query = 'ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($fields) . ')'; $this->connection->query('ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($fields) . ')');
$ret[] = update_sql($query);
} }
public function dropIndex(&$ret, $table, $name) { public function dropIndex($table, $name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`'); $this->connection->query('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`');
} }
public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) { public function changeField($table, $field, $field_new, $spec, $keys_new = array()) {
$sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec)); $sql = 'ALTER TABLE {' . $table . '} CHANGE `' . $field . '` ' . $this->createFieldSql($field_new, $this->processField($spec));
if (count($keys_new)) { if (count($keys_new)) {
$sql .= ', ADD ' . implode(', ADD ', $this->createKeysSql($keys_new)); $sql .= ', ADD ' . implode(', ADD ', $this->createKeysSql($keys_new));
} }
$ret[] = update_sql($sql); $this->connection->query($sql);
} }
public function prepareComment($comment, $length = NULL) { public function prepareComment($comment, $length = NULL) {
@ -374,11 +372,11 @@ class DatabaseSchema_mysql extends DatabaseSchema {
$condition->condition('column_name', $column); $condition->condition('column_name', $column);
$condition->compile($this->connection, $this); $condition->compile($this->connection, $this);
// Don't use {} around information_schema.columns table. // Don't use {} around information_schema.columns table.
return db_query("SELECT column_comment FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField(); return $this->connection->query("SELECT column_comment FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
} }
$condition->compile($this->connection, $this); $condition->compile($this->connection, $this);
// Don't use {} around information_schema.tables table. // Don't use {} around information_schema.tables table.
$comment = db_query("SELECT table_comment FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField(); $comment = $this->connection->query("SELECT table_comment FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
// Work-around for MySQL 5.0 bug http://bugs.mysql.com/bug.php?id=11379 // Work-around for MySQL 5.0 bug http://bugs.mysql.com/bug.php?id=11379
return preg_replace('/; InnoDB free:.*$/', '', $comment); return preg_replace('/; InnoDB free:.*$/', '', $comment);
} }

View File

@ -52,7 +52,11 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
'sequences' => array(), 'sequences' => array(),
); );
// Don't use {} around information_schema.columns table. // Don't use {} around information_schema.columns table.
$result = db_query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = :schema AND table_name = :table AND (data_type = 'bytea' OR (numeric_precision IS NOT NULL AND column_default LIKE :default))", array(':schema' => $schema, ':table' => $table_name, ':default' => '%nextval%')); $result = $this->connection->query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = :schema AND table_name = :table AND (data_type = 'bytea' OR (numeric_precision IS NOT NULL AND column_default LIKE :default))", array(
':schema' => $schema,
':table' => $table_name,
':default' => '%nextval%',
));
foreach ($result as $column) { foreach ($result as $column) {
if ($column->data_type == 'bytea') { if ($column->data_type == 'bytea') {
$table_information->blob_fields[$column->column_name] = TRUE; $table_information->blob_fields[$column->column_name] = TRUE;
@ -258,49 +262,29 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
} }
protected function _createKeySql($fields) { protected function _createKeySql($fields) {
$ret = array(); $return = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')'; $return[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')';
} }
else { else {
$ret[] = '"' . $field . '"'; $return[] = '"' . $field . '"';
} }
} }
return implode(', ', $ret); return implode(', ', $return);
} }
/** function renameTable($table, $new_name) {
* Rename a table. $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
*
* @param $ret
* Array to which query results will be added.
* @param $table
* The table to be renamed.
* @param $new_name
* The new name for the table.
*/
function renameTable(&$ret, $table, $new_name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
} }
/** public function dropTable($table) {
* Drop a table. $this->connection->query('DROP TABLE {' . $table . '}');
*
* @param $ret
* Array to which query results will be added.
* @param $table
* The table to be dropped.
*/
public function dropTable(&$ret, $table) {
$ret[] = update_sql('DROP TABLE {' . $table . '}');
} }
/** /**
* Add a new field to a table. * Add a new field to a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table to be altered. * Name of the table to be altered.
* @param $field * @param $field
@ -321,7 +305,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* *
* @see db_change_field() * @see db_change_field()
*/ */
public function addField(&$ret, $table, $field, $spec, $new_keys = array()) { public function addField($table, $field, $spec, $new_keys = array()) {
$fixnull = FALSE; $fixnull = FALSE;
if (!empty($spec['not null']) && !isset($spec['default'])) { if (!empty($spec['not null']) && !isset($spec['default'])) {
$fixnull = TRUE; $fixnull = TRUE;
@ -329,44 +313,39 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
} }
$query = 'ALTER TABLE {' . $table . '} ADD COLUMN '; $query = 'ALTER TABLE {' . $table . '} ADD COLUMN ';
$query .= $this->createFieldSql($field, $this->processField($spec)); $query .= $this->createFieldSql($field, $this->processField($spec));
$ret[] = update_sql($query); $this->connection->query($query);
if (isset($spec['initial'])) { if (isset($spec['initial'])) {
// All this because update_sql does not support %-placeholders. $this->connection->update($table)
$sql = 'UPDATE {' . $table . '} SET ' . $field . ' = :value'; ->fields(array($field, $spec['initial']))
$result = db_query($sql, array(':value' => $spec['initial'])); ->execute();
$ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql . ' (' . $spec['initial'] . ')'));
} }
if ($fixnull) { if ($fixnull) {
$ret[] = update_sql("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL"); $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL");
} }
if (isset($new_keys)) { if (isset($new_keys)) {
$this->_createKeys($ret, $table, $new_keys); $this->_createKeys($table, $new_keys);
} }
// Add column comment. // Add column comment.
if (!empty($spec['description'])) { if (!empty($spec['description'])) {
$ret[] = update_sql('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description'])); $this->connection->query('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description']));
} }
} }
/** /**
* Drop a field. * Drop a field.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be dropped. * The field to be dropped.
*/ */
public function dropField(&$ret, $table, $field) { public function dropField($table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"'); $this->connection->query('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"');
} }
/** /**
* Set the default value for a field. * Set the default value for a field.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
@ -374,7 +353,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* @param $default * @param $default
* Default value to be set. NULL for 'default NULL'. * Default value to be set. NULL for 'default NULL'.
*/ */
public function fieldSetDefault(&$ret, $table, $field, $default) { public function fieldSetDefault($table, $field, $default) {
if (is_null($default)) { if (is_null($default)) {
$default = 'NULL'; $default = 'NULL';
} }
@ -382,54 +361,46 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
$default = is_string($default) ? "'$default'" : $default; $default = is_string($default) ? "'$default'" : $default;
} }
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" SET DEFAULT ' . $default); $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" SET DEFAULT ' . $default);
} }
/** /**
* Set a field to have no default value. * Set a field to have no default value.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be altered. * The field to be altered.
*/ */
public function fieldSetNoDefault(&$ret, $table, $field) { public function fieldSetNoDefault($table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT'); $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT');
} }
/** /**
* Add a primary key. * Add a primary key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $fields * @param $fields
* Fields for the primary key. * Fields for the primary key.
*/ */
public function addPrimaryKey(&$ret, $table, $fields) { public function addPrimaryKey($table, $fields) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')'); $this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')');
} }
/** /**
* Drop the primary key. * Drop the primary key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
*/ */
public function dropPrimaryKey(&$ret, $table) { public function dropPrimaryKey($table) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey'); $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey');
} }
/** /**
* Add a unique key. * Add a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -437,31 +408,27 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
function addUniqueKey(&$ret, $table, $name, $fields) { function addUniqueKey($table, $name, $fields) {
$name = '{' . $table . '}_' . $name . '_key'; $name = '{' . $table . '}_' . $name . '_key';
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $name . '" UNIQUE (' . implode(',', $fields) . ')'); $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $name . '" UNIQUE (' . implode(',', $fields) . ')');
} }
/** /**
* Drop a unique key. * Drop a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the key. * The name of the key.
*/ */
public function dropUniqueKey(&$ret, $table, $name) { public function dropUniqueKey($table, $name) {
$name = '{' . $table . '}_' . $name . '_key'; $name = '{' . $table . '}_' . $name . '_key';
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $name . '"'); $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $name . '"');
} }
/** /**
* Add an index. * Add an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -469,23 +436,21 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
public function addIndex(&$ret, $table, $name, $fields) { public function addIndex($table, $name, $fields) {
$ret[] = update_sql($this->_createIndexSql($table, $name, $fields)); $this->connection->query($this->_createIndexSql($table, $name, $fields));
} }
/** /**
* Drop an index. * Drop an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the index. * The name of the index.
*/ */
public function dropIndex(&$ret, $table, $name) { public function dropIndex($table, $name) {
$name = '{' . $table . '}_' . $name . '_idx'; $name = '{' . $table . '}_' . $name . '_idx';
$ret[] = update_sql('DROP INDEX ' . $name); $this->connection->query('DROP INDEX ' . $name);
} }
/** /**
@ -511,8 +476,8 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* and you want to change foo.bar to be type serial, leaving it as the * and you want to change foo.bar to be type serial, leaving it as the
* primary key. The correct sequence is: * primary key. The correct sequence is:
* @code * @code
* db_drop_primary_key($ret, 'foo'); * db_drop_primary_key('foo');
* db_change_field($ret, 'foo', 'bar', 'bar', * db_change_field('foo', 'bar', 'bar',
* array('type' => 'serial', 'not null' => TRUE), * array('type' => 'serial', 'not null' => TRUE),
* array('primary key' => array('bar'))); * array('primary key' => array('bar')));
* @endcode * @endcode
@ -535,8 +500,6 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* unless you are converting a field to be type serial. You can use * unless you are converting a field to be type serial. You can use
* the $new_keys argument in all cases. * the $new_keys argument in all cases.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table. * Name of the table.
* @param $field * @param $field
@ -550,15 +513,15 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* table along with changing the field. The format is the same as a * table along with changing the field. The format is the same as a
* table specification but without the 'fields' element. * table specification but without the 'fields' element.
*/ */
public function changeField(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) { public function changeField($table, $field, $field_new, $spec, $new_keys = array()) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field . '_old"'); $this->connection->query('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field . '_old"');
$not_null = isset($spec['not null']) ? $spec['not null'] : FALSE; $not_null = isset($spec['not null']) ? $spec['not null'] : FALSE;
unset($spec['not null']); unset($spec['not null']);
if (!array_key_exists('size', $spec)) { if (!array_key_exists('size', $spec)) {
$spec['size'] = 'normal'; $spec['size'] = 'normal';
} }
$this->addField($ret, $table, "$field_new", $spec); $this->addField($table, "$field_new", $spec);
// We need to typecast the new column to best be able to transfer the data // We need to typecast the new column to best be able to transfer the data
// Schema_pgsql::getFieldTypeMap() will return possibilities that are not // Schema_pgsql::getFieldTypeMap() will return possibilities that are not
@ -568,16 +531,16 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
if (in_array($typecast, array('serial', 'bigserial', 'numeric'))) { if (in_array($typecast, array('serial', 'bigserial', 'numeric'))) {
$typecast = 'int'; $typecast = 'int';
} }
$ret[] = update_sql("UPDATE {" . $table . "} SET $field_new = CAST(" . $field . "_old as " . $typecast . ")"); $this->connection->query("UPDATE {" . $table . "} SET $field_new = CAST(" . $field . "_old as " . $typecast . ")");
if ($not_null) { if ($not_null) {
$ret[] = update_sql("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL"); $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL");
} }
$this->dropField($ret, $table, $field . '_old'); $this->dropField($table, $field . '_old');
if (isset($new_keys)) { if (isset($new_keys)) {
$this->_createKeys($ret, $table, $new_keys); $this->_createKeys($table, $new_keys);
} }
} }
@ -587,18 +550,18 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
return $query; return $query;
} }
protected function _createKeys(&$ret, $table, $new_keys) { protected function _createKeys($table, $new_keys) {
if (isset($new_keys['primary key'])) { if (isset($new_keys['primary key'])) {
$this->addPrimaryKey($ret, $table, $new_keys['primary key']); $this->addPrimaryKey($table, $new_keys['primary key']);
} }
if (isset($new_keys['unique keys'])) { if (isset($new_keys['unique keys'])) {
foreach ($new_keys['unique keys'] as $name => $fields) { foreach ($new_keys['unique keys'] as $name => $fields) {
$this->addUniqueKey($ret, $table, $name, $fields); $this->addUniqueKey($table, $name, $fields);
} }
} }
if (isset($new_keys['indexes'])) { if (isset($new_keys['indexes'])) {
foreach ($new_keys['indexes'] as $name => $fields) { foreach ($new_keys['indexes'] as $name => $fields) {
$this->addIndex($ret, $table, $name, $fields); $this->addIndex($table, $name, $fields);
} }
} }
} }
@ -610,8 +573,8 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
$table = $this->connection->prefixTables('{' . $table . '}'); $table = $this->connection->prefixTables('{' . $table . '}');
// Don't use {} around pg_class, pg_attribute tables. // Don't use {} around pg_class, pg_attribute tables.
if (isset($column)) { if (isset($column)) {
return db_query('SELECT col_description(oid, attnum) FROM pg_class, pg_attribute WHERE attrelid = oid AND relname = ? AND attname = ?', array($table, $column))->fetchField(); $this->connection->query('SELECT col_description(oid, attnum) FROM pg_class, pg_attribute WHERE attrelid = oid AND relname = ? AND attname = ?', array($table, $column))->fetchField();
} }
return db_query('SELECT obj_description(oid, ?) FROM pg_class WHERE relname = ?', array('pg_class', $table))->fetchField(); $this->connection->query('SELECT obj_description(oid, ?) FROM pg_class WHERE relname = ?', array('pg_class', $table))->fetchField();
} }
} }

View File

@ -267,30 +267,24 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
/** /**
* Rename a table. * Rename a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be renamed. * The table to be renamed.
* @param $new_name * @param $new_name
* The new name for the table. * The new name for the table.
*/ */
abstract public function renameTable(&$ret, $table, $new_name); abstract public function renameTable($table, $new_name);
/** /**
* Drop a table. * Drop a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be dropped. * The table to be dropped.
*/ */
abstract public function dropTable(&$ret, $table); abstract public function dropTable($table);
/** /**
* Add a new field to a table. * Add a new field to a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table to be altered. * Name of the table to be altered.
* @param $field * @param $field
@ -309,25 +303,21 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* or index including it in this array. @see db_change_field for more * or index including it in this array. @see db_change_field for more
* explanation why. * explanation why.
*/ */
abstract public function addField(&$ret, $table, $field, $spec, $keys_new = array()); abstract public function addField($table, $field, $spec, $keys_new = array());
/** /**
* Drop a field. * Drop a field.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be dropped. * The field to be dropped.
*/ */
abstract public function dropField(&$ret, $table, $field); abstract public function dropField($table, $field);
/** /**
* Set the default value for a field. * Set the default value for a field.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
@ -335,47 +325,39 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* @param $default * @param $default
* Default value to be set. NULL for 'default NULL'. * Default value to be set. NULL for 'default NULL'.
*/ */
abstract public function fieldSetDefault(&$ret, $table, $field, $default); abstract public function fieldSetDefault($table, $field, $default);
/** /**
* Set a field to have no default value. * Set a field to have no default value.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be altered. * The field to be altered.
*/ */
abstract public function fieldSetNoDefault(&$ret, $table, $field); abstract public function fieldSetNoDefault($table, $field);
/** /**
* Add a primary key. * Add a primary key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $fields * @param $fields
* Fields for the primary key. * Fields for the primary key.
*/ */
abstract public function addPrimaryKey(&$ret, $table, $fields); abstract public function addPrimaryKey($table, $fields);
/** /**
* Drop the primary key. * Drop the primary key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
*/ */
abstract public function dropPrimaryKey(&$ret, $table); abstract public function dropPrimaryKey($table);
/** /**
* Add a unique key. * Add a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -383,25 +365,21 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
abstract public function addUniqueKey(&$ret, $table, $name, $fields); abstract public function addUniqueKey($table, $name, $fields);
/** /**
* Drop a unique key. * Drop a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the key. * The name of the key.
*/ */
abstract public function dropUniqueKey(&$ret, $table, $name); abstract public function dropUniqueKey($table, $name);
/** /**
* Add an index. * Add an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -409,20 +387,17 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
abstract public function addIndex(&$ret, $table, $name, $fields); abstract public function addIndex($table, $name, $fields);
/** /**
* Drop an index. * Drop an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the index. * The name of the index.
*/ */
abstract public function dropIndex(&$ret, $table, $name); abstract public function dropIndex($table, $name);
/** /**
* Change a field definition. * Change a field definition.
@ -447,8 +422,8 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* and you want to change foo.bar to be type serial, leaving it as the * and you want to change foo.bar to be type serial, leaving it as the
* primary key. The correct sequence is: * primary key. The correct sequence is:
* @code * @code
* db_drop_primary_key($ret, 'foo'); * db_drop_primary_key('foo');
* db_change_field($ret, 'foo', 'bar', 'bar', * db_change_field('foo', 'bar', 'bar',
* array('type' => 'serial', 'not null' => TRUE), * array('type' => 'serial', 'not null' => TRUE),
* array('primary key' => array('bar'))); * array('primary key' => array('bar')));
* @endcode * @endcode
@ -471,8 +446,6 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* unless you are converting a field to be type serial. You can use * unless you are converting a field to be type serial. You can use
* the $keys_new argument in all cases. * the $keys_new argument in all cases.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table. * Name of the table.
* @param $field * @param $field
@ -486,22 +459,20 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* table along with changing the field. The format is the same as a * table along with changing the field. The format is the same as a
* table specification but without the 'fields' element. * table specification but without the 'fields' element.
*/ */
abstract public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()); abstract public function changeField($table, $field, $field_new, $spec, $keys_new = array());
/** /**
* Create a new table from a Drupal table definition. * Create a new table from a Drupal table definition.
* *
* @param $ret
* Array to which query results will be added.
* @param $name * @param $name
* The name of the table to create. * The name of the table to create.
* @param $table * @param $table
* A Schema API table definition array. * A Schema API table definition array.
*/ */
public function createTable(&$ret, $name, $table) { public function createTable($name, $table) {
$statements = $this->createTableSql($name, $table); $statements = $this->createTableSql($name, $table);
foreach ($statements as $statement) { foreach ($statements as $statement) {
$ret[] = update_sql($statement); $this->connection->query($statement);
} }
} }
@ -517,16 +488,16 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* An array of field names. * An array of field names.
*/ */
public function fieldNames($fields) { public function fieldNames($fields) {
$ret = array(); $return = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = $field[0]; $return[] = $field[0];
} }
else { else {
$ret[] = $field; $return[] = $field;
} }
} }
return $ret; return $return;
} }
/** /**

View File

@ -86,16 +86,16 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* Build the SQL expression for keys. * Build the SQL expression for keys.
*/ */
protected function createKeySql($fields) { protected function createKeySql($fields) {
$ret = array(); $return = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = $field[0]; $return[] = $field[0];
} }
else { else {
$ret[] = $field; $return[] = $field;
} }
} }
return implode(', ', $ret); return implode(', ', $return);
} }
/** /**
@ -214,34 +214,28 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
/** /**
* Rename a table. * Rename a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be renamed. * The table to be renamed.
* @param $new_name * @param $new_name
* The new name for the table. * The new name for the table.
*/ */
public function renameTable(&$ret, $table, $new_name) { public function renameTable($table, $new_name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
} }
/** /**
* Drop a table. * Drop a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be dropped. * The table to be dropped.
*/ */
public function dropTable(&$ret, $table) { public function dropTable($table) {
$ret[] = update_sql('DROP TABLE {' . $table . '}'); $this->connection->query('DROP TABLE {' . $table . '}');
} }
/** /**
* Add a new field to a table. * Add a new field to a table.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table to be altered. * Name of the table to be altered.
* @param $field * @param $field
@ -249,11 +243,11 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* @param $spec * @param $spec
* The field specification array, as taken from a schema definition. * The field specification array, as taken from a schema definition.
*/ */
public function addField(&$ret, $table, $field, $spec, $keys_new = array()) { public function addField($table, $field, $spec, $keys_new = array()) {
// TODO: $keys_new is not supported yet. // TODO: $keys_new is not supported yet.
$query = 'ALTER TABLE {' . $table . '} ADD '; $query = 'ALTER TABLE {' . $table . '} ADD ';
$query .= $this->createFieldSql($field, $this->processField($spec)); $query .= $this->createFieldSql($field, $this->processField($spec));
$ret[] = update_sql($query); $this->connection->query($query);
} }
/** /**
@ -262,30 +256,32 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* As SQLite does not support ALTER TABLE (with a few exceptions) it is * As SQLite does not support ALTER TABLE (with a few exceptions) it is
* necessary to create a new table and copy over the old content. * necessary to create a new table and copy over the old content.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table to be altered. * Name of the table to be altered.
* @param $new_schema * @param $new_schema
* The new schema array for the table. * The new schema array for the table.
*/ */
protected function alterTable(&$ret, $table, $new_schema) { protected function alterTable($table, $new_schema) {
$i = 0; $i = 0;
do { do {
$new_table = $table . '_' . $i++; $new_table = $table . '_' . $i++;
} while ($this->tableExists($new_table)); } while ($this->tableExists($new_table));
$this->createTable($ret, $new_table, $new_schema);
$fields = implode(', ', array_keys($new_schema['fields'])); $this->createTable($new_table, $new_schema);
$ret[] = update_sql('INSERT INTO {' . $new_table . "} ($fields) SELECT $fields FROM {" . $table . '}');
$old_count = db_query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField(); $select = $this->connection->select($table)->fields($new_schema['fields']);
$new_count = db_query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField(); $this->connection->insert($new_table)
->from($select)
->execute();
$old_count = $this->connection->query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField();
$new_count = $this->connection->query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField();
if ($old_count == $new_count) { if ($old_count == $new_count) {
do { do {
$temp_table = $table . '_' . $i++; $temp_table = $table . '_' . $i++;
} while ($this->tableExists($temp_table)); } while ($this->tableExists($temp_table));
$this->renameTable($ret, $table, $temp_table); $this->renameTable($table, $temp_table);
$this->renameTable($ret, $new_table, $table); $this->renameTable($new_table, $table);
$this->dropTable($ret, $temp_table); $this->dropTable($temp_table);
} }
} }
@ -305,7 +301,8 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
protected function introspectSchema($table) { protected function introspectSchema($table) {
$mapped_fields = array_flip($this->getFieldTypeMap()); $mapped_fields = array_flip($this->getFieldTypeMap());
$schema = array(); $schema = array();
foreach (db_query("PRAGMA table_info('{" . $table . "}')") as $row) { $result = $this->connection->query("PRAGMA table_info('{" . $table . "}')");
foreach ($result as $row) {
if (preg_match('/^([^(]+)\((.*)\)$/', $row->type, $matches)) { if (preg_match('/^([^(]+)\((.*)\)$/', $row->type, $matches)) {
$type = $matches[1]; $type = $matches[1];
$length = $matches[2]; $length = $matches[2];
@ -334,7 +331,8 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
} }
} }
$indexes = array(); $indexes = array();
foreach (db_query("PRAGMA index_list('{" . $table . "}')") as $row) { $result = $this->connection->query("PRAGMA index_list('{" . $table . "}')");
foreach ($result as $row) {
if (strpos($row->name, 'sqlite_autoindex_') !== 0) { if (strpos($row->name, 'sqlite_autoindex_') !== 0) {
$indexes[] = array( $indexes[] = array(
'schema_key' => $row->unique ? 'unique keys' : 'indexes', 'schema_key' => $row->unique ? 'unique keys' : 'indexes',
@ -346,7 +344,8 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
foreach ($indexes as $index) { foreach ($indexes as $index) {
$name = $index['name']; $name = $index['name'];
$index_name = substr($name, $n); $index_name = substr($name, $n);
foreach (db_query("PRAGMA index_info('$name')") as $row) { $result = $this->connection->query("PRAGMA index_info('$name')");
foreach ($result as $row) {
$schema[$index['schema_key']][$index_name][] = $row->name; $schema[$index['schema_key']][$index_name][] = $row->name;
} }
} }
@ -359,14 +358,12 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* This implementation can't use ALTER TABLE directly, because SQLite only * This implementation can't use ALTER TABLE directly, because SQLite only
* supports a limited subset of that command. * supports a limited subset of that command.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be dropped. * The field to be dropped.
*/ */
public function dropField(&$ret, $table, $field) { public function dropField($table, $field) {
$new_schema = $this->introspectSchema($table); $new_schema = $this->introspectSchema($table);
unset($new_schema['fields'][$field]); unset($new_schema['fields'][$field]);
foreach ($new_schema['indexes'] as $index => $fields) { foreach ($new_schema['indexes'] as $index => $fields) {
@ -380,7 +377,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
unset($new_schema['indexes'][$index]); unset($new_schema['indexes'][$index]);
} }
} }
$this->alterTable($ret, $table, $new_schema); $this->alterTable($table, $new_schema);
} }
/** /**
@ -389,8 +386,6 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* This implementation can't use ALTER TABLE directly, because SQLite only * This implementation can't use ALTER TABLE directly, because SQLite only
* supports a limited subset of that command. * supports a limited subset of that command.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* Name of the table. * Name of the table.
* @param $field * @param $field
@ -404,7 +399,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* table along with changing the field. The format is the same as a * table along with changing the field. The format is the same as a
* table specification but without the 'fields' element. * table specification but without the 'fields' element.
*/ */
public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) { public function changeField($table, $field, $field_new, $spec, $keys_new = array()) {
$new_schema = $this->introspectSchema($table); $new_schema = $this->introspectSchema($table);
unset($new_schema['fields'][$field]); unset($new_schema['fields'][$field]);
$new_schema['fields'][$field_new] = $spec; $new_schema['fields'][$field_new] = $spec;
@ -417,14 +412,12 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
$new_schema[$k] = $keys_new[$k] + $new_schema[$k]; $new_schema[$k] = $keys_new[$k] + $new_schema[$k];
} }
} }
$this->alterTable($ret, $table, $new_schema); $this->alterTable($table, $new_schema);
} }
/** /**
* Add an index. * Add an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -432,33 +425,29 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
public function addIndex(&$ret, $table, $name, $fields) { public function addIndex($table, $name, $fields) {
$schema['indexes'][$name] = $fields; $schema['indexes'][$name] = $fields;
$statements = $this->createIndexSql($table, $schema); $statements = $this->createIndexSql($table, $schema);
foreach ($statements as $statement) { foreach ($statements as $statement) {
$ret[] = update_sql($statement); $this->connection->query($statement);
} }
} }
/** /**
* Drop an index. * Drop an index.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the index. * The name of the index.
*/ */
public function dropIndex(&$ret, $table, $name) { public function dropIndex($table, $name) {
$ret[] = update_sql('DROP INDEX ' . '{' . $table . '}_' . $name); $this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
} }
/** /**
* Add a unique key. * Add a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
@ -466,26 +455,24 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* @param $fields * @param $fields
* An array of field names. * An array of field names.
*/ */
public function addUniqueKey(&$ret, $table, $name, $fields) { public function addUniqueKey($table, $name, $fields) {
$schema['unique keys'][$name] = $fields; $schema['unique keys'][$name] = $fields;
$statements = $this->createIndexSql($table, $schema); $statements = $this->createIndexSql($table, $schema);
foreach ($statements as $statement) { foreach ($statements as $statement) {
$ret[] = update_sql($statement); $this->connection->query($statement);
} }
} }
/** /**
* Drop a unique key. * Drop a unique key.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $name * @param $name
* The name of the key. * The name of the key.
*/ */
public function dropUniqueKey(&$ret, $table, $name) { public function dropUniqueKey($table, $name) {
$ret[] = update_sql('DROP INDEX ' . '{' . $table . '}_' . $name); $this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
} }
/** /**
@ -494,17 +481,15 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* This implementation can't use ALTER TABLE directly, because SQLite only * This implementation can't use ALTER TABLE directly, because SQLite only
* supports a limited subset of that command. * supports a limited subset of that command.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $fields * @param $fields
* Fields for the primary key. * Fields for the primary key.
*/ */
public function addPrimaryKey(&$ret, $table, $fields) { public function addPrimaryKey($table, $fields) {
$new_schema = $this->introspectSchema($table); $new_schema = $this->introspectSchema($table);
$new_schema['primary key'] = $fields; $new_schema['primary key'] = $fields;
$this->alterTable($ret, $table, $new_schema); $this->alterTable($table, $new_schema);
} }
/** /**
@ -513,15 +498,13 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* This implementation can't use ALTER TABLE directly, because SQLite only * This implementation can't use ALTER TABLE directly, because SQLite only
* supports a limited subset of that command.` * supports a limited subset of that command.`
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
*/ */
public function dropPrimaryKey(&$ret, $table) { public function dropPrimaryKey($table) {
$new_schema = $this->introspectSchema($table); $new_schema = $this->introspectSchema($table);
unset($new_schema['primary key']); unset($new_schema['primary key']);
$this->alterTable($ret, $table, $new_schema); $this->alterTable($table, $new_schema);
} }
/** /**
@ -530,8 +513,6 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* This implementation can't use ALTER TABLE directly, because SQLite only * This implementation can't use ALTER TABLE directly, because SQLite only
* supports a limited subset of that command. * supports a limited subset of that command.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
@ -539,10 +520,10 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* @param $default * @param $default
* Default value to be set. NULL for 'default NULL'. * Default value to be set. NULL for 'default NULL'.
*/ */
public function fieldSetDefault(&$ret, $table, $field, $default) { public function fieldSetDefault($table, $field, $default) {
$new_schema = $this->introspectSchema($table); $new_schema = $this->introspectSchema($table);
$new_schema['fields'][$field]['default'] = $default; $new_schema['fields'][$field]['default'] = $default;
$this->alterTable($ret, $table, $new_schema); $this->alterTable($table, $new_schema);
} }
/** /**
@ -551,17 +532,15 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* This implementation can't use ALTER TABLE directly, because SQLite only * This implementation can't use ALTER TABLE directly, because SQLite only
* supports a limited subset of that command. * supports a limited subset of that command.
* *
* @param $ret
* Array to which query results will be added.
* @param $table * @param $table
* The table to be altered. * The table to be altered.
* @param $field * @param $field
* The field to be altered. * The field to be altered.
*/ */
public function fieldSetNoDefault(&$ret, $table, $field) { public function fieldSetNoDefault($table, $field) {
$new_schema = $this->introspectSchema($table); $new_schema = $this->introspectSchema($table);
unset($new_schema['fields'][$field]['default']); unset($new_schema['fields'][$field]['default']);
$this->alterTable($ret, $table, $new_schema); $this->alterTable($table, $new_schema);
} }
/** /**

View File

@ -765,7 +765,7 @@ function locale_translate_import_form_submit($form, &$form_state) {
} }
// Now import strings into the language // Now import strings into the language
if ($ret = _locale_import_po($file, $langcode, $form_state['values']['mode'], $form_state['values']['group']) == FALSE) { if ($return = _locale_import_po($file, $langcode, $form_state['values']['mode'], $form_state['values']['group']) == FALSE) {
$variables = array('%filename' => $file->filename); $variables = array('%filename' => $file->filename);
drupal_set_message(t('The translation import of %filename failed.', $variables), 'error'); drupal_set_message(t('The translation import of %filename failed.', $variables), 'error');
watchdog('locale', 'The translation import of %filename failed.', $variables, WATCHDOG_ERROR); watchdog('locale', 'The translation import of %filename failed.', $variables, WATCHDOG_ERROR);
@ -2175,7 +2175,7 @@ function _locale_export_string($str) {
*/ */
function _locale_export_wrap($str, $len) { function _locale_export_wrap($str, $len) {
$words = explode(' ', $str); $words = explode(' ', $str);
$ret = array(); $return = array();
$cur = ""; $cur = "";
$nstr = 1; $nstr = 1;
@ -2186,16 +2186,16 @@ function _locale_export_wrap($str, $len) {
$nstr = 0; $nstr = 0;
} }
elseif (strlen("$cur $word") > $len) { elseif (strlen("$cur $word") > $len) {
$ret[] = $cur . " "; $return[] = $cur . " ";
$cur = $word; $cur = $word;
} }
else { else {
$cur = "$cur $word"; $cur = "$cur $word";
} }
} }
$ret[] = $cur; $return[] = $cur;
return implode("\n", $ret); return implode("\n", $return);
} }
/** /**

View File

@ -14,7 +14,6 @@
* current version of Drupal core. * current version of Drupal core.
*/ */
function update_fix_compatibility() { function update_fix_compatibility() {
$ret = array();
$incompatible = array(); $incompatible = array();
$result = db_query("SELECT name, type, status FROM {system} WHERE status = 1 AND type IN ('module','theme')"); $result = db_query("SELECT name, type, status FROM {system} WHERE status = 1 AND type IN ('module','theme')");
foreach ($result as $row) { foreach ($result as $row) {
@ -23,9 +22,11 @@ function update_fix_compatibility() {
} }
} }
if (!empty($incompatible)) { if (!empty($incompatible)) {
$ret[] = update_sql("UPDATE {system} SET status = 0 WHERE name IN ('" . implode("','", $incompatible) . "')"); db_update('system')
->fields(array('status' => 0))
->condition('name', $incompatible, 'IN')
->execute();
} }
return $ret;
} }
/** /**
@ -113,7 +114,6 @@ function update_prepare_d7_bootstrap() {
*/ */
function update_fix_d7_requirements() { function update_fix_d7_requirements() {
global $conf; global $conf;
$ret = array();
// Rewrite the settings.php file if necessary. // Rewrite the settings.php file if necessary.
// @see update_prepare_d7_bootstrap(). // @see update_prepare_d7_bootstrap().
@ -126,11 +126,11 @@ function update_fix_d7_requirements() {
// Add the cache_path table. // Add the cache_path table.
$schema['cache_path'] = drupal_get_schema_unprocessed('system', 'cache'); $schema['cache_path'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_path']['description'] = 'Cache table used for path alias lookups.'; $schema['cache_path']['description'] = 'Cache table used for path alias lookups.';
db_create_table($ret, 'cache_path', $schema['cache_path']); db_create_table('cache_path', $schema['cache_path']);
// Add column for locale context. // Add column for locale context.
if (db_table_exists('locales_source')) { if (db_table_exists('locales_source')) {
db_add_field($ret, 'locales_source', 'context', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', 'description' => 'The context this string applies to.')); db_add_field('locales_source', 'context', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', 'description' => 'The context this string applies to.'));
} }
// Rename 'site_offline_message' variable to 'maintenance_mode_message'. // Rename 'site_offline_message' variable to 'maintenance_mode_message'.
@ -144,8 +144,6 @@ function update_fix_d7_requirements() {
} }
update_fix_d7_install_profile(); update_fix_d7_install_profile();
return $ret;
} }
/** /**
@ -161,7 +159,6 @@ function update_fix_d7_requirements() {
function update_fix_d7_install_profile() { function update_fix_d7_install_profile() {
$profile = drupal_get_profile(); $profile = drupal_get_profile();
$results = db_select('system', 's') $results = db_select('system', 's')
->fields('s', array('name', 'schema_version')) ->fields('s', array('name', 'schema_version'))
->condition('name', $profile) ->condition('name', $profile)

View File

@ -266,10 +266,9 @@ function aggregator_schema() {
* Add hash column to aggregator_feed table. * Add hash column to aggregator_feed table.
*/ */
function aggregator_update_7000() { function aggregator_update_7000() {
$ret = array(); db_add_field('aggregator_feed', 'hash', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''));
db_add_field($ret, 'aggregator_feed', 'hash', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''));
return $ret;
} }
/** /**
* Add aggregator teaser length to settings from old global default teaser length * Add aggregator teaser length to settings from old global default teaser length
*/ */

View File

@ -223,9 +223,10 @@ function block_install() {
* so before block module runs, there will not be much to alter. * so before block module runs, there will not be much to alter.
*/ */
function block_update_7000() { function block_update_7000() {
$ret = array(); db_update('system')
$ret[] = update_sql("UPDATE {system} SET weight = -5 WHERE name = 'block'"); ->fields(array('weight', '-5'))
return $ret; ->condition('name', 'block')
->execute();
} }
@ -233,8 +234,6 @@ function block_update_7000() {
* Add the block_node_type table. * Add the block_node_type table.
*/ */
function block_update_7001() { function block_update_7001() {
$ret = array();
$schema['block_node_type'] = array( $schema['block_node_type'] = array(
'description' => 'Sets up display criteria for blocks based on content types', 'description' => 'Sets up display criteria for blocks based on content types',
'fields' => array( 'fields' => array(
@ -263,6 +262,5 @@ function block_update_7001() {
), ),
); );
db_create_table($ret, 'block_node_type', $schema['block_node_type']); db_create_table('block_node_type', $schema['block_node_type']);
return $ret;
} }

View File

@ -58,40 +58,42 @@ function comment_update_7000() {
foreach ($types as $type => $object) { foreach ($types as $type => $object) {
variable_del('comment_default_order' . $type); variable_del('comment_default_order' . $type);
} }
return array(array('success' => TRUE, 'query' => 'Comment order settings removed.')); return t('Comment order settings removed.');
} }
/** /**
* Change comment status from published being 0 to being 1 * Change comment status from published being 0 to being 1
*/ */
function comment_update_7001() { function comment_update_7001() {
$ret = array(); $changes = array(
$ret[] = update_sql("UPDATE {comments} SET status = 3 WHERE status = 0"); 3 => 0,
$ret[] = update_sql("UPDATE {comments} SET status = 0 WHERE status = 1"); 0 => 1,
$ret[] = update_sql("UPDATE {comments} SET status = 1 WHERE status = 3"); 1 => 3,
);
return $ret; foreach ($changes as $old => $new) {
db_update('comments')
->fields(array('status', $new))
->condition('status', $old)
->execute();
}
} }
/** /**
* Rename {comments} table to {comment}. * Rename {comments} table to {comment}.
*/ */
function comment_update_7002() { function comment_update_7002() {
$ret = array(); db_rename_table('comments', 'comment');
db_rename_table($ret, 'comments', 'comment');
return $ret;
} }
/** /**
* Improve indexes on the comment table. * Improve indexes on the comment table.
*/ */
function comment_update_7003() { function comment_update_7003() {
$ret = array(); db_drop_index('comment', 'status');
db_drop_index($ret, 'comment', 'status'); db_drop_index('comment', 'pid');
db_drop_index($ret, 'comment', 'pid'); db_add_index('comment', 'comment_pid_status', array('pid', 'status'));
db_add_index($ret, 'comment', 'comment_pid_status', array('pid', 'status')); db_add_index('comment', 'comment_num_new', array('nid', 'timestamp', 'status'));
db_add_index($ret, 'comment', 'comment_num_new', array('nid', 'timestamp', 'status'));
return $ret;
} }
/** /**
@ -108,29 +110,23 @@ function comment_update_7004() {
variable_set('comment_default_mode_' . $type, 0); variable_set('comment_default_mode_' . $type, 0);
} }
} }
return array();
} }
/** /**
* Create comment Field API bundles. * Create comment Field API bundles.
*/ */
function comment_update_7005() { function comment_update_7005() {
$ret = array();
foreach (node_type_get_types() as $info) { foreach (node_type_get_types() as $info) {
field_attach_create_bundle('comment_node_' . $info->type); field_attach_create_bundle('comment_node_' . $info->type);
} }
return $ret;
} }
/** /**
* Create user related indexes. * Create user related indexes.
*/ */
function comment_update_7006() { function comment_update_7006() {
$ret = array(); db_add_index('comment', 'comment_uid', array('uid'));
db_add_index($ret, 'comment', 'comment_uid', array('uid')); db_add_index('node_comment_statistics', 'last_comment_uid', array('last_comment_uid'));
db_add_index($ret, 'node_comment_statistics', 'last_comment_uid', array('last_comment_uid'));
return $ret;
} }
/** /**

View File

@ -85,7 +85,6 @@ function contact_schema() {
function contact_update_7000() { function contact_update_7000() {
variable_set('contact_threshold_limit', variable_get('contact_hourly_threshold', 5)); variable_set('contact_threshold_limit', variable_get('contact_hourly_threshold', 5));
variable_del('contact_hourly_threshold'); variable_del('contact_hourly_threshold');
return array();
} }
/** /**

View File

@ -96,26 +96,20 @@ function dblog_schema() {
* Allow NULL values for links and longer referers. * Allow NULL values for links and longer referers.
*/ */
function dblog_update_7001() { function dblog_update_7001() {
$ret = array(); db_change_field('watchdog', 'link', 'link', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => ''));
db_change_field($ret, 'watchdog', 'link', 'link', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'default' => '')); db_change_field('watchdog', 'referer', 'referer', array('type' => 'text', 'not null' => FALSE));
db_change_field($ret, 'watchdog', 'referer', 'referer', array('type' => 'text', 'not null' => FALSE));
return $ret;
} }
/** /**
* Add index on uid. * Add index on uid.
*/ */
function dblog_update_7002() { function dblog_update_7002() {
$ret = array(); db_add_index('watchdog', 'uid', array('uid'));
db_add_index($ret, 'watchdog', 'uid', array('uid'));
return $ret;
} }
/** /**
* Allow longer type values. * Allow longer type values.
*/ */
function dblog_update_7003() { function dblog_update_7003() {
$ret = array(); db_change_field('watchdog', 'type', 'type', array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''));
db_change_field($ret, 'watchdog', 'type', 'type', array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''));
return $ret;
} }

View File

@ -211,7 +211,7 @@ function _field_sql_storage_schema($field) {
function field_sql_storage_field_storage_create_field($field) { function field_sql_storage_field_storage_create_field($field) {
$schema = _field_sql_storage_schema($field); $schema = _field_sql_storage_schema($field);
foreach ($schema as $name => $table) { foreach ($schema as $name => $table) {
db_create_table($ret, $name, $table); db_create_table($name, $table);
} }
} }
@ -231,17 +231,15 @@ function field_sql_storage_field_update_forbid($field, $prior_field, $has_data)
* Implement hook_field_storage_update_field(). * Implement hook_field_storage_update_field().
*/ */
function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) { function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) {
$ret = array();
if (! $has_data) { if (! $has_data) {
// There is no data. Re-create the tables completely. // There is no data. Re-create the tables completely.
$prior_schema = _field_sql_storage_schema($prior_field); $prior_schema = _field_sql_storage_schema($prior_field);
foreach ($prior_schema as $name => $table) { foreach ($prior_schema as $name => $table) {
db_drop_table($ret, $name, $table); db_drop_table($name, $table);
} }
$schema = _field_sql_storage_schema($field); $schema = _field_sql_storage_schema($field);
foreach ($schema as $name => $table) { foreach ($schema as $name => $table) {
db_create_table($ret, $name, $table); db_create_table($name, $table);
} }
} }
else { else {
@ -253,8 +251,8 @@ function field_sql_storage_field_storage_update_field($field, $prior_field, $has
foreach ($prior_field['indexes'] as $name => $columns) { foreach ($prior_field['indexes'] as $name => $columns) {
if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) { if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) {
$real_name = _field_sql_storage_indexname($field['field_name'], $name); $real_name = _field_sql_storage_indexname($field['field_name'], $name);
db_drop_index($ret, $table, $real_name); db_drop_index($table, $real_name);
db_drop_index($ret, $revision_table, $real_name); db_drop_index($revision_table, $real_name);
} }
} }
$table = _field_sql_storage_tablename($field); $table = _field_sql_storage_tablename($field);
@ -266,8 +264,8 @@ function field_sql_storage_field_storage_update_field($field, $prior_field, $has
foreach ($columns as $column_name) { foreach ($columns as $column_name) {
$real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name); $real_columns[] = _field_sql_storage_columnname($field['field_name'], $column_name);
} }
db_add_index($ret, $table, $real_name, $real_columns); db_add_index($table, $real_name, $real_columns);
db_add_index($ret, $revision_table, $real_name, $real_columns); db_add_index($revision_table, $real_name, $real_columns);
} }
} }
} }
@ -286,12 +284,11 @@ function field_sql_storage_field_storage_delete_field($field) {
->execute(); ->execute();
// Move the table to a unique name while the table contents are being deleted. // Move the table to a unique name while the table contents are being deleted.
$ret = array();
$field['deleted'] = 1; $field['deleted'] = 1;
$new_table = _field_sql_storage_tablename($field); $new_table = _field_sql_storage_tablename($field);
$revision_new_table = _field_sql_storage_revision_tablename($field); $revision_new_table = _field_sql_storage_revision_tablename($field);
db_rename_table($ret, $table, $new_table); db_rename_table($table, $new_table);
db_rename_table($ret, $revision_table, $revision_new_table); db_rename_table($revision_table, $revision_new_table);
drupal_get_schema(NULL, TRUE); drupal_get_schema(NULL, TRUE);
} }
@ -655,10 +652,9 @@ function field_sql_storage_field_attach_rename_bundle($bundle_old, $bundle_new)
* that is left is to delete the table. * that is left is to delete the table.
*/ */
function field_sql_storage_field_storage_purge_field($field) { function field_sql_storage_field_storage_purge_field($field) {
$ret = array();
$table_name = _field_sql_storage_tablename($field); $table_name = _field_sql_storage_tablename($field);
$revision_name = _field_sql_storage_revision_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field);
db_drop_table($ret, $table_name); db_drop_table($table_name);
db_drop_table($ret, $revision_name); db_drop_table($revision_name);
} }

View File

@ -113,42 +113,44 @@ function filter_schema() {
* Add a weight column to the filter formats table. * Add a weight column to the filter formats table.
*/ */
function filter_update_7000() { function filter_update_7000() {
$ret = array(); db_add_field('filter_formats', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
db_add_field($ret, 'filter_formats', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
return $ret;
} }
/** /**
* Break out "escape HTML filter" option to its own filter. * Break out "escape HTML filter" option to its own filter.
*/ */
function filter_update_7001() { function filter_update_7001() {
$ret = array();
$result = db_query("SELECT format FROM {filter_formats}"); $result = db_query("SELECT format FROM {filter_formats}");
$insert = db_insert('filters')->fields(array('format', 'module', 'delta', 'weight'));
foreach ($result as $format) { foreach ($result as $format) {
// Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2. // Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2.
if (variable_get('filter_html_' . $format->format, 1) == 2) { if (variable_get('filter_html_' . $format->format, 1) == 2) {
$ret[] = update_sql("INSERT INTO {filters} (format, module, delta, weight) VALUES (" . $format->format . ", 'filter', 4, 0)"); $insert->values(array(
'format' => $format->format,
'filter' => 'filter',
'delta' => 4,
'weight' => 0,
));
} }
variable_del('filter_html_' . $format->format); variable_del('filter_html_' . $format->format);
} }
return $ret;
$insert->execute();
} }
/** /**
* Rename {filters} table to {filter} and {filter_formats} table to {filter_format}. * Rename {filters} table to {filter} and {filter_formats} table to {filter_format}.
*/ */
function filter_update_7002() { function filter_update_7002() {
$ret = array(); db_rename_table('filters', 'filter');
db_rename_table($ret, 'filters', 'filter'); db_rename_table('filter_formats', 'filter_format');
db_rename_table($ret, 'filter_formats', 'filter_format');
return $ret;
} }
/** /**
* Remove hardcoded numeric deltas from all filters in core. * Remove hardcoded numeric deltas from all filters in core.
*/ */
function filter_update_7003() { function filter_update_7003() {
$ret = array();
// Get an array of the renamed filter deltas, organized by module. // Get an array of the renamed filter deltas, organized by module.
$renamed_deltas = array( $renamed_deltas = array(
'filter' => array( 'filter' => array(
@ -164,9 +166,9 @@ function filter_update_7003() {
); );
// Rename field 'delta' to 'name'. // Rename field 'delta' to 'name'.
db_drop_unique_key($ret, 'filter', 'fmd'); db_drop_unique_key('filter', 'fmd');
db_drop_index($ret, 'filter', 'list'); db_drop_index('filter', 'list');
db_change_field($ret, 'filter', 'delta', 'name', db_change_field('filter', 'delta', 'name',
array( array(
'type' => 'varchar', 'type' => 'varchar',
'length' => 32, 'length' => 32,
@ -187,10 +189,13 @@ function filter_update_7003() {
// Loop through each filter and make changes to the core filter table. // Loop through each filter and make changes to the core filter table.
foreach ($renamed_deltas as $module => $deltas) { foreach ($renamed_deltas as $module => $deltas) {
foreach ($deltas as $old_delta => $new_delta) { foreach ($deltas as $old_delta => $new_delta) {
$ret[] = update_sql("UPDATE {filter} SET name = '" . $new_delta . "' WHERE module = '" . $module . "' AND name = '" . $old_delta . "'"); db_update('filter')
->fields(array('name', $new_delta))
->condition('module', $module)
->condition('name', $old_delta)
->execute();
} }
} }
return $ret;
} }
/** /**
@ -202,10 +207,9 @@ function filter_update_7003() {
* - Add {filter}.settings. * - Add {filter}.settings.
*/ */
function filter_update_7004() { function filter_update_7004() {
$ret = array(); db_drop_field('filter', 'fid');
db_drop_field($ret, 'filter', 'fid'); db_add_primary_key('filter', array('format', 'name'));
db_add_primary_key($ret, 'filter', array('format', 'name')); db_add_field('filter', 'status',
db_add_field($ret, 'filter', 'status',
array( array(
'type' => 'int', 'type' => 'int',
'not null' => TRUE, 'not null' => TRUE,
@ -213,7 +217,7 @@ function filter_update_7004() {
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)', 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
) )
); );
db_add_field($ret, 'filter', 'settings', db_add_field('filter', 'settings',
array( array(
'type' => 'text', 'type' => 'text',
'not null' => FALSE, 'not null' => FALSE,
@ -224,7 +228,9 @@ function filter_update_7004() {
); );
// Enable all existing filters ({filter} contained only enabled previously). // Enable all existing filters ({filter} contained only enabled previously).
$ret[] = update_sql("UPDATE {filter} SET status = 1"); db_update('filter')
->fields('status', '1')
->execute();
// Move filter settings from system variables into {filter}.settings. // Move filter settings from system variables into {filter}.settings.
$filters = db_query("SELECT * FROM {filter} WHERE module = :name", array(':name' => 'filter')); $filters = db_query("SELECT * FROM {filter} WHERE module = :name", array(':name' => 'filter'));
@ -251,11 +257,13 @@ function filter_update_7004() {
} }
} }
if (!empty($settings)) { if (!empty($settings)) {
$ret[] = update_sql("UPDATE {filter} SET settings = '" . serialize($settings) . "' WHERE format = {$filter->format} AND name = '{$filter->name}'"); db_upddate('filter')
->fields(array('settings' => serialize($settings)))
->condition('format', $filter->format)
->condition('name', $filter->name)
->execute();
} }
} }
return $ret;
} }
/** /**
@ -267,7 +275,6 @@ function filter_update_7004() {
* in cases that used to rely on a single site-wide default. * in cases that used to rely on a single site-wide default.
*/ */
function filter_update_7005() { function filter_update_7005() {
$ret = array();
// Move role data from the filter system to the user permission system. // Move role data from the filter system to the user permission system.
$all_roles = array_keys(user_roles()); $all_roles = array_keys(user_roles());
@ -286,7 +293,7 @@ function filter_update_7005() {
} }
// Drop the roles field from the {filter_format} table. // Drop the roles field from the {filter_format} table.
db_drop_field($ret, 'filter_format', 'roles'); db_drop_field('filter_format', 'roles');
// Add a fallback text format which outputs plain text and appears last on // Add a fallback text format which outputs plain text and appears last on
// the list for all users. Generate a unique name for it, starting with // the list for all users. Generate a unique name for it, starting with
@ -336,7 +343,6 @@ function filter_update_7005() {
// We do not delete the 'filter_default_format' variable, since other modules // We do not delete the 'filter_default_format' variable, since other modules
// may need it in their update functions. // may need it in their update functions.
// @todo This variable can be deleted in Drupal 8. // @todo This variable can be deleted in Drupal 8.
return $ret;
} }
/** /**

View File

@ -116,9 +116,6 @@ function forum_schema() {
* Add new index to forum table. * Add new index to forum table.
*/ */
function forum_update_7000() { function forum_update_7000() {
$ret = array(); db_drop_index('forum', 'nid');
db_drop_index($ret, 'forum', 'nid'); db_add_index('forum', 'forum_topic', array('nid', 'tid'));
db_add_index($ret, 'forum', 'forum_topic', array('nid', 'tid'));
return $ret;
} }

View File

@ -36,10 +36,8 @@ function locale_install() {
* Allow longer location. * Allow longer location.
*/ */
function locale_update_7000() { function locale_update_7000() {
$ret = array(); db_drop_index('locales_source', 'source');
db_drop_index($ret, 'locales_source', 'source'); db_add_index('locales_source', 'source_context', array(array('source', 30), 'context'));
db_add_index($ret, 'locales_source', 'source_context', array(array('source', 30), 'context'));
return $ret;
} }
/** /**

View File

@ -360,42 +360,34 @@ function node_schema() {
* Fix node type 'module' attribute to avoid name-space conflicts. * Fix node type 'module' attribute to avoid name-space conflicts.
*/ */
function node_update_7000() { function node_update_7000() {
$ret = array(); db_update('node_type')
->fields(array('module' => 'node_content'))
$ret[] = update_sql("UPDATE {node_type} SET module = 'node_content' WHERE module = 'node'"); ->condition('module', 'node')
db_change_field($ret, 'node_type', 'module', 'base', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE)); ->execute();
return $ret;
} }
/** /**
* Rename {node_revisions} table to {node_revision}. * Rename {node_revisions} table to {node_revision}.
*/ */
function node_update_7001() { function node_update_7001() {
$ret = array(); db_rename_table('node_revisions', 'node_revision');
db_rename_table($ret, 'node_revisions', 'node_revision');
return $ret;
} }
/** /**
* Extend the node_promote_status index to include all fields required for the node page query. * Extend the node_promote_status index to include all fields required for the node page query.
*/ */
function node_update_7002() { function node_update_7002() {
$ret = array(); db_drop_index('node', 'node_promote_status');
db_drop_index($ret, 'node', 'node_promote_status'); db_add_index('node', 'node_frontpage', array('promote', 'status', 'sticky', 'created'));
db_add_index($ret, 'node', 'node_frontpage', array('promote', 'status', 'sticky', 'created'));
return $ret;
} }
/** /**
* Remove the node_counter if the statistics module is uninstalled. * Remove the node_counter if the statistics module is uninstalled.
*/ */
function node_update_7003() { function node_update_7003() {
$ret = array();
if (drupal_get_installed_schema_version('statistics') == SCHEMA_UNINSTALLED) { if (drupal_get_installed_schema_version('statistics') == SCHEMA_UNINSTALLED) {
db_drop_table($ret, 'node_counter'); db_drop_table('node_counter');
} }
return $ret;
} }
/** /**
@ -418,30 +410,26 @@ function node_update_7004() {
} }
// Delete old variable but leave 'teaser_length' for aggregator module upgrade. // Delete old variable but leave 'teaser_length' for aggregator module upgrade.
variable_del('node_preview'); variable_del('node_preview');
return array();
} }
/** /**
* Add status/comment/promote and sticky columns to the {node_revision} table. * Add status/comment/promote and sticky columns to the {node_revision} table.
*/ */
function node_update_7005() { function node_update_7005() {
$ret = array();
foreach(array('status', 'comment', 'promote', 'sticky') as $column) { foreach(array('status', 'comment', 'promote', 'sticky') as $column) {
db_add_field($ret, 'node_revision', $column, array( db_add_field('node_revision', $column, array(
'type' => 'int', 'type' => 'int',
'not null' => TRUE, 'not null' => TRUE,
'default' => 0, 'default' => 0,
)); ));
} }
return $ret;
} }
/** /**
* Convert body and teaser from node properties to fields, and migrate status/comment/promote and sticky columns to the {node_revision} table. * Convert body and teaser from node properties to fields, and migrate status/comment/promote and sticky columns to the {node_revision} table.
*/ */
function node_update_7006(&$context) { function node_update_7006(&$context) {
$ret = array('#finished' => 0); $context['#finished'] = 0;
// Get node type info for every invocation. // Get node type info for every invocation.
drupal_static_reset('_node_types_build'); drupal_static_reset('_node_types_build');
@ -536,33 +524,29 @@ function node_update_7006(&$context) {
} }
} }
$ret['#finished'] = min(0.99, $context['count'] / $context['total']); $context['#finished'] = min(0.99, $context['count'] / $context['total']);
} }
if (!$found) { if (!$found) {
// All nodes are processed. // All nodes are processed.
$ret[] = array('success' => TRUE, 'query' => "{$context['total']} node body and teaser properties migrated to the 'body' field.");
// Remove the now-obsolete body info from node_revision. // Remove the now-obsolete body info from node_revision.
db_drop_field($ret, 'node_revision', 'body'); db_drop_field('node_revision', 'body');
db_drop_field($ret, 'node_revision', 'teaser'); db_drop_field('node_revision', 'teaser');
db_drop_field($ret, 'node_revision', 'format'); db_drop_field('node_revision', 'format');
// We're done. // We're done.
$ret['#finished'] = 1; $context['#finished'] = 1;
return t("!number node body and teaser properties migrated to the 'body' field.", array('!number' => $context['total']));
} }
} }
return $ret;
} }
/** /**
* Remove column min_word_count. * Remove column min_word_count.
*/ */
function node_update_7007() { function node_update_7007() {
$ret = array(); db_drop_field('node_type', 'min_word_count');
db_drop_field($ret, 'node_type', 'min_word_count');
return $ret;
} }
/** /**

View File

@ -141,22 +141,18 @@ function poll_schema() {
* Rename {poll_choices} table to {poll_choice} and {poll_votes} to {poll_vote}. * Rename {poll_choices} table to {poll_choice} and {poll_votes} to {poll_vote}.
*/ */
function poll_update_7001() { function poll_update_7001() {
$ret = array(); db_rename_table('poll_choices', 'poll_choice');
db_rename_table($ret, 'poll_choices', 'poll_choice'); db_rename_table('poll_votes', 'poll_vote');
db_rename_table($ret, 'poll_votes', 'poll_vote');
return $ret;
} }
/** /**
* Add timestamp field to {poll_votes}. * Add timestamp field to {poll_votes}.
*/ */
function poll_update_7002() { function poll_update_7002() {
$ret = array();
$field = array( $field = array(
'type' => 'int', 'type' => 'int',
'not null' => TRUE, 'not null' => TRUE,
'default' => 0, 'default' => 0,
); );
db_add_field($ret, 'poll_votes', 'timestamp', $field); db_add_field('poll_votes', 'timestamp', $field);
return $ret;
} }

View File

@ -151,8 +151,6 @@ function profile_schema() {
* Rename {profile_fields} table to {profile_field} and {profile_values} to {profile_value}. * Rename {profile_fields} table to {profile_field} and {profile_values} to {profile_value}.
*/ */
function profile_update_7001() { function profile_update_7001() {
$ret = array(); db_rename_table('profile_fields', 'profile_field');
db_rename_table($ret, 'profile_fields', 'profile_field'); db_rename_table('profile_values', 'profile_value');
db_rename_table($ret, 'profile_values', 'profile_value');
return $ret;
} }

View File

@ -154,13 +154,10 @@ function search_schema() {
* Replace unique keys in 'search_dataset' and 'search_index' by primary keys. * Replace unique keys in 'search_dataset' and 'search_index' by primary keys.
*/ */
function search_update_7000() { function search_update_7000() {
$ret = array(); db_drop_unique_key('search_dataset', 'sid_type');
db_drop_unique_key($ret, 'search_dataset', 'sid_type'); db_add_primary_key('search_dataset', array('sid', 'type'));
db_add_primary_key($ret, 'search_dataset', array('sid', 'type'));
db_drop_index($ret, 'search_index', 'word'); db_drop_index('search_index', 'word');
db_drop_unique_key($ret, 'search_index', 'word_sid_type'); db_drop_unique_key('search_index', 'word_sid_type');
db_add_primary_key($ret, 'search_index', array('word', 'sid', 'type')); db_add_primary_key('search_index', array('word', 'sid', 'type'));
return $ret;
} }

View File

@ -1180,7 +1180,7 @@ class DrupalWebTestCase extends DrupalTestCase {
$schema = drupal_get_schema(NULL, TRUE); $schema = drupal_get_schema(NULL, TRUE);
$ret = array(); $ret = array();
foreach ($schema as $name => $table) { foreach ($schema as $name => $table) {
db_drop_table($ret, $name); db_drop_table($name);
} }
// Return the database prefix to the original. // Return the database prefix to the original.

View File

@ -413,12 +413,11 @@ function simpletest_clean_environment() {
function simpletest_clean_database() { function simpletest_clean_database() {
$tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%'); $tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
$schema = drupal_get_schema_unprocessed('simpletest'); $schema = drupal_get_schema_unprocessed('simpletest');
$ret = array();
foreach (array_diff_key($tables, $schema) as $table) { foreach (array_diff_key($tables, $schema) as $table) {
// Strip the prefix and skip tables without digits following "simpletest", // Strip the prefix and skip tables without digits following "simpletest",
// e.g. {simpletest_test_id}. // e.g. {simpletest_test_id}.
if (preg_match('/simpletest\d+.*/', $table, $matches)) { if (preg_match('/simpletest\d+.*/', $table, $matches)) {
db_drop_table($ret, $matches[0]); db_drop_table($matches[0]);
} }
} }

View File

@ -40,12 +40,11 @@ class DatabaseTestCase extends DrupalWebTestCase {
*/ */
function installTables($schema) { function installTables($schema) {
// This ends up being a test for table drop and create, too, which is nice. // This ends up being a test for table drop and create, too, which is nice.
$ret = array();
foreach ($schema as $name => $data) { foreach ($schema as $name => $data) {
if (db_table_exists($name)) { if (db_table_exists($name)) {
db_drop_table($ret, $name); db_drop_table($name);
} }
db_create_table($ret, $name, $data); db_create_table($name, $data);
} }
foreach ($schema as $name => $data) { foreach ($schema as $name => $data) {
@ -2904,9 +2903,8 @@ class DatabaseExtraTypesTestCase extends DrupalWebTestCase {
), ),
), ),
); );
$ret = array(); db_create_table('date_table', $date_table);
db_create_table($ret, 'date_table', $date_table); $this->assertTrue(db_table_exists('date_table'), t('Created table with date field'));
$this->assertEqual($ret[0]['success'], 1, t('Created table with date field'));
db_insert('date_table')->fields(array('date_field')) db_insert('date_table')->fields(array('date_field'))
->values(array('date_field' => '2001-01-01')) ->values(array('date_field' => '2001-01-01'))
@ -2926,8 +2924,8 @@ class DatabaseExtraTypesTestCase extends DrupalWebTestCase {
$this->assertEqual($date, '2100-06-30', t('Date retrieved in order @date', array('@date' => $date))); $this->assertEqual($date, '2100-06-30', t('Date retrieved in order @date', array('@date' => $date)));
db_drop_table($ret, 'date_table'); db_drop_table('date_table');
$this->assertEqual($ret[1]['success'], 1, t('Dropped table with date field')); $this->assertFalse(db_table_exists('date_table'), t('Dropped table with date field'));
} catch (Exception $e) { } catch (Exception $e) {
$this->fail($e->getMessage()); $this->fail($e->getMessage());
@ -2949,9 +2947,8 @@ class DatabaseExtraTypesTestCase extends DrupalWebTestCase {
), ),
), ),
); );
$ret = array(); db_create_table('time_table', $time_table);
db_create_table($ret, 'time_table', $time_table); $this->assertTrue(db_table_exists('time_table'), t('Created table with time field'));
$this->assertEqual($ret[0]['success'], 1, t('Created table with time field'));
db_insert('time_table')->fields(array('time_field')) db_insert('time_table')->fields(array('time_field'))
->values(array('time_field' => '12:59:00')) ->values(array('time_field' => '12:59:00'))
@ -2970,8 +2967,8 @@ class DatabaseExtraTypesTestCase extends DrupalWebTestCase {
$time = $res->fetch()->time_field; $time = $res->fetch()->time_field;
$this->assertEqual($time, '23:17:00', t('Time retrieved in order @time', array('@time' => $time))); $this->assertEqual($time, '23:17:00', t('Time retrieved in order @time', array('@time' => $time)));
db_drop_table($ret, 'time_table'); db_drop_table('time_table');
$this->assertEqual($ret[1]['success'], 1, t('Dropped table with time field')); $this->assertFalse(db_table_exists('time_table'), t('Dropped table with time field'));
} catch (Exception $e) { } catch (Exception $e) {
$this->fail($e->getMessage()); $this->fail($e->getMessage());
} }

View File

@ -1115,9 +1115,9 @@ function field_test_memorize($key = NULL, $value = NULL) {
$memorize = &drupal_static(__FUNCTION__, NULL); $memorize = &drupal_static(__FUNCTION__, NULL);
if (is_null($key)) { if (is_null($key)) {
$ret = $memorize; $return = $memorize;
$memorize = array(); $memorize = array();
return $ret; return $return;
} }
if (is_array($memorize)) { if (is_array($memorize)) {
$memorize[$key][] = $value; $memorize[$key][] = $value;

View File

@ -37,8 +37,7 @@ class SchemaTestCase extends DrupalWebTestCase {
), ),
), ),
); );
$ret = array(); db_create_table('test_table', $table_specification);
db_create_table($ret, 'test_table', $table_specification);
// Assert that the table exists. // Assert that the table exists.
$this->assertTrue(db_table_exists('test_table'), t('The table exists.')); $this->assertTrue(db_table_exists('test_table'), t('The table exists.'));
@ -53,19 +52,19 @@ class SchemaTestCase extends DrupalWebTestCase {
$this->assertFalse($this->tryInsert(), t('Insert without a default failed.')); $this->assertFalse($this->tryInsert(), t('Insert without a default failed.'));
// Add a default value to the column. // Add a default value to the column.
db_field_set_default($ret, 'test_table', 'test_field', 0); db_field_set_default('test_table', 'test_field', 0);
// The insert should now succeed. // The insert should now succeed.
$this->assertTrue($this->tryInsert(), t('Insert with a default succeeded.')); $this->assertTrue($this->tryInsert(), t('Insert with a default succeeded.'));
// Remove the default. // Remove the default.
db_field_set_no_default($ret, 'test_table', 'test_field'); db_field_set_no_default('test_table', 'test_field');
// The insert should fail again. // The insert should fail again.
$this->assertFalse($this->tryInsert(), t('Insert without a default failed.')); $this->assertFalse($this->tryInsert(), t('Insert without a default failed.'));
// Rename the table. // Rename the table.
db_rename_table($ret, 'test_table', 'test_table2'); db_rename_table('test_table', 'test_table2');
// We need the default so that we can insert after the rename. // We need the default so that we can insert after the rename.
db_field_set_default($ret, 'test_table2', 'test_field', 0); db_field_set_default('test_table2', 'test_field', 0);
$this->assertFalse($this->tryInsert(), t('Insert into the old table failed.')); $this->assertFalse($this->tryInsert(), t('Insert into the old table failed.'));
$this->assertTrue($this->tryInsert('test_table2'), t('Insert into the new table succeeded.')); $this->assertTrue($this->tryInsert('test_table2'), t('Insert into the new table succeeded.'));
@ -74,19 +73,19 @@ class SchemaTestCase extends DrupalWebTestCase {
$this->assertEqual($count, 2, t('Two fields were successfully inserted.')); $this->assertEqual($count, 2, t('Two fields were successfully inserted.'));
// Try to drop the table. // Try to drop the table.
db_drop_table($ret, 'test_table2'); db_drop_table('test_table2');
$this->assertFalse(db_table_exists('test_table2'), t('The dropped table does not exist.')); $this->assertFalse(db_table_exists('test_table2'), t('The dropped table does not exist.'));
// Recreate the table. // Recreate the table.
db_create_table($ret, 'test_table', $table_specification); db_create_table('test_table', $table_specification);
db_field_set_default($ret, 'test_table', 'test_field', 0); db_field_set_default('test_table', 'test_field', 0);
db_add_field($ret, 'test_table', 'test_serial', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.')); 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. // Assert that the column comment has been set.
$this->checkSchemaComment('Added column description.', 'test_table', 'test_serial'); $this->checkSchemaComment('Added column description.', 'test_table', 'test_serial');
// Change the new field to a serial column. // Change the new field to a serial column.
db_change_field($ret, 'test_table', 'test_serial', 'test_serial', array('type' => 'serial', 'not null' => TRUE, 'description' => 'Changed column description.'), array('primary key' => array('test_serial'))); 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. // Assert that the column comment has been set.
$this->checkSchemaComment('Changed column description.', 'test_table', 'test_serial'); $this->checkSchemaComment('Changed column description.', 'test_table', 'test_serial');

View File

@ -142,9 +142,7 @@ function statistics_schema() {
* Allow longer referrers. * Allow longer referrers.
*/ */
function statistics_update_7000() { function statistics_update_7000() {
$ret = array(); db_change_field('accesslog', 'url', 'url', array('type' => 'text', 'not null' => FALSE));
db_change_field($ret, 'accesslog', 'url', 'url', array('type' => 'text', 'not null' => FALSE));
return $ret;
} }
/** /**

View File

@ -1897,30 +1897,28 @@ function hook_install() {
* If your update task is potentially time-consuming, you'll need to implement a * If your update task is potentially time-consuming, you'll need to implement a
* multipass update to avoid PHP timeouts. Multipass updates use the $sandbox * multipass update to avoid PHP timeouts. Multipass updates use the $sandbox
* parameter provided by the batch API (normally, $context['sandbox']) to store * parameter provided by the batch API (normally, $context['sandbox']) to store
* information between successive calls, and the $ret['#finished'] return value * information between successive calls, and the $sandbox['#finished'] value
* to provide feedback regarding completion level. * to provide feedback regarding completion level.
* *
* See the batch operations page for more information on how to use the batch API: * See the batch operations page for more information on how to use the batch API:
* @link http://drupal.org/node/146843 http://drupal.org/node/146843 @endlink * @link http://drupal.org/node/146843 http://drupal.org/node/146843 @endlink
* *
* @return An array with the results of the calls to update_sql(). An upate * @throws DrupalUpdateException, PDOException
* function can force the current and all later updates for this * In case of error, update hooks should throw an instance of DrupalUpdateException
* module to abort by returning a $ret array with an element like: * with a meaningful message for the user. If a database query fails for whatever
* $ret['#abort'] = array('success' => FALSE, 'query' => 'What went wrong'); * reason, it will throw a PDOException.
* The schema version will not be updated in this case, and all the *
* aborted updates will continue to appear on update.php as updates that * @return
* have not yet been run. Multipass update functions will also want to pass * Optionally update hooks may return a translated string that will be displayed
* back the $ret['#finished'] variable to inform the batch API of progress. * to the user. If no message is returned, no message will be presented to the
* user.
*/ */
function hook_update_N(&$sandbox = NULL) { function hook_update_N(&$sandbox = NULL) {
// For most updates, the following is sufficient. // For most updates, the following is sufficient.
$ret = array(); db_add_field('mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE, 'description' => 'My new integer column.'));
db_add_field($ret, 'mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE, 'description' => 'My new integer column.'));
return $ret;
// However, for more complex operations that may take a long time, // However, for more complex operations that may take a long time,
// you may hook into Batch API as in the following example. // you may hook into Batch API as in the following example.
$ret = array();
// Update 3 users at a time to have an exclamation point after their names. // Update 3 users at a time to have an exclamation point after their names.
// (They're really happy that we can do batch API in this hook!) // (They're really happy that we can do batch API in this hook!)
@ -1938,15 +1936,23 @@ function hook_update_N(&$sandbox = NULL) {
->execute(); ->execute();
foreach ($users as $user) { foreach ($users as $user) {
$user->name .= '!'; $user->name .= '!';
$ret[] = update_sql("UPDATE {users} SET name = '$user->name' WHERE uid = $user->uid"); db_update('users')
->fields(array('name' => $user->name))
->condition('uid', $user->uid)
->execute();
$sandbox['progress']++; $sandbox['progress']++;
$sandbox['current_uid'] = $user->uid; $sandbox['current_uid'] = $user->uid;
} }
$ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
return $ret; // To display a message to the user when the update is completed, return it.
// If you do not want to display a completion message, simply return nothing.
return t('The update did what it was supposed to do.');
// In case of an error, simply throw an exception with an error message.
throw new DrupalUpdateException('Something went wrong; here is what you should do.');
} }
/** /**

View File

@ -1527,7 +1527,6 @@ function system_update_last_removed() {
* Rename blog and forum permissions to be consistent with other content types. * Rename blog and forum permissions to be consistent with other content types.
*/ */
function system_update_7000() { function system_update_7000() {
$ret = array();
$result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid"); $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
foreach ($result as $role) { foreach ($result as $role) {
$renamed_permission = preg_replace('/(?<=^|,\ )create\ blog\ entries(?=,|$)/', 'create blog content', $role->perm); $renamed_permission = preg_replace('/(?<=^|,\ )create\ blog\ entries(?=,|$)/', 'create blog content', $role->perm);
@ -1543,28 +1542,25 @@ function system_update_7000() {
$renamed_permission = preg_replace('/(?<=^|,\ )edit\ own\ forum\ topics(?=,|$)/', 'edit own forum content', $role->perm); $renamed_permission = preg_replace('/(?<=^|,\ )edit\ own\ forum\ topics(?=,|$)/', 'edit own forum content', $role->perm);
if ($renamed_permission != $role->perm) { if ($renamed_permission != $role->perm) {
$ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid"); db_update('permission')
->fields(array('perm' => $renamed_permission))
->condition('rid', $role->rid)
->execute();
} }
} }
return $ret;
} }
/** /**
* Generate a cron key and save it in the variables table. * Generate a cron key and save it in the variables table.
*/ */
function system_update_7001() { function system_update_7001() {
$ret = array();
variable_set('cron_key', md5(mt_rand())); variable_set('cron_key', md5(mt_rand()));
$ret[] = array('success' => TRUE, 'query' => "variable_set('cron_key')");
return $ret;
} }
/** /**
* Add a table to store blocked IP addresses. * Add a table to store blocked IP addresses.
*/ */
function system_update_7002() { function system_update_7002() {
$ret = array();
$schema['blocked_ips'] = array( $schema['blocked_ips'] = array(
'description' => 'Stores blocked IP addresses.', 'description' => 'Stores blocked IP addresses.',
'fields' => array( 'fields' => array(
@ -1588,16 +1584,14 @@ function system_update_7002() {
'primary key' => array('iid'), 'primary key' => array('iid'),
); );
db_create_table($ret, 'blocked_ips', $schema['blocked_ips']); db_create_table('blocked_ips', $schema['blocked_ips']);
return $ret;
} }
/** /**
* Update {blocked_ips} with valid IP addresses from {access}. * Update {blocked_ips} with valid IP addresses from {access}.
*/ */
function system_update_7003() { function system_update_7003() {
$ret = array(); $messages = array();
$type = 'host'; $type = 'host';
$result = db_query("SELECT mask FROM {access} WHERE status = :status AND type = :type", array( $result = db_query("SELECT mask FROM {access} WHERE status = :status AND type = :type", array(
':status' => 0, ':status' => 0,
@ -1605,16 +1599,17 @@ function system_update_7003() {
)); ));
foreach ($result as $blocked) { foreach ($result as $blocked) {
if (filter_var($blocked->mask, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) !== FALSE) { if (filter_var($blocked->mask, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) !== FALSE) {
$ret[] = update_sql("INSERT INTO {blocked_ips} (ip) VALUES ('$blocked->mask')"); db_insert('blocked_ips')
->fields(array('ip' => $blocked->mask))
->execute();
} }
else { else {
$invalid_host = check_plain($blocked->mask); $invalid_host = check_plain($blocked->mask);
$ret[] = array('success' => TRUE, 'query' => 'The host ' . $invalid_host . ' is no longer blocked because it is not a valid IP address.'); $messages[] = t('The host !host is no longer blocked because it is not a valid IP address.', array('!host' => $invalid_host ));
} }
} }
if (isset($invalid_host)) { if (isset($invalid_host)) {
drupal_set_message('Drupal no longer supports wildcard IP address blocking. Visitors whose IP addresses match ranges you have previously set using <em>access rules</em> will no longer be blocked from your site when you put the site online. See the <a href="http://drupal.org/node/24302">IP address and referrer blocking Handbook page</a> for alternative methods.', 'warning'); drupal_set_message('Drupal no longer supports wildcard IP address blocking. Visitors whose IP addresses match ranges you have previously set using <em>access rules</em> will no longer be blocked from your site when you put the site online. See the <a href="http://drupal.org/node/24302">IP address and referrer blocking Handbook page</a> for alternative methods.', 'warning');
$ret[] = array('success' => TRUE, 'query' => '');
} }
// Make sure not to block any IP addresses that were specifically allowed by access rules. // Make sure not to block any IP addresses that were specifically allowed by access rules.
if (!empty($result)) { if (!empty($result)) {
@ -1622,19 +1617,22 @@ function system_update_7003() {
':status' => 1, ':status' => 1,
':type' => $type, ':type' => $type,
)); ));
$or = db_condition('or');
foreach ($result as $allowed) { foreach ($result as $allowed) {
$ret[] = update_sql("DELETE FROM {blocked_ips} WHERE LOWER(ip) LIKE LOWER('$allowed->mask')"); $or->where('LOWER(ip) LIKE LOWER(:mask)', array(':mask' => $allowed->mask));
}
if (count($or)) {
db_delete('blocked_ips')
->condition($or)
->execute();
} }
} }
return $ret;
} }
/** /**
* Remove hardcoded numeric deltas from all blocks in core. * Remove hardcoded numeric deltas from all blocks in core.
*/ */
function system_update_7004(&$sandbox) { function system_update_7004(&$sandbox) {
$ret = array();
// Get an array of the renamed block deltas, organized by module. // Get an array of the renamed block deltas, organized by module.
$renamed_deltas = array( $renamed_deltas = array(
'blog' => array('0' => 'recent'), 'blog' => array('0' => 'recent'),
@ -1672,7 +1670,11 @@ function system_update_7004(&$sandbox) {
)) ))
->fetchField(); ->fetchField();
if ($block_exists) { if ($block_exists) {
$ret[] = update_sql("UPDATE {" . $table . "} SET delta = '" . $new_delta . "' WHERE module = '" . $module . "' AND delta = '" . $old_delta . "'"); db_update($table)
->fields(array('delta' => $new_delta))
->condition('module', $module)
->condition('delta', $old_delta)
->execute();
} }
} }
} }
@ -1728,24 +1730,20 @@ function system_update_7004(&$sandbox) {
} }
// Indicate our current progress to the batch update system. // Indicate our current progress to the batch update system.
if ($sandbox['progress'] < $sandbox['max']) { if ($sandbox['progress'] < $sandbox['max']) {
$ret['#finished'] = $sandbox['progress'] / $sandbox['max']; $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
} }
return $ret;
} }
/** /**
* Remove throttle columns and variables. * Remove throttle columns and variables.
*/ */
function system_update_7005() { function system_update_7005() {
$ret = array(); db_drop_field('blocks', 'throttle');
db_drop_field($ret, 'blocks', 'throttle'); db_drop_field('system', 'throttle');
db_drop_field($ret, 'system', 'throttle');
variable_del('throttle_user'); variable_del('throttle_user');
variable_del('throttle_anonymous'); variable_del('throttle_anonymous');
variable_del('throttle_level'); variable_del('throttle_level');
variable_del('throttle_probability_limiter'); variable_del('throttle_probability_limiter');
return $ret;
} }
/** /**
@ -1753,7 +1751,6 @@ function system_update_7005() {
* longer needed. * longer needed.
*/ */
function system_update_7006() { function system_update_7006() {
$ret = array();
$schema['registry'] = array( $schema['registry'] = array(
'fields' => array( 'fields' => array(
'name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), 'name' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
@ -1787,11 +1784,10 @@ function system_update_7006() {
'indexes' => array('expire' => array('expire')), 'indexes' => array('expire' => array('expire')),
'primary key' => array('cid'), 'primary key' => array('cid'),
); );
db_create_table($ret, 'cache_registry', $schema['cache_registry']); db_create_table('cache_registry', $schema['cache_registry']);
db_create_table($ret, 'registry', $schema['registry']); db_create_table('registry', $schema['registry']);
db_create_table($ret, 'registry_file', $schema['registry_file']); db_create_table('registry_file', $schema['registry_file']);
registry_rebuild(); registry_rebuild();
return $ret;
} }
/** /**
@ -1801,8 +1797,6 @@ function system_update_7006() {
* all modules can use the updated permission scheme during their updates. * all modules can use the updated permission scheme during their updates.
*/ */
function system_update_7007() { function system_update_7007() {
$ret = array();
$schema['role_permission'] = array( $schema['role_permission'] = array(
'fields' => array( 'fields' => array(
'rid' => array( 'rid' => array(
@ -1823,9 +1817,10 @@ function system_update_7007() {
), ),
); );
db_create_table($ret, 'role_permission', $schema['role_permission']); db_create_table('role_permission', $schema['role_permission']);
// Copy the permissions from the old {permission} table to the new {role_permission} table. // Copy the permissions from the old {permission} table to the new {role_permission} table.
$messages = array();
$result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid ASC"); $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid ASC");
$query = db_insert('role_permission')->fields(array('rid', 'permission')); $query = db_insert('role_permission')->fields(array('rid', 'permission'));
foreach ($result as $role) { foreach ($result as $role) {
@ -1835,12 +1830,12 @@ function system_update_7007() {
'permission' => $perm, 'permission' => $perm,
)); ));
} }
$ret[] = array('success' => TRUE, 'query' => "Inserted into {role_permission} the permissions for role ID " . $role->rid); $messages[] = t('Inserted into {role_permission} the permissions for role ID !id', array('!id' => $role->rid));
} }
$query->execute(); $query->execute();
db_drop_table($ret, 'permission'); db_drop_table('permission');
return $ret; return implode(', ', $messages);
} }
@ -1849,20 +1844,18 @@ function system_update_7007() {
* the choice order. Rename chorder to weight. * the choice order. Rename chorder to weight.
*/ */
function system_update_7008() { function system_update_7008() {
$ret = array();
if (db_table_exists('poll_votes')) { if (db_table_exists('poll_votes')) {
// Add chid column and convert existing votes. // Add chid column and convert existing votes.
db_add_field($ret, 'poll_votes', 'chid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0)); db_add_field('poll_votes', 'chid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
db_add_index($ret, 'poll_votes', 'chid', array('chid')); db_add_index('poll_votes', 'chid', array('chid'));
$ret[] = update_sql("UPDATE {poll_votes} SET chid = (SELECT chid FROM {poll_choices} c WHERE {poll_votes}.chorder = c.chorder AND {poll_votes}.nid = c.nid)"); db_query("UPDATE {poll_votes} SET chid = (SELECT chid FROM {poll_choices} c WHERE {poll_votes}.chorder = c.chorder AND {poll_votes}.nid = c.nid)");
// Remove old chorder column. // Remove old chorder column.
db_drop_field($ret, 'poll_votes', 'chorder'); db_drop_field('poll_votes', 'chorder');
} }
if (db_table_exists('poll_choices')) { if (db_table_exists('poll_choices')) {
// Change the chorder column to weight in poll_choices. // Change the chorder column to weight in poll_choices.
db_change_field($ret, 'poll_choices', 'chorder', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny')); db_change_field('poll_choices', 'chorder', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
} }
return $ret;
} }
/** /**
@ -1870,30 +1863,29 @@ function system_update_7008() {
* *
*/ */
function system_update_7009() { function system_update_7009() {
$ret = array(); db_update('variable')
$ret[] = update_sql("UPDATE {variable} SET name = 'menu_main_links_source' WHERE name = 'menu_primary_links_source'"); ->fields(array('name' => 'main_menu_links_source'))
->condition('name', 'menu_primary_links_source')
return $ret; ->execute();
} }
/** /**
* Moved to system_update_6048(). * Moved to system_update_6048().
*/ */
function system_update_7010() { function system_update_7010() {
return array(); return '';
} }
/** /**
* Split the 'bypass node access' permission from 'administer nodes'. * Split the 'bypass node access' permission from 'administer nodes'.
*/ */
function system_update_7011() { function system_update_7011() {
$ret = array();
// Get existing roles that can 'administer nodes'. // Get existing roles that can 'administer nodes'.
$rids = array(); $rids = array();
$rids = db_query("SELECT rid FROM {role_permission} WHERE permission = :perm", array(':perm' => 'administer nodes'))->fetchCol(); $rids = db_query("SELECT rid FROM {role_permission} WHERE permission = :perm", array(':perm' => 'administer nodes'))->fetchCol();
// None found. // None found.
if (empty($rids)) { if (empty($rids)) {
return $ret; return;
} }
$insert = db_insert('role_permission')->fields(array('rid', 'permission')); $insert = db_insert('role_permission')->fields(array('rid', 'permission'));
foreach ($rids as $rid) { foreach ($rids as $rid) {
@ -1903,7 +1895,6 @@ function system_update_7011() {
)); ));
} }
$insert->execute(); $insert->execute();
return $ret;
} }
/** /**
@ -1911,18 +1902,15 @@ function system_update_7011() {
* {boxes} to {box}. * {boxes} to {box}.
*/ */
function system_update_7012() { function system_update_7012() {
$ret = array(); db_rename_table('blocks', 'block');
db_rename_table($ret, 'blocks', 'block'); db_rename_table('blocks_roles', 'block_role');
db_rename_table($ret, 'blocks_roles', 'block_role'); db_rename_table('boxes', 'box');
db_rename_table($ret, 'boxes', 'box');
return $ret;
} }
/** /**
* Convert default time zone offset to default time zone name. * Convert default time zone offset to default time zone name.
*/ */
function system_update_7013() { function system_update_7013() {
$ret = array();
$timezone = NULL; $timezone = NULL;
$timezones = system_time_zones(); $timezones = system_time_zones();
// If the contributed Date module set a default time zone name, use this // If the contributed Date module set a default time zone name, use this
@ -1956,32 +1944,33 @@ function system_update_7013() {
} }
variable_set('date_default_timezone', $timezone); variable_set('date_default_timezone', $timezone);
drupal_set_message('The default time zone has been set to <em>' . check_plain($timezone) . '</em>. Please check the ' . l('date and time configuration page', 'admin/config/regional/settings') . ' to configure it correctly.', 'warning'); drupal_set_message('The default time zone has been set to <em>' . check_plain($timezone) . '</em>. Please check the ' . l('date and time configuration page', 'admin/config/regional/settings') . ' to configure it correctly.', 'warning');
return $ret;
} }
/** /**
* Drop the bootstrap column from the {system} table. This was reverted. * Drop the bootstrap column from the {system} table. This was reverted.
*/ */
function system_update_7014() { function system_update_7014() {
$ret = array();
return $ret;
} }
/** /**
* Change the user logout path. * Change the user logout path.
*/ */
function system_update_7015() { function system_update_7015() {
$ret = array(); db_update('menu_links')
$ret[] = update_sql("UPDATE {menu_links} SET link_path = 'user/logout' WHERE link_path = 'logout'"); ->fields(array('link_path' => 'user/logout'))
$ret[] = update_sql("UPDATE {menu_links} SET router_path = 'user/logout' WHERE router_path = 'logout'"); ->condition('link_path', 'logout')
return $ret; ->execute();
db_update('menu_links')
->fields(array('router_path' => 'user/logout'))
->condition('router_path', 'logout')
->execute();
} }
/** /**
* Remove custom datatype *_unsigned in PostgreSQL. * Remove custom datatype *_unsigned in PostgreSQL.
*/ */
function system_update_7016() { function system_update_7016() {
$ret = array();
// Only run these queries if the driver used is pgsql. // Only run these queries if the driver used is pgsql.
if (db_driver() == 'pgsql') { if (db_driver() == 'pgsql') {
$result = db_query("SELECT c.relname AS table, a.attname AS field, $result = db_query("SELECT c.relname AS table, a.attname AS field,
@ -2001,28 +1990,25 @@ function system_update_7016() {
$datatype = 'bigint'; $datatype = 'bigint';
break; break;
} }
$ret[] = update_sql('ALTER TABLE ' . $row->table . ' ALTER COLUMN ' . $row->field . ' TYPE ' . $datatype); db_query('ALTER TABLE ' . $row->table . ' ALTER COLUMN ' . $row->field . ' TYPE ' . $datatype);
$ret[] = update_sql('ALTER TABLE ' . $row->table . ' ADD CHECK (' . $row->field . ' >= 0)'); db_query('ALTER TABLE ' . $row->table . ' ADD CHECK (' . $row->field . ' >= 0)');
} }
$ret[] = update_sql('DROP DOMAIN smallint_unsigned'); db_query('DROP DOMAIN smallint_unsigned');
$ret[] = update_sql('DROP DOMAIN int_unsigned'); db_query('DROP DOMAIN int_unsigned');
$ret[] = update_sql('DROP DOMAIN bigint_unsigned'); db_query('DROP DOMAIN bigint_unsigned');
} }
return $ret;
} }
/** /**
* Change the theme setting 'toggle_node_info' into a per content type variable. * Change the theme setting 'toggle_node_info' into a per content type variable.
*/ */
function system_update_7017() { function system_update_7017() {
$ret = array();
$types = node_type_get_types(); $types = node_type_get_types();
if (count($types)) { if (count($types)) {
foreach ($types as $type) { foreach ($types as $type) {
$node_info = theme_get_setting('toggle_node_info_' . $type->type); $node_info = theme_get_setting('toggle_node_info_' . $type->type);
if ($node_info !== NULL) { if ($node_info !== NULL) {
variable_set('node_submitted_' . $type->type, $node_info); variable_set('node_submitted_' . $type->type, $node_info);
$ret[] = array('success' => TRUE, 'query' => "variable_set('node_submitted_$type->type')");
} }
} }
} }
@ -2035,85 +2021,148 @@ function system_update_7017() {
} }
} }
variable_set('theme_settings', $theme_settings); variable_set('theme_settings', $theme_settings);
$ret[] = array('success' => TRUE, 'query' => "variable_set('theme_settings')");
return $ret;
} }
/** /**
* Shorten the {system}.type column and add an index on type and name. * Shorten the {system}.type column and add an index on type and name.
*/ */
function system_update_7018() { function system_update_7018() {
$ret = array(); db_drop_index('system', 'modules');
db_drop_index($ret, 'system', 'modules'); db_drop_index('system', 'type_name');
db_drop_index($ret, 'system', 'type_name'); db_change_field('system', 'type', 'type', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''));
db_change_field($ret, 'system', 'type', 'type', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => '')); db_add_index('system', 'modules', array('type', 'status', 'weight', 'name'));
db_add_index($ret, 'system', 'modules', array('type', 'status', 'weight', 'name')); db_add_index('system', 'type_name', array('type', 'name'));
db_add_index($ret, 'system', 'type_name', array('type', 'name'));
return $ret;
} }
/** /**
* Enable field module. * Enable field module.
*/ */
function system_update_7020() { function system_update_7020() {
$ret = array();
$module_list = array('field_sql_storage', 'field'); $module_list = array('field_sql_storage', 'field');
drupal_install_modules($module_list); drupal_install_modules($module_list);
module_enable($module_list); module_enable($module_list);
return $ret;
} }
/** /**
* Add new blocks to new regions, migrate custom variables to blocks. * Add new blocks to new regions, migrate custom variables to blocks.
*/ */
function system_update_7021() { function system_update_7021() {
$ret = array();
// Collect a list of themes with blocks. // Collect a list of themes with blocks.
$themes_with_blocks = array(); $themes_with_blocks = array();
$result = db_query("SELECT s.name FROM {system} s INNER JOIN {block} b ON s.name = b.theme WHERE s.type = 'theme' GROUP by s.name"); $result = db_query("SELECT s.name FROM {system} s INNER JOIN {block} b ON s.name = b.theme WHERE s.type = 'theme' GROUP by s.name");
$insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
foreach ($result as $theme) { foreach ($result as $theme) {
$themes_with_blocks[] = $theme->name; $themes_with_blocks[] = $theme->name;
// Add new system generated help block. // Add new system generated help block.
$ret[] = update_sql("INSERT INTO {block} (module, delta, theme, status, weight, region, pages, cache) VALUES ('system', 'help', '" . $theme->name . "', 1, 0, 'help', '', 1)"); $insert->values(array(
'module' => 'system',
'delta' => 'help',
'theme' => $theme->name,
'status' => 1,
'weight' => 0,
'region' => 'help',
'pages' => '',
'cache' => 1,
));
// Add new system generated main page content block. // Add new system generated main page content block.
$ret[] = update_sql("INSERT INTO {block} (module, delta, theme, status, weight, region, pages, cache) VALUES ('system', 'main', '" . $theme->name . "', 1, 0, 'content', '', -1)"); $insert->values(array(
'module' => 'system',
'delta' => 'main',
'theme' => $theme->name,
'status' => 1,
'weight' => 0,
'region' => 'content',
'pages' => '',
'cache' => -1,
));
} }
$insert->execute();
// Migrate blocks from left/right regions to first/second regions. // Migrate blocks from left/right regions to first/second regions.
$ret[] = update_sql("UPDATE {block} SET region = 'sidebar_first' WHERE region = 'left'"); db_update('block')
$ret[] = update_sql("UPDATE {block} SET region = 'sidebar_second' WHERE region = 'right'"); ->fields(array('region' => 'sidebar_first'))
->condition('region', 'left')
->execute();
db_update('block')
->fields(array('region' => 'sidebar_second'))
->condition('region', 'right')
->execute();
// Migrate contact form information. // Migrate contact form information.
$default_format = variable_get('filter_default_format', 1); $default_format = variable_get('filter_default_format', 1);
if ($contact_help = variable_get('contact_form_information', '')) { if ($contact_help = variable_get('contact_form_information', '')) {
$bid = db_insert('box')->fields(array('body' => $contact_help, 'info' => 'Contact page help', 'format' => $default_format))->execute(); $bid = db_insert('box')
->fields(array(
'body' => $contact_help,
'info' => 'Contact page help',
'format' => $default_format,
))
->execute();
$insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
foreach ($themes_with_blocks as $theme) { foreach ($themes_with_blocks as $theme) {
// Add contact help block for themes, which had blocks. // Add contact help block for themes, which had blocks.
$ret[] = update_sql("INSERT INTO {block} (module, delta, theme, status, weight, region, visibility, pages, cache) VALUES ('block', '" . $bid . "', '" . $theme . "', 1, 5, 'help', 1, 'contact', -1)"); $insert->values(array(
'module' => 'block',
'delta' => $bid,
'theme' => $theme,
'status' => 1,
'weight' => 5,
'region' => 'help',
'visibility' => 1,
'pages' => 'contact',
'cache' => -1,
));
} }
drupal_set_message('The contact form information setting was migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to only show on the site-wide contact page. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right.'); drupal_set_message('The contact form information setting was migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to only show on the site-wide contact page. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right.');
} }
$insert->execute();
// Migrate user help setting. // Migrate user help setting.
if ($user_help = variable_get('user_registration_help', '')) { if ($user_help = variable_get('user_registration_help', '')) {
$bid = db_insert('box')->fields(array('body' => $user_help, 'info' => 'User registration guidelines', 'format' => $default_format))->execute(); $bid = db_insert('box')->fields(array('body' => $user_help, 'info' => 'User registration guidelines', 'format' => $default_format))->execute();
$insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
foreach ($themes_with_blocks as $theme) { foreach ($themes_with_blocks as $theme) {
// Add user registration help block for themes, which had blocks. // Add user registration help block for themes, which had blocks.
$ret[] = update_sql("INSERT INTO {block} (module, delta, theme, status, weight, region, visibility, pages, cache) VALUES ('block', '" . $bid . "', '" . $theme . "', 1, 5, 'help', 1, 'user/register', -1)"); $insert->values(array(
'module' => 'block',
'delta' => $bid,
'theme' => $theme,
'status' => 1,
'weight' => 5,
'region' => 'help',
'visibility' => 1,
'pages' => 'user/register',
'cache' => -1,
));
} }
drupal_set_message('The user registration guidelines were migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to only show on the user registration page. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right.'); drupal_set_message('The user registration guidelines were migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to only show on the user registration page. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right.');
$insert->execute();
} }
// Migrate site mission setting. // Migrate site mission setting.
if ($mission = variable_get('site_mission')) { if ($mission = variable_get('site_mission')) {
$bid = db_insert('box')->fields(array('body' => $mission, 'info' => 'Site mission', 'format' => $default_format))->execute(); $bid = db_insert('box')->fields(array('body' => $mission, 'info' => 'Site mission', 'format' => $default_format))->execute();
$insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
foreach ($themes_with_blocks as $theme) { foreach ($themes_with_blocks as $theme) {
// Add mission block for themes, which had blocks. // Add mission block for themes, which had blocks.
$ret[] = update_sql("INSERT INTO {block} (module, delta, theme, status, weight, region, visibility, pages, cache) VALUES ('block', '" . $bid . "', '" . $theme . "', 1, 0, 'highlight', 1, '<front>', -1)"); $insert->values(array(
'module' => 'block',
'delta' => $bid,
'theme' => $theme,
'status' => 1,
'weight' => 0,
'region' => 'highlight',
'visibility' => 1,
'pages' => '<front>',
'cache' => -1,
));
} }
drupal_set_message('The site mission was migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to only show on the front page in the highlighted content region. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right. If your theme does not have a highlighted content region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.'); drupal_set_message('The site mission was migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to only show on the front page in the highlighted content region. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right. If your theme does not have a highlighted content region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.');
$insert->execute();
// Migrate mission to RSS site description. // Migrate mission to RSS site description.
variable_set('feed_description', $mission); variable_set('feed_description', $mission);
} }
@ -2121,13 +2170,25 @@ function system_update_7021() {
// Migrate site footer message to a custom block. // Migrate site footer message to a custom block.
if ($footer_message = variable_get('site_footer', '')) { if ($footer_message = variable_get('site_footer', '')) {
$bid = db_insert('box')->fields(array('body' => $footer_message, 'info' => 'Footer message', 'format' => $default_format))->execute(); $bid = db_insert('box')->fields(array('body' => $footer_message, 'info' => 'Footer message', 'format' => $default_format))->execute();
$insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
foreach ($themes_with_blocks as $theme) { foreach ($themes_with_blocks as $theme) {
// Add site footer block for themes, which had blocks. // Add site footer block for themes, which had blocks.
// Set low weight, so the block comes early (it used to be // Set low weight, so the block comes early (it used to be
// before the other blocks). // before the other blocks).
$ret[] = update_sql("INSERT INTO {block} (module, delta, theme, status, weight, region, pages, cache) VALUES ('block', '" . $bid . "', '" . $theme . "', 1, -10, 'footer', '', -1)"); $insert->values(array(
'module' => 'block',
'delta' => $bid,
'theme' => $theme,
'status' => 1,
'weight' => -10,
'region' => 'footer',
'pages' => '',
'cache' => -1,
));
} }
drupal_set_message('The footer message was migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to appear in the footer. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right. If your theme does not have a footer region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.'); drupal_set_message('The footer message was migrated to <a href="' . url('admin/structure/block/configure/block/' . $bid) . '">a custom block</a> and set up to appear in the footer. The block was set to use the default text format, which might differ from the HTML based format used before. Please check the block and ensure that the output is right. If your theme does not have a footer region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.');
$insert->execute();
} }
// Remove the variables (even if they were saved empty on the admin interface), // Remove the variables (even if they were saved empty on the admin interface),
@ -2139,16 +2200,12 @@ function system_update_7021() {
// Rebuild theme data, so the new 'help' region is identified. // Rebuild theme data, so the new 'help' region is identified.
system_get_theme_data(); system_get_theme_data();
return $ret;
} }
/** /**
* Add the queue tables. * Add the queue tables.
*/ */
function system_update_7022() { function system_update_7022() {
$ret = array();
$schema['queue'] = array( $schema['queue'] = array(
'description' => 'Stores items in queues.', 'description' => 'Stores items in queues.',
'fields' => array( 'fields' => array(
@ -2209,19 +2266,19 @@ function system_update_7022() {
), ),
'primary key' => array('consumer_id'), 'primary key' => array('consumer_id'),
); );
db_create_table($ret, 'queue', $schema['queue']);
db_create_table($ret, 'queue_consumer_id', $schema['queue_consumer_id']);
return $ret; db_create_table('queue', $schema['queue']);
db_create_table('queue_consumer_id', $schema['queue_consumer_id']);
} }
/** /**
* Change the PHP for settings permission. * Change the PHP for settings permission.
*/ */
function system_update_7023() { function system_update_7023() {
$ret = array(); db_update('role_permission')
$ret[] = update_sql("UPDATE {role_permission} SET permission = 'use PHP for settings' WHERE permission = 'use PHP for block visibility'"); ->fields(array('permission' => 'use PHP for settings'))
return $ret; ->condition('permission', 'use PHP for block visibility')
->execute();
} }
/** /**
@ -2231,28 +2288,22 @@ function system_update_7023() {
* for driver-specific updates yet. * for driver-specific updates yet.
*/ */
function system_update_7024() { function system_update_7024() {
$ret = array();
if (db_driver() == 'pgsql') { if (db_driver() == 'pgsql') {
$ret[] = update_sql('CREATE OR REPLACE FUNCTION "substring_index"(text, text, integer) RETURNS text AS db_query('CREATE OR REPLACE FUNCTION "substring_index"(text, text, integer) RETURNS text AS
\'SELECT array_to_string((string_to_array($1, $2)) [1:$3], $2);\' \'SELECT array_to_string((string_to_array($1, $2)) [1:$3], $2);\'
LANGUAGE \'sql\'' LANGUAGE \'sql\''
); );
} }
return $ret;
} }
/** /**
* Improve indexes on the {url_alias} table. * Improve indexes on the {url_alias} table.
*/ */
function system_update_7025() { function system_update_7025() {
$ret = array(); db_drop_index('url_alias', 'src_language');
db_drop_index($ret, 'url_alias', 'src_language'); db_drop_index('url_alias', 'dst_language');
db_drop_index($ret, 'url_alias', 'dst_language'); db_add_index('url_alias', 'dst_language_pid', array('dst', 'language', 'pid'));
db_add_index($ret, 'url_alias', 'dst_language_pid', array('dst', 'language', 'pid')); db_add_index('url_alias', 'src_language_pid', array('src', 'language', 'pid'));
db_add_index($ret, 'url_alias', 'src_language_pid', array('src', 'language', 'pid'));
return $ret;
} }
/** /**
@ -2260,38 +2311,34 @@ function system_update_7025() {
* *
*/ */
function system_update_7026() { function system_update_7026() {
$ret = array(); db_change_field('url_alias', 'src', 'src', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
db_change_field($ret, 'url_alias', 'src', 'src', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE)); db_change_field('url_alias', 'dst', 'dst', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
db_change_field($ret, 'url_alias', 'dst', 'dst', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
return $ret;
} }
/** /**
* Enable field type modules. * Enable field type modules.
*/ */
function system_update_7027() { function system_update_7027() {
$ret = array();
$module_list = array('text', 'number', 'list', 'options'); $module_list = array('text', 'number', 'list', 'options');
db_delete('system')->condition('type', 'module')->condition('name', $module_list)->execute(); db_delete('system')
->condition('type', 'module')
->condition('name', $module_list)
->execute();
drupal_install_modules($module_list); drupal_install_modules($module_list);
module_enable($module_list); module_enable($module_list);
return $ret;
} }
/** /**
* Rename taxonomy tables. * Rename taxonomy tables.
*/ */
function system_update_7028() { function system_update_7028() {
$ret = array(); db_rename_table('term_data', 'taxonomy_term_data');
db_rename_table($ret, 'term_data', 'taxonomy_term_data'); db_rename_table('term_hierarchy', 'taxonomy_term_hierarchy');
db_rename_table($ret, 'term_hierarchy', 'taxonomy_term_hierarchy'); db_rename_table('term_node', 'taxonomy_term_node');
db_rename_table($ret, 'term_node', 'taxonomy_term_node'); db_rename_table('term_relation', 'taxonomy_term_relation');
db_rename_table($ret, 'term_relation', 'taxonomy_term_relation'); db_rename_table('term_synonym', 'taxonomy_term_synonym');
db_rename_table($ret, 'term_synonym', 'taxonomy_term_synonym'); db_rename_table('vocabulary', 'taxonomy_vocabulary');
db_rename_table($ret, 'vocabulary', 'taxonomy_vocabulary'); db_rename_table('vocabulary_node_types', 'taxonomy_vocabulary_node_type');
db_rename_table($ret, 'vocabulary_node_types', 'taxonomy_vocabulary_node_type');
return $ret;
} }
/** /**
@ -2299,62 +2346,51 @@ function system_update_7028() {
* Preserves legacy behavior from Drupal 6.x. * Preserves legacy behavior from Drupal 6.x.
*/ */
function system_update_7029() { function system_update_7029() {
$ret = array();
db_insert('role_permission') db_insert('role_permission')
->fields(array( ->fields(array(
'rid' => DRUPAL_AUTHENTICATED_RID, 'rid' => DRUPAL_AUTHENTICATED_RID,
'permission' => 'view own unpublished content', 'permission' => 'view own unpublished content',
)) ))
->execute(); ->execute();
return $ret;
} }
/** /**
* Add an index to node_comment_statistics on comment_count. * Add an index to node_comment_statistics on comment_count.
*/ */
function system_update_7030() { function system_update_7030() {
$ret = array(); db_add_index('node_comment_statistics', 'comment_count', array('comment_count'));
db_add_index($ret, 'node_comment_statistics', 'comment_count', array('comment_count'));
return $ret;
} }
/** /**
* Add a missing index on the {menu_router} table. * Add a missing index on the {menu_router} table.
*/ */
function system_update_7031() { function system_update_7031() {
$ret = array(); db_add_index('menu_router', 'tab_root_weight_title', array(array('tab_root', 64), 'weight', 'title'));
db_add_index($ret, 'menu_router', 'tab_root_weight_title', array(array('tab_root', 64), 'weight', 'title'));
return $ret;
} }
/** /**
* Alter field hostname to identifier in the {flood} table. * Alter field hostname to identifier in the {flood} table.
*/ */
function system_update_7032() { function system_update_7032() {
$ret = array(); db_drop_index('flood', 'allow');
db_drop_index($ret, 'flood', 'allow'); db_change_field('flood', 'hostname', 'identifier', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
db_change_field($ret, 'flood', 'hostname', 'identifier', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => '')); db_add_index('flood', 'allow', array('event', 'identifier', 'timestamp'));
db_add_index($ret, 'flood', 'allow', array('event', 'identifier', 'timestamp'));
return $ret;
} }
/** /**
* Move CACHE_AGGRESSIVE to CACHE_NORMAL. * Move CACHE_AGGRESSIVE to CACHE_NORMAL.
*/ */
function system_update_7033() { function system_update_7033() {
$ret = array();
if (variable_get('cache') == 2) { if (variable_get('cache') == 2) {
variable_set('cache', CACHE_NORMAL); variable_set('cache', CACHE_NORMAL);
$ret[] = array('success' => TRUE, 'query' => "Aggressive caching was disabled and replaced with normal caching, please read the page caching section in default.settings.php for more information on how to enable similar functionality."); return t('Aggressive caching was disabled and replaced with normal caching, please read the page caching section in default.settings.php for more information on how to enable similar functionality.');
} }
return $ret;
} }
/** /**
* Migrate the file_downloads setting and create the new {file} table. * Migrate the file_downloads setting and create the new {file} table.
*/ */
function system_update_7034() { function system_update_7034() {
$ret = array();
$files_directory = variable_get('file_directory_path', NULL); $files_directory = variable_get('file_directory_path', NULL);
if (variable_get('file_downloads', 1) == 1) { if (variable_get('file_downloads', 1) == 1) {
// Our default is public, so we don't need to set anything. // Our default is public, so we don't need to set anything.
@ -2438,18 +2474,18 @@ function system_update_7034() {
), ),
'primary key' => array('fid'), 'primary key' => array('fid'),
); );
db_create_table($ret, 'file', $schema['file']);
return $ret; db_create_table('file', $schema['file']);
} }
/** /**
* Migrate upload module files to the new {file} table. * Migrate upload module files to the new {file} table.
*/ */
function system_update_7035() { function system_update_7035() {
$ret = array();
if (!db_table_exists('upload')) { if (!db_table_exists('upload')) {
return $ret; return;
} }
// The old {files} tables still exists. We migrate core data from upload // The old {files} tables still exists. We migrate core data from upload
// module, but any contrib module using it will need to do its own update. // module, but any contrib module using it will need to do its own update.
$result = db_query('SELECT fid, uid, filename, filepath AS uri, filemime, filesize, status, timestamp FROM {files} f INNER JOIN {upload} u ON u.fid = f.fid', array(), array('fetch' => PDO::FETCH_ASSOC)); $result = db_query('SELECT fid, uid, filename, filepath AS uri, filemime, filesize, status, timestamp FROM {files} f INNER JOIN {upload} u ON u.fid = f.fid', array(), array('fetch' => PDO::FETCH_ASSOC));
@ -2459,7 +2495,7 @@ function system_update_7035() {
$basename = variable_get('file_directory_path', conf_path() . '/files'); $basename = variable_get('file_directory_path', conf_path() . '/files');
$scheme = variable_get('file_default_scheme', 'public') . '://'; $scheme = variable_get('file_default_scheme', 'public') . '://';
$fids = array(); $fids = array();
// TODO: does this function need to run in batch mode? // TODO: does this function need to run in batch mode, or should we use a multi-insert?
foreach ($result as $file) { foreach ($result as $file) {
$file['uri'] = $scheme . str_replace($basename, '', $file['uri']); $file['uri'] = $scheme . str_replace($basename, '', $file['uri']);
$file['uri'] = file_stream_wrapper_uri_normalize($file['uri']); $file['uri'] = file_stream_wrapper_uri_normalize($file['uri']);
@ -2467,19 +2503,17 @@ function system_update_7035() {
$fids[] = $file['fid']; $fids[] = $file['fid'];
} }
// TODO: delete the found fids from {files}? // TODO: delete the found fids from {files}?
return $ret;
} }
/** /**
* Split the 'access site in maintenance mode' permission from 'administer site configuration'. * Split the 'access site in maintenance mode' permission from 'administer site configuration'.
*/ */
function system_update_7036() { function system_update_7036() {
$ret = array();
// Get existing roles that can 'administer site configuration'. // Get existing roles that can 'administer site configuration'.
$rids = db_query("SELECT rid FROM {role_permission} WHERE permission = :perm", array(':perm' => 'administer site configuration'))->fetchCol(); $rids = db_query("SELECT rid FROM {role_permission} WHERE permission = :perm", array(':perm' => 'administer site configuration'))->fetchCol();
// None found. // None found.
if (empty($rids)) { if (empty($rids)) {
return $ret; return;
} }
$insert = db_insert('role_permission')->fields(array('rid', 'permission')); $insert = db_insert('role_permission')->fields(array('rid', 'permission'));
foreach ($rids as $rid) { foreach ($rids as $rid) {
@ -2493,26 +2527,20 @@ function system_update_7036() {
// Remove obsolete variable 'site_offline_message'. // Remove obsolete variable 'site_offline_message'.
// @see update_fix_d7_requirements(). // @see update_fix_d7_requirements().
variable_del('site_offline_message'); variable_del('site_offline_message');
return $ret;
} }
/** /**
* Rename {box} table to {block_custom}. * Rename {box} table to {block_custom}.
*/ */
function system_update_7037() { function system_update_7037() {
$ret = array(); db_rename_table('box', 'block_custom');
db_rename_table($ret, 'box', 'block_custom');
return $ret;
} }
/** /**
* Rename action description to label. * Rename action description to label.
*/ */
function system_update_7038() { function system_update_7038() {
$ret = array(); db_change_field('actions', 'description', 'label', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'));
db_change_field($ret, 'actions', 'description', 'label', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'));
return $ret;
} }
/** /**

View File

@ -293,7 +293,6 @@ function taxonomy_schema() {
* Add vocabulary machine_name column. * Add vocabulary machine_name column.
*/ */
function taxonomy_update_7002() { function taxonomy_update_7002() {
$ret = array();
$field = array( $field = array(
'type' => 'varchar', 'type' => 'varchar',
'length' => 255, 'length' => 255,
@ -302,7 +301,7 @@ function taxonomy_update_7002() {
'description' => 'The vocabulary machine name.', 'description' => 'The vocabulary machine name.',
); );
db_add_field($ret, 'taxonomy_vocabulary', 'machine_name', $field); db_add_field('taxonomy_vocabulary', 'machine_name', $field);
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$machine_name = 'vocabulary_' . $vid; $machine_name = 'vocabulary_' . $vid;
@ -312,7 +311,6 @@ function taxonomy_update_7002() {
->execute(); ->execute();
field_attach_create_bundle($machine_name); field_attach_create_bundle($machine_name);
} }
return $ret;
} }
/** /**
@ -322,8 +320,5 @@ function taxonomy_update_7002() {
* itself is retained to allow for data to be upgraded. * itself is retained to allow for data to be upgraded.
*/ */
function taxonomy_update_7003() { function taxonomy_update_7003() {
$ret = array(); db_drop_field('taxonomy_vocabulary', 'relations');
db_drop_field($ret, 'taxonomy_vocabulary', 'relations');
return $ret;
} }

View File

@ -197,16 +197,14 @@ function tracker_update_7000() {
), ),
); );
$ret = array();
foreach ($schema as $name => $table) { foreach ($schema as $name => $table) {
db_create_table($ret, $name, $table); db_create_table($name, $table);
} }
$max_nid = db_query('SELECT MAX(nid) FROM {node}')->fetchField(); $max_nid = db_query('SELECT MAX(nid) FROM {node}')->fetchField();
if ($max_nid != 0) { if ($max_nid != 0) {
variable_set('tracker_index_nid', $max_nid); variable_set('tracker_index_nid', $max_nid);
} }
return $ret;
} }
/** /**

View File

@ -54,13 +54,15 @@ function trigger_schema() {
* Adds operation names to the hook names and drops the "op" field. * Adds operation names to the hook names and drops the "op" field.
*/ */
function trigger_update_7000() { function trigger_update_7000() {
$ret = array();
$result = db_query("SELECT hook, op, aid FROM {trigger_assignments} WHERE op <> ''"); $result = db_query("SELECT hook, op, aid FROM {trigger_assignments} WHERE op <> ''");
while ($row = db_fetch_object($result)) { foreach ($result as $record) {
$ret[] = update_sql("UPDATE {trigger_assignments} SET hook = '%s' WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $row->hook . '_' . $row->op, $row->hook, $row->op, $row->aid); db_update('trigger_assignments')
->fields(array('hook' => $record->hook . '_' . $record->op))
->condition('hook', $record->hook)
->condition('op', $record->op)
->condition('aid', $record->aid)
->execute();
} }
$ret[] = update_sql("ALTER TABLE {trigger_assignments} DROP op"); db_drop_field('trigger_assignments', 'op');
return $ret;
} }

View File

@ -82,7 +82,6 @@ function upload_schema() {
* Migrate upload module files from {files} to {file}. * Migrate upload module files from {files} to {file}.
*/ */
function upload_update_7000(&$sandbox) { function upload_update_7000(&$sandbox) {
$ret = array();
/* /*
TODO: Fix the updates. This is broken. See http://drupal.org/node/329301#comment-1404336 TODO: Fix the updates. This is broken. See http://drupal.org/node/329301#comment-1404336
@ -128,8 +127,6 @@ function upload_update_7000(&$sandbox) {
// Indicate our current progress to the batch update system. If there's no // Indicate our current progress to the batch update system. If there's no
// max value then there's nothing to update and we're finished. // max value then there's nothing to update and we're finished.
$ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
return $ret;
} }

View File

@ -255,12 +255,12 @@ function user_schema() {
* lengthy process, and is performed batch-wise. * lengthy process, and is performed batch-wise.
*/ */
function user_update_7000(&$sandbox) { function user_update_7000(&$sandbox) {
$ret = array('#finished' => 0); $sandbox['#finished'] = 0;
// Lower than DRUPAL_HASH_COUNT to make the update run at a reasonable speed. // Lower than DRUPAL_HASH_COUNT to make the update run at a reasonable speed.
$hash_count_log2 = 11; $hash_count_log2 = 11;
// Multi-part update. // Multi-part update.
if (!isset($sandbox['user_from'])) { if (!isset($sandbox['user_from'])) {
db_change_field($ret, 'users', 'pass', 'pass', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => '')); db_change_field('users', 'pass', 'pass', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
$sandbox['user_from'] = 0; $sandbox['user_from'] = 0;
$sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField(); $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
} }
@ -283,14 +283,13 @@ function user_update_7000(&$sandbox) {
->execute(); ->execute();
} }
} }
$ret['#finished'] = $sandbox['user_from']/$sandbox['user_count']; $sandbox['#finished'] = $sandbox['user_from']/$sandbox['user_count'];
$sandbox['user_from'] += $count; $sandbox['user_from'] += $count;
if (!$has_rows) { if (!$has_rows) {
$ret['#finished'] = 1; $sandbox['#finished'] = 1;
$ret[] = array('success' => TRUE, 'query' => "UPDATE {users} SET pass = 'U' . user_hash_password(pass) WHERE uid > 0"); return t('User passwords rehashed to improve security');
} }
} }
return $ret;
} }
/** /**
@ -300,23 +299,20 @@ function user_update_7000(&$sandbox) {
*/ */
function user_update_7001() { function user_update_7001() {
$ret = array(); db_drop_field('users', 'threshold');
db_drop_field($ret, 'users', 'threshold'); db_drop_field('users', 'mode');
db_drop_field($ret, 'users', 'mode'); db_drop_field('users', 'sort');
db_drop_field($ret, 'users', 'sort');
return $ret;
} }
/** /**
* Convert user time zones from time zone offsets to time zone names. * Convert user time zones from time zone offsets to time zone names.
*/ */
function user_update_7002(&$sandbox) { function user_update_7002(&$sandbox) {
$ret = array('#finished' => 0); $sandbox['#finished'] = 0;
// Multi-part update. // Multi-part update.
if (!isset($sandbox['user_from'])) { if (!isset($sandbox['user_from'])) {
db_change_field($ret, 'users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE)); db_change_field('users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE));
$sandbox['user_from'] = 0; $sandbox['user_from'] = 0;
$sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField(); $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
$sandbox['user_not_migrated'] = 0; $sandbox['user_not_migrated'] = 0;
@ -355,25 +351,30 @@ function user_update_7002(&$sandbox) {
} }
} }
if ($timezone) { if ($timezone) {
db_query("UPDATE {users} SET timezone = :timezone WHERE uid = :uid", array(':timezone' => $timezone, ':uid' => $account->uid)); db_update('users')
->fields(array('timezone' => $timezone))
->condition('uid', $account->uid)
->execute();
} }
else { else {
$sandbox['user_not_migrated']++; $sandbox['user_not_migrated']++;
db_query("UPDATE {users} SET timezone = NULL WHERE uid = :uid", array(':uid' => $account->uid)); db_update('users')
->fields(array('timezone', NULL))
->condition('uid', $account->uid)
->execute();
} }
$sandbox['user_from']++; $sandbox['user_from']++;
} }
$ret['#finished'] = $sandbox['user_from'] / $sandbox['user_count']; $sandbox['#finished'] = $sandbox['user_from'] / $sandbox['user_count'];
if ($sandbox['user_from'] == $sandbox['user_count']) { if ($sandbox['user_from'] == $sandbox['user_count']) {
$ret[] = array('success' => TRUE, 'query' => "Migrate user time zones.");
if ($sandbox['user_not_migrated'] > 0) { if ($sandbox['user_not_migrated'] > 0) {
variable_set('empty_timezone_message', 1); variable_set('empty_timezone_message', 1);
drupal_set_message('Some user time zones have been emptied and need to be set to the correct values. Use the new ' . l('time zone options', 'admin/config/regional/settings') . ' to choose whether to remind users at login to set the correct time zone.', 'warning'); drupal_set_message('Some user time zones have been emptied and need to be set to the correct values. Use the new ' . l('time zone options', 'admin/config/regional/settings') . ' to choose whether to remind users at login to set the correct time zone.', 'warning');
} }
return t('Migrated user time zones');
} }
} }
return $ret;
} }
/** /**
@ -384,7 +385,6 @@ function user_update_7002(&$sandbox) {
* which is the same as the 'user_cancel_reassign' method now. * which is the same as the 'user_cancel_reassign' method now.
*/ */
function user_update_7003() { function user_update_7003() {
$ret = array();
// Set the default account cancellation method. // Set the default account cancellation method.
variable_set('user_cancel_method', 'user_cancel_reassign'); variable_set('user_cancel_method', 'user_cancel_reassign');
// Re-assign notification setting. // Re-assign notification setting.
@ -401,14 +401,12 @@ function user_update_7003() {
variable_set('user_mail_status_canceled_body', $setting); variable_set('user_mail_status_canceled_body', $setting);
variable_del('user_mail_status_deleted_body'); variable_del('user_mail_status_deleted_body');
} }
return $ret;
} }
/** /**
* Add the user's pictures to the {file} table and make them managed files. * Add the user's pictures to the {file} table and make them managed files.
*/ */
function user_update_7004(&$sandbox) { function user_update_7004(&$sandbox) {
$ret = array();
$picture_field = array( $picture_field = array(
'type' => 'int', 'type' => 'int',
@ -422,7 +420,7 @@ function user_update_7004(&$sandbox) {
// update. // update.
if (!db_column_exists('users', 'picture_fid')) { if (!db_column_exists('users', 'picture_fid')) {
// Add a new field for the fid. // Add a new field for the fid.
db_add_field($ret, 'users', 'picture_fid', $picture_field); db_add_field('users', 'picture_fid', $picture_field);
} }
// Initialize batch update information. // Initialize batch update information.
@ -469,16 +467,14 @@ function user_update_7004(&$sandbox) {
// Indicate our current progress to the batch update system. If there's no // Indicate our current progress to the batch update system. If there's no
// max value then there's nothing to update and we're finished. // max value then there's nothing to update and we're finished.
$ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
// When we're finished, drop the old picture field and rename the new one to // When we're finished, drop the old picture field and rename the new one to
// replace it. // replace it.
if (isset($ret['#finished']) && $ret['#finished'] == 1) { if (isset($sandbox['#finished']) && $sandbox['#finished'] == 1) {
db_drop_field($ret, 'user', 'picture'); db_drop_field('user', 'picture');
db_change_field($ret, 'user', 'picture_fid', 'picture', $picture_field); db_change_field('user', 'picture_fid', 'picture', $picture_field);
} }
return $ret;
} }
/** /**