Skip to content

Instantly share code, notes, and snippets.

@ruudk
Last active February 12, 2025 17:42
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['type'] === 'datetime(6)') {
$column->setType(Type::getTypeRegistry()->get('utc_microdatetime_immutable'));
} elseif ($tableColumn['type'] === 'timestamp' && $tableColumn['EXTRA'] === 'on update CURRENT_TIMESTAMP') {
$column->setType(Type::getTypeRegistry()->get('timestamped'));
}
return $column;
}
};
}
};
}
};
}
}
@hrvoj3e
Copy link

hrvoj3e commented Jan 30, 2025

Where did you plug it in?
I am using DBAL for diff only and trying to create migrations between two shemas.
No framework - just plain PHP with connections to two databases.
Any pointers?

@ruudk
Copy link
Author

ruudk commented Feb 8, 2025

Something like this:

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

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