Problem/Motivation
Over in #3232699: SQLite schema introspection implementation struggles with NULL default values we're fixing some SQLite default value bugs and the fact that MySQL 5.7 doesn't support default values for blob, text and json fields came to light. Drupal has gone through quite a bit of pain over the years with this. See:
- #1747358: set 'initial' by default for text field
- #300971: Longtexts cannot have default values (install.php fatal error)
- #299292: Defaults on text fields prevent installation (HEAD installation is broken)
The good news is that MySQL changed its handling of default values to allow blob, text or JSON in 8.0.13: https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html (thanks @longwave for this info).
The bad news is that according to the docs...
The BLOB, TEXT, GEOMETRY, and JSON data types can be assigned a default value only if the value is written as an expression, even if the expression value is a literal:
This is permitted (literal default specified as expression):
CREATE TABLE t2 (b BLOB DEFAULT ('abc'));
This produces an error (literal default not specified as expression):
CREATE TABLE t2 (b BLOB DEFAULT 'abc');
So we're going to need to fix up \Drupal\Core\Database\Driver\mysql\Schema::createFieldSql() to cope with this.
Steps to reproduce
Do something like...
// Create the table.
$table_specification = [
'description' => 'Test table.',
'fields' => [
'column1' => [
'type' => 'int',
'default' => NULL,
],
'column2' => [
'type' => 'text',
'default' => 'a literal value',
],
],
];
$schema->createTable('test table', $table_specification);
Proposed resolution
Fix up \Drupal\Core\Database\Driver\mysql\Schema::createFieldSql() to cope with this.
Determine what to do about MySQL 5.7. Default values on text fields is supported by the other db engines we support.