Issue #2994694 by mitrpaka, hardik.p, Vidushi Mehta, Hardik Rawal, mondrake, longwave: Properly deprecate db_driver, db_escape_field, db_escape_table, db_rename_table, db_drop_index, db_drop_unique_key, db_add_unique_key, db_drop_primary_key, db_add_primary_key

8.7.x
Nathaniel Catchpole 2018-08-31 10:05:49 +09:00
parent df1de90392
commit 617d5b7bed
4 changed files with 99 additions and 5 deletions

View File

@ -365,6 +365,7 @@ function db_set_active($key = 'default') {
* @see \Drupal\Core\Database\Connection::escapeTable()
*/
function db_escape_table($table) {
@trigger_error('db_escape_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call escapeTable() on it. For example, $injected_database->escapeTable($table). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->escapeTable($table);
}
@ -381,12 +382,13 @@ function db_escape_table($table) {
*
* @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get
* a database connection injected into your service from the container and
* call escapeTable() on it. For example,
* $injected_database->escapeTable($table);
* call escapeField() on it. For example,
* $injected_database->escapeField($field);
*
* @see \Drupal\Core\Database\Connection::escapeField()
*/
function db_escape_field($field) {
@trigger_error('db_escape_field() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call escapeField() on it. For example, $injected_database->escapeField($field). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->escapeField($field);
}
@ -444,6 +446,7 @@ function db_like($string) {
* @see \Drupal\Core\Database\Connection::driver()
*/
function db_driver() {
@trigger_error('db_driver() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call driver() on it. For example, $injected_database->driver($string). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->driver();
}
@ -727,6 +730,7 @@ function db_find_tables($table_expression) {
* @see \Drupal\Core\Database\Schema::renameTable()
*/
function db_rename_table($table, $new_name) {
@trigger_error('db_rename_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call renameTable() on it. For example, $injected_database->schema()->renameTable($table, $new_name). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->renameTable($table, $new_name);
}
@ -860,6 +864,7 @@ function db_field_set_no_default($table, $field) {
* @see \Drupal\Core\Database\Schema::addPrimaryKey()
*/
function db_add_primary_key($table, $fields) {
@trigger_error('db_add_primary_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addPrimaryKey() on it. For example, $injected_database->schema()->addPrimaryKey($table, $fields). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->addPrimaryKey($table, $fields);
}
@ -881,6 +886,7 @@ function db_add_primary_key($table, $fields) {
* @see \Drupal\Core\Database\Schema::dropPrimaryKey()
*/
function db_drop_primary_key($table) {
@trigger_error('db_drop_primary_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropPrimaryKey() on it. For example, $injected_database->schema()->dropPrimaryKey($table). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->dropPrimaryKey($table);
}
@ -902,6 +908,7 @@ function db_drop_primary_key($table) {
* @see \Drupal\Core\Database\Schema::addUniqueKey()
*/
function db_add_unique_key($table, $name, $fields) {
@trigger_error('db_add_unique_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addUniqueKey() on it. For example, $injected_database->schema()->addUniqueKey($table, $name, $fields). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->addUniqueKey($table, $name, $fields);
}
@ -925,6 +932,7 @@ function db_add_unique_key($table, $name, $fields) {
* @see \Drupal\Core\Database\Schema::dropUniqueKey()
*/
function db_drop_unique_key($table, $name) {
@trigger_error('db_drop_unique_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropUniqueKey() on it. For example, $injected_database->schema()->dropUniqueKey($table, $name). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->dropUniqueKey($table, $name);
}
@ -975,6 +983,7 @@ function db_add_index($table, $name, $fields, array $spec) {
* @see \Drupal\Core\Database\Schema::dropIndex()
*/
function db_drop_index($table, $name) {
@trigger_error('db_drop_index() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropIndex() on it. For example, $injected_database->schema()->dropIndex($table, $name). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->dropIndex($table, $name);
}
@ -1003,7 +1012,7 @@ function db_drop_index($table, $name) {
* key. The correct sequence is:
* @code
* $schema = \Drupal::database()->schema();
* db_drop_primary_key('foo');
* $schema->dropPrimaryKey('foo');
* $schema->changeField('foo', 'bar', 'bar',
* array('type' => 'serial', 'not null' => TRUE),
* array('primary key' => array('bar')));

View File

@ -38,7 +38,8 @@ class TableSortExtender extends SelectExtender {
$this->header = $header;
$ts = $this->init();
if (!empty($ts['sql'])) {
// Based on code from db_escape_table(), but this can also contain a dot.
// Based on code from \Drupal\Core\Database\Connection::escapeTable(),
// but this can also contain a dot.
$field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
// orderBy() will ensure that only ASC/DESC values are accepted, so we

View File

@ -559,7 +559,7 @@ abstract class Schema implements PlaceholderInterface {
* primary key. The correct sequence is:
* @code
* $schema = \Drupal::database()->schema();
* db_drop_primary_key('foo');
* $schema->dropPrimaryKey('foo');
* $schema->changeField('foo', 'bar', 'bar',
* array('type' => 'serial', 'not null' => TRUE),
* array('primary key' => array('bar')));

View File

@ -207,6 +207,90 @@ class DatabaseLegacyTest extends DatabaseTestBase {
$this->assertSame($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
}
/**
* Tests deprecation of the db_driver() function.
*
* @expectedDeprecation db_driver() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call driver() on it. For example, $injected_database->driver($string). See https://www.drupal.org/node/2993033
*/
public function testDbDriver() {
$this->assertNotNull(db_driver());
}
/**
* Tests deprecation of the db_escape_field() function.
*
* @expectedDeprecation db_escape_field() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call escapeField() on it. For example, $injected_database->escapeField($field). See https://www.drupal.org/node/2993033
*/
public function testDbEscapeField() {
$this->assertNotNull(db_escape_field('test'));
}
/**
* Tests deprecation of the db_escape_table() function.
*
* @expectedDeprecation db_escape_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call escapeTable() on it. For example, $injected_database->escapeTable($table). See https://www.drupal.org/node/2993033
*/
public function testDbEscapeTable() {
$this->assertNotNull(db_escape_table('test'));
}
/**
* Tests deprecation of the db_rename_table() function.
*
* @expectedDeprecation db_rename_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call renameTable() on it. For example, $injected_database->schema()->renameTable($table, $new_name). See https://www.drupal.org/node/2993033
*/
public function testDbRenameTable() {
$this->assertTrue($this->connection->schema()->tableExists('test'));
$this->assertTrue(db_rename_table('test', 'test_rename'));
$this->assertTrue($this->connection->schema()->tableExists('test_rename'));
}
/**
* Tests deprecation of the db_drop_index() function.
*
* @expectedDeprecation db_drop_index() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropIndex() on it. For example, $injected_database->schema()->dropIndex($table, $name). See https://www.drupal.org/node/2993033
*/
public function testDbDropIndex() {
$this->assertFalse(db_drop_index('test', 'no_such_index'));
}
/**
* Tests deprecation of the db_drop_unique_key() function.
*
* @expectedDeprecation db_drop_unique_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropUniqueKey() on it. For example, $injected_database->schema()->dropUniqueKey($table, $name). See https://www.drupal.org/node/2993033
*/
public function testDbDropUniqueKey() {
$this->assertTrue(db_drop_unique_key('test', 'name'));
}
/**
* Tests deprecation of the db_add_unique_key() function.
*
* @expectedDeprecation db_add_unique_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addUniqueKey() on it. For example, $injected_database->schema()->addUniqueKey($table, $name, $fields). See https://www.drupal.org/node/2993033
*/
public function testDbAddUniqueKey() {
db_add_unique_key('test', 'age', ['age']);
}
/**
* Tests deprecation of the db_drop_primary_key() function.
*
* @expectedDeprecation db_drop_primary_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropPrimaryKey() on it. For example, $injected_database->schema()->dropPrimaryKey($table). See https://www.drupal.org/node/2993033
*/
public function testDbDropPrimaryKey() {
$this->assertTrue(db_drop_primary_key('test_people'));
}
/**
* Tests deprecation of the db_add_primary_key() function.
*
* @expectedDeprecation db_add_primary_key() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addPrimaryKey() on it. For example, $injected_database->schema()->addPrimaryKey($table, $fields). See https://www.drupal.org/node/2993033
*/
public function testDbAddPrimaryKey() {
$this->connection->schema()->dropPrimaryKey('test_people');
db_add_primary_key('test_people', ['job']);
}
/**
* Tests the db_update() function.
*