Skip to content

Instantly share code, notes, and snippets.

@ruudk
Last active July 12, 2025 07:20
Show Gist options
  • Save ruudk/10fd5dc1325492df97b33682b3423f34 to your computer and use it in GitHub Desktop.
Save ruudk/10fd5dc1325492df97b33682b3423f34 to your computer and use it in GitHub Desktop.
This custom middleware can be used to overwrite the schema manager on the connection. It allows for supporting DATETIME(6) (with precision) to be properly detected by Doctrine. See https://github.com/doctrine/dbal/issues/6631
final readonly class DoctrineSchemaUpdateMiddleware implements Middleware
{
#[Override]
public function wrap(DriverInterface $driver) : DriverInterface
{
return new class($driver) extends AbstractDriverMiddleware {
#[Override]
public function getDatabasePlatform(ServerVersionProvider $versionProvider) : AbstractPlatform
{
$platform = parent::getDatabasePlatform($versionProvider);
// We want this to break whenever we upgrade to a newer MySQL verion or whenever
// Doctrine DBAL removes the deprecated MySQL80Platform class.
Assert::true($platform::class === MySQL80Platform::class);
return new class extends MySQL80Platform {
#[Override]
public function createSchemaManager(Connection $connection) : MySQLSchemaManager
{
return new class($connection, $this) extends MySQLSchemaManager {
#[Override]
protected function _getPortableTableColumnDefinition(array $tableColumn) : Column
{
$column = parent::_getPortableTableColumnDefinition($tableColumn);
if ($tableColumn['COLUMN_TYPE'] === 'datetime(6)') {
$column->setType(Type::getTypeRegistry()->get('utc_microdatetime_immutable'));
} elseif ($tableColumn['COLUMN_TYPE'] === 'timestamp' && $tableColumn['EXTRA'] === 'on update CURRENT_TIMESTAMP') {
$column->setType(Type::getTypeRegistry()->get('timestamped'));
}
return $column;
}
};
}
};
}
};
}
}
@ruudk
Copy link
Author

ruudk commented Feb 8, 2025

Something like this:

$b = new \Doctrine\DBAL\Configuration();
$b->setMiddlewares([new DoctrineSchemaUpdateMiddleware()]);

@ruudk
Copy link
Author

ruudk commented Jul 12, 2025

Updated the gist to support DBAL 4.3.0 where the type key was changed to 'COLUMN_TYPE'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment