Last active
February 12, 2025 17:42
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; | |
} | |
}; | |
} | |
}; | |
} | |
} |
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
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?