Issue #2993577 by mondrake, voleger, andypost: Properly deprecate db_add_field, db_drop_field, db_field_names

8.7.x
Nathaniel Catchpole 2018-08-23 14:31:16 +09:00
parent 5cbcd6d33d
commit 53c4c3c27e
2 changed files with 39 additions and 0 deletions

View File

@ -607,6 +607,7 @@ function db_create_table($name, $table) {
* @see \Drupal\Core\Database\Schema::fieldNames()
*/
function db_field_names($fields) {
@trigger_error('db_field_names() 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 fieldNames() on it. For example, $injected_database->schema()->fieldNames($fields). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->fieldNames($fields);
}
@ -764,6 +765,7 @@ function db_drop_table($table) {
* @see \Drupal\Core\Database\Schema::changeField()
*/
function db_add_field($table, $field, $spec, $keys_new = []) {
@trigger_error('db_add_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, get its schema driver, and call addField() on it. For example, $injected_database->schema()->addField($table, $field, $spec, $keys_new). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->addField($table, $field, $spec, $keys_new);
}
@ -787,6 +789,7 @@ function db_add_field($table, $field, $spec, $keys_new = []) {
* @see \Drupal\Core\Database\Schema::dropField()
*/
function db_drop_field($table, $field) {
@trigger_error('db_drop_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, get its schema driver, and call dropField() on it. For example, $injected_database->schema()->dropField($table, $field). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->dropField($table, $field);
}

View File

@ -78,4 +78,40 @@ class DatabaseLegacyTest extends DatabaseTestBase {
$this->assertFalse(Database::isActiveConnection(), 'Database connection is not active');
}
/**
* Tests deprecation of the db_add_field() function.
*
* @expectedDeprecation db_add_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, get its schema driver, and call addField() on it. For example, $injected_database->schema()->addField($table, $field, $spec, $keys_new). See https://www.drupal.org/node/2993033
*/
public function testDbAddField() {
$this->assertFalse($this->connection->schema()->fieldExists('test', 'anint'));
db_add_field('test', 'anint', [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Added int column.',
]);
$this->assertTrue($this->connection->schema()->fieldExists('test', 'anint'));
}
/**
* Tests deprecation of the db_drop_field() function.
*
* @expectedDeprecation db_drop_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, get its schema driver, and call dropField() on it. For example, $injected_database->schema()->dropField($table, $field). See https://www.drupal.org/node/2993033
*/
public function testDbDropField() {
$this->assertTrue($this->connection->schema()->fieldExists('test', 'age'));
$this->assertTrue(db_drop_field('test', 'age'));
$this->assertFalse($this->connection->schema()->fieldExists('test', 'age'));
}
/**
* Tests deprecation of the db_field_names() function.
*
* @expectedDeprecation db_field_names() 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 fieldNames() on it. For example, $injected_database->schema()->fieldNames($fields). See https://www.drupal.org/node/2993033
*/
public function testDbFieldNames() {
$this->assertSame(['test_field'], db_field_names(['test_field']));
}
}