Commit ca4076c0 authored by catch's avatar catch

Issue #2993663 by mondrake, andypost: Deprecate Schema::fieldSetDefault and...

Issue #2993663 by mondrake, andypost: Deprecate Schema::fieldSetDefault and Schema::fieldSetNoDefault, they're not used
parent 9f3ca2e3
......@@ -823,12 +823,14 @@ function db_drop_field($table, $field) {
*
* @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, get
* its schema driver, and call fieldSetDefault() on it. For example,
* $injected_database->schema()->fieldSetDefault($table, $field, $default);
* its schema driver, and call changeField() on it, passing a full field
* specification. For example,
* $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
*
* @see \Drupal\Core\Database\Schema::fieldSetDefault()
* @see \Drupal\Core\Database\Schema::changeField()
*/
function db_field_set_default($table, $field, $default) {
@trigger_error('db_field_set_default() 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 changeField() on it, passing a full field specification. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->fieldSetDefault($table, $field, $default);
}
......@@ -842,12 +844,14 @@ function db_field_set_default($table, $field, $default) {
*
* @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, get
* its schema driver, and call fieldSetNoDefault() on it. For example,
* $injected_database->schema()->fieldSetNoDefault($table, $field);
* its schema driver, and call changeField() on it, passing a full field
* specification. For example,
* $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
*
* @see \Drupal\Core\Database\Schema::fieldSetNoDefault()
* @see \Drupal\Core\Database\Schema::changeField()
*/
function db_field_set_no_default($table, $field) {
@trigger_error('db_field_set_no_default() 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 changeField() on it, passing a full field specification. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
return Database::getConnection()->schema()->fieldSetNoDefault($table, $field);
}
......
......@@ -490,6 +490,7 @@ public function dropField($table, $field) {
* {@inheritdoc}
*/
public function fieldSetDefault($table, $field, $default) {
@trigger_error('fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035', E_USER_DEPRECATED);
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
}
......@@ -501,6 +502,7 @@ public function fieldSetDefault($table, $field, $default) {
* {@inheritdoc}
*/
public function fieldSetNoDefault($table, $field) {
@trigger_error('fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035', E_USER_DEPRECATED);
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
}
......
......@@ -619,6 +619,7 @@ public function dropField($table, $field) {
* {@inheritdoc}
*/
public function fieldSetDefault($table, $field, $default) {
@trigger_error('fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035', E_USER_DEPRECATED);
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
}
......@@ -632,6 +633,7 @@ public function fieldSetDefault($table, $field, $default) {
* {@inheritdoc}
*/
public function fieldSetNoDefault($table, $field) {
@trigger_error('fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035', E_USER_DEPRECATED);
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
}
......@@ -839,7 +841,7 @@ public function changeField($table, $field, $field_new, $spec, $new_keys = []) {
}
// Remove old default.
$this->fieldSetNoDefault($table, $field);
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT');
// Convert field type.
// Usually, we do this via a simple typecast 'USING fieldname::type'. But
......@@ -899,7 +901,7 @@ public function changeField($table, $field, $field_new, $spec, $new_keys = []) {
// Add default if necessary.
if (isset($spec['default'])) {
$this->fieldSetDefault($table, $field_new, $spec['default']);
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field_new . '" SET DEFAULT ' . $this->escapeDefaultValue($spec['default']));
}
// Change description if necessary.
......
......@@ -778,6 +778,7 @@ protected function findPrimaryKeyColumns($table) {
* {@inheritdoc}
*/
public function fieldSetDefault($table, $field, $default) {
@trigger_error('fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035', E_USER_DEPRECATED);
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
}
......@@ -793,6 +794,7 @@ public function fieldSetDefault($table, $field, $default) {
* {@inheritdoc}
*/
public function fieldSetNoDefault($table, $field) {
@trigger_error('fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035', E_USER_DEPRECATED);
if (!$this->fieldExists($table, $field)) {
throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
}
......
......@@ -352,6 +352,11 @@ public function fieldExists($table, $column) {
*
* @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
* If the specified table or field doesn't exist.
*
* @deprecated as of Drupal 8.7.x, will be removed in Drupal 9.0.0. Instead,
* call ::changeField() passing a full field specification.
*
* @see ::changeField()
*/
abstract public function fieldSetDefault($table, $field, $default);
......@@ -365,6 +370,11 @@ public function fieldExists($table, $column) {
*
* @throws \Drupal\Core\Database\SchemaObjectDoesNotExistException
* If the specified table or field doesn't exist.
*
* @deprecated as of Drupal 8.7.x, will be removed in Drupal 9.0.0. Instead,
* call ::changeField() passing a full field specification.
*
* @see ::changeField()
*/
abstract public function fieldSetNoDefault($table, $field);
......
......@@ -8,6 +8,7 @@
use Drupal\Core\Database\Query\Update;
use Drupal\Core\Database\Transaction;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseException;
/**
* Deprecation tests cases for the database layer.
......@@ -114,6 +115,79 @@ public function testDbChangeField() {
db_change_field('test', 'name', 'nosuchcolumn', $spec);
}
/**
* Tests deprecation of the db_field_set_default() function.
*
* @expectedDeprecation db_field_set_default() 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 changeField() on it, passing a full field specification. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033
* @expectedDeprecation fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
*/
public function testDbFieldSetDefault() {
db_field_set_default('test', 'job', 'baz');
}
/**
* Tests deprecation of the db_field_set_no_default() function.
*
* @expectedDeprecation db_field_set_no_default() 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 changeField() on it, passing a full field specification. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033
* @expectedDeprecation fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
*/
public function testDbFieldSetNoDefault() {
db_field_set_no_default('test_null', 'age');
}
/**
* Tests Schema::fieldSetDefault and Schema::fieldSetNoDefault.
*
* @expectedDeprecation fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
* @expectedDeprecation fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
*/
public function testSchemaFieldDefaultChange() {
// Create a table.
$table_specification = [
'description' => 'Schema table description.',
'fields' => [
'id' => [
'type' => 'int',
'default' => NULL,
],
'test_field' => [
'type' => 'int',
'not null' => TRUE,
'description' => 'Test field',
],
],
];
$this->connection->schema()->createTable('test_table', $table_specification);
// An insert without a value for the column 'test_field' should fail.
try {
$this->connection->insert('test_table')->fields(['id' => 1])->execute();
$this->fail('Expected DatabaseException, none was thrown.');
}
catch (DatabaseException $e) {
$this->assertEquals(0, $this->connection->select('test_table')->countQuery()->execute()->fetchField());
}
// Add a default value to the column.
$this->connection->schema()->fieldSetDefault('test_table', 'test_field', 0);
// The insert should now succeed.
$this->connection->insert('test_table')->fields(['id' => 1])->execute();
$this->assertEquals(1, $this->connection->select('test_table')->countQuery()->execute()->fetchField());
// Remove the default.
$this->connection->schema()->fieldSetNoDefault('test_table', 'test_field');
// The insert should fail again.
try {
$this->connection->insert('test_table')->fields(['id' => 2])->execute();
$this->fail('Expected DatabaseException, none was thrown.');
}
catch (DatabaseException $e) {
$this->assertEquals(1, $this->connection->select('test_table')->countQuery()->execute()->fetchField());
}
}
/**
* Tests deprecation of the db_transaction() function.
*
......
......@@ -109,12 +109,12 @@ public function testSchema() {
$this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
// Add a default value to the column.
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
$this->schema->changeField('test_table', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE, 'default' => 0]);
// The insert should now succeed.
$this->assertTrue($this->tryInsert(), 'Insert with a default succeeded.');
// Remove the default.
$this->schema->fieldSetNoDefault('test_table', 'test_field');
$this->schema->changeField('test_table', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE]);
// The insert should fail again.
$this->assertFalse($this->tryInsert(), 'Insert without a default failed.');
......@@ -135,7 +135,7 @@ public function testSchema() {
$this->assertTrue($index_exists, 'Index was renamed.');
// We need the default so that we can insert after the rename.
$this->schema->fieldSetDefault('test_table2', 'test_field', 0);
$this->schema->changeField('test_table2', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE, 'default' => 0]);
$this->assertFalse($this->tryInsert(), 'Insert into the old table failed.');
$this->assertTrue($this->tryInsert('test_table2'), 'Insert into the new table succeeded.');
......@@ -149,7 +149,7 @@ public function testSchema() {
// Recreate the table.
$this->schema->createTable('test_table', $table_specification);
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
$this->schema->changeField('test_table', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE, 'default' => 0]);
$this->schema->addField('test_table', 'test_serial', ['type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Added column description.']);
// Assert that the column comment has been set.
......@@ -173,7 +173,7 @@ public function testSchema() {
// Test adding a serial field to an existing table.
$this->schema->dropTable('test_table');
$this->schema->createTable('test_table', $table_specification);
$this->schema->fieldSetDefault('test_table', 'test_field', 0);
$this->schema->changeField('test_table', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE, 'default' => 0]);
$this->schema->addField('test_table', 'test_serial', ['type' => 'serial', 'not null' => TRUE], ['primary key' => ['test_serial']]);
// Test the primary key columns.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment