Issue #2999678 by andypost: Properly deprecate db_field_exists

8.7.x
Nathaniel Catchpole 2018-09-17 11:49:33 +01:00
parent e3587aae73
commit 05f5a67660
4 changed files with 18 additions and 5 deletions

View File

@ -698,6 +698,7 @@ function db_table_exists($table) {
* @see \Drupal\Core\Database\Schema::fieldExists()
*/
function db_field_exists($table, $field) {
@trigger_error('db_field_exists() 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 fieldExists() on it. For example, $injected_database->schema()->fieldExists($table, $field). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->fieldExists($table, $field);
}

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\content_translation\Kernel;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
/**
@ -31,9 +32,10 @@ class ContentTranslationSettingsApiTest extends KernelTestBase {
*/
public function testSettingsApi() {
$this->container->get('content_translation.manager')->setEnabled('entity_test_mul', 'entity_test_mul', TRUE);
$schema = Database::getConnection()->schema();
$result =
db_field_exists('entity_test_mul_property_data', 'content_translation_source') &&
db_field_exists('entity_test_mul_property_data', 'content_translation_outdated');
$schema->fieldExists('entity_test_mul_property_data', 'content_translation_source') &&
$schema->fieldExists('entity_test_mul_property_data', 'content_translation_outdated');
$this->assertTrue($result, 'Schema updates correctly performed.');
}

View File

@ -236,6 +236,15 @@ class DatabaseLegacyTest extends DatabaseTestBase {
$this->assertFalse($this->connection->schema()->fieldExists('test', 'age'));
}
/**
* Tests deprecation of the db_field_exists() function.
*
* @expectedDeprecation db_field_exists() 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 fieldExists() on it. For example, $injected_database->schema()->fieldExists($table, $field). See https://www.drupal.org/node/2993033
*/
public function testDbFieldExists() {
$this->assertTrue(db_field_exists('test', 'age'));
}
/**
* Tests deprecation of the db_field_names() function.
*

View File

@ -42,11 +42,12 @@ class RegressionTest extends DatabaseTestBase {
}
/**
* Tests the db_field_exists() function.
* Tests the \Drupal\Core\Database\Schema::fieldExists() method.
*/
public function testDBFieldExists() {
$this->assertSame(TRUE, db_field_exists('test', 'name'), 'Returns true for existent column.');
$this->assertSame(FALSE, db_field_exists('test', 'nosuchcolumn'), 'Returns false for nonexistent column.');
$schema = $this->connection->schema();
$this->assertSame(TRUE, $schema->fieldExists('test', 'name'), 'Returns true for existent column.');
$this->assertSame(FALSE, $schema->fieldExists('test', 'nosuchcolumn'), 'Returns false for nonexistent column.');
}
/**