Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider\ConnectionCollationMetadataProvider;
use Doctrine\DBAL\Platforms\MySQL\DefaultTableOptions;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\DefaultExpression\CurrentDate;
use Doctrine\DBAL\Schema\DefaultExpression\CurrentTime;
use Doctrine\DBAL\Schema\DefaultExpression\CurrentTimestamp;
use Doctrine\DBAL\Types\Type;

use function array_change_key_case;
Expand Down Expand Up @@ -187,10 +190,12 @@ protected function _getPortableTableColumnDefinition(array $tableColumn): Column
break;
}

if ($this->platform instanceof MariaDBPlatform) {
$columnDefault = $this->getMariaDBColumnDefault($this->platform, $tableColumn['default']);
if ($tableColumn['default'] === null) {
$columnDefault = null;
} elseif ($this->platform instanceof MariaDBPlatform) {
$columnDefault = $this->parseMariaDBColumnDefault($tableColumn['default']);
} else {
$columnDefault = $tableColumn['default'];
$columnDefault = $this->parseMySQLColumnDefault($dbType, $tableColumn['default']);
}

$options = [
Expand Down Expand Up @@ -228,6 +233,19 @@ private function parseEnumExpression(string $expression): array
);
}

/** @link https://dev.mysql.com/doc/refman/8.4/en/timestamp-initialization.html */
private function parseMySQLColumnDefault(string $type, string $default): string|DefaultExpression
{
// There is no way to tell whether the "CURRENT_TIMESTAMP" value represents a string or an expression in MySQL
// schema introspection results. We rely on the fact that the "CURRENT_TIMESTAMP" expression is only supported
// for the "DATETIME" and "TIMESTAMP" data types and represent this value as an expression only in these cases.
if (($type === 'datetime' || $type === 'timestamp') && $default === 'CURRENT_TIMESTAMP') {
return new CurrentTimestamp();
}

return $default;
}

/**
* Return Doctrine/Mysql-compatible column default values for MariaDB 10.2.7+ servers.
*
Expand All @@ -242,11 +260,11 @@ private function parseEnumExpression(string $expression): array
* @link https://mariadb.com/kb/en/library/information-schema-columns-table/
* @link https://jira.mariadb.org/browse/MDEV-13132
*
* @param string|null $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7
* @param string $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7
*/
private function getMariaDBColumnDefault(MariaDBPlatform $platform, ?string $columnDefault): ?string
private function parseMariaDBColumnDefault(string $columnDefault): string|DefaultExpression|null
{
if ($columnDefault === 'NULL' || $columnDefault === null) {
if ($columnDefault === 'NULL') {
return null;
}

Expand All @@ -255,9 +273,9 @@ private function getMariaDBColumnDefault(MariaDBPlatform $platform, ?string $col
}

return match ($columnDefault) {
'current_timestamp()' => $platform->getCurrentTimestampSQL(),
'curdate()' => $platform->getCurrentDateSQL(),
'curtime()' => $platform->getCurrentTimeSQL(),
'current_timestamp()' => new CurrentTimestamp(),
'curdate()' => new CurrentDate(),
'curtime()' => new CurrentTime(),
default => $columnDefault,
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/Schema/SQLServerSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Platforms\SQLServer;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\DefaultExpression\CurrentTimestamp;
use Doctrine\DBAL\Types\Type;

use function array_change_key_case;
Expand Down Expand Up @@ -148,7 +149,7 @@ protected function _getPortableTableColumnDefinition(array $tableColumn): Column
return $column;
}

private function parseDefaultExpression(string $value): ?string
private function parseDefaultExpression(string $value): string|DefaultExpression|null
{
while (preg_match('/^\((.*)\)$/s', $value, $matches) === 1) {
$value = $matches[1];
Expand All @@ -163,7 +164,7 @@ private function parseDefaultExpression(string $value): ?string
}

if ($value === 'getdate()') {
return $this->platform->getCurrentTimestampSQL();
return new CurrentTimestamp();
}

return $value;
Expand Down