doctrine / dbal

Doctrine Database Abstraction Layer
https://www.doctrine-project.org/projects/dbal.html
MIT License
9.4k stars 1.33k forks source link

feat: add TIMESTAMP and TIME precision #6438

Open vencakrecl opened 3 weeks ago

vencakrecl commented 3 weeks ago
Q A
Type feature
Fixed issues

Summary

Added support for precision for PostgreSQL TIMESTAMP and TIME. It would be nice to have this feature per column but it is impossible with the current implementation of datetime types.

How to use:

<?php

declare(strict_types=1);

namespace App\Doctrine;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsMiddleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;

#[AsMiddleware]
class QueuesMiddleware implements Middleware
{
    public function wrap(Driver $driver): Driver
    {
        return new class extends AbstractDriverMiddleware {
            public function getDatabasePlatform(): PostgreSqlPlatform
            {
                $platform = new PostgreSqlPlatform();
                $platform->setTimestampPrecision(6);

                return $platform;
            }

            public function createDatabasePlatformForVersion($version): PostgreSqlPlatform
            {
                return $this->getDatabasePlatform();
            }
        };
    }
}