-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/1601 ensure utc in database #46
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2575b73
1601: Ensure all timestamps are persisted as UTC in the database
turegjorup e434321
1601: Configure model and view timezone in EasyAdmin
turegjorup 4f94663
1601: Add ADR and update Changelog
turegjorup 169589b
1601: Markdown lint
turegjorup 6b561b3
1601: Psalm fixes
turegjorup 446bc71
Update src/Doctrine/Extensions/DBAL/Types/UTCDateTimeImmutableType.php
turegjorup 673f63e
1601: ADR 008 clarifications
turegjorup 2cfe8c0
1601: Markdown lint
turegjorup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Enforce UTC in database and ORM layer | ||
|
||
Date: 10-06-2024 | ||
|
||
## Status | ||
|
||
Accepted | ||
|
||
## Context | ||
|
||
Doctrine ORM by design does NOT handle timezones when persisting DateTime objects. E.g. if you do for a | ||
doctrine entity (note different timezones) | ||
|
||
```php | ||
$event->start = new \DateTimeImmutable('2024-06-04 17:17:17.000000', new \DateTimeZone('UTC')); | ||
$event->end = new \DateTimeImmutable('2024-06-04 17:17:17.000000', new \DateTimeZone('Europe/Copenhagen')); | ||
|
||
$this->entityManager->persist($event); | ||
$this->entityManager->flush(); | ||
``` | ||
|
||
Then in the database these will have been persisted as | ||
|
||
| id | start | end | | ||
|-----|---------------------|---------------------| | ||
| xyz | 2024-06-04 17:17:17 | 2024-06-04 17:17:17 | | ||
|
||
Thereby discarding the timezone and timezone offset. | ||
|
||
Because we accept any valid timestamp in the feed imports regardless of timezone this doctrine behavior will lead to | ||
inconsistencies in the timestamps persisted in the database and exposed through the API. | ||
|
||
## Decision | ||
|
||
All datetime fields must be converted to UTC before they are persisted to database. This is done by overwriting the | ||
standard doctrine `DateTime` and `DateTimeImmutable` types by custom types as specified by Doctrine | ||
[Handling different Timezones with the DateTime Type](https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/cookbook/working-with-datetime.html#handling-different-timezones-with-the-datetime-type) | ||
|
||
```yaml | ||
# config/packages/doctrine.yaml | ||
doctrine: | ||
dbal: | ||
types: | ||
datetime: App\Doctrine\Extensions\DBAL\Types\UTCDateTimeType | ||
datetime_immutable: App\Doctrine\Extensions\DBAL\Types\UTCDateTimeImmutableType | ||
``` | ||
|
||
## Consequences | ||
|
||
All "view layers" need to be configured to factor in the model timezone. For the API this means that all datetime fields | ||
must be serialized with timezone (already the default). For EasyAdmin all datetime fields must be configured with | ||
correct "model" and "view" timezones. This is done with [field configurators](https://symfony.com/bundles/EasyAdminBundle/current/fields.html#field-configurators) | ||
and filter configurators. See `App\EasyAdmin\DateTimeFieldConfigurator` and `App\EasyAdmin\DateTimeFilterConfigurator` |
This file contains 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
This file contains 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
47 changes: 47 additions & 0 deletions
47
src/Doctrine/Extensions/DBAL/Types/UTCDateTimeImmutableType.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Doctrine\Extensions\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\DateTimeImmutableType; | ||
|
||
class UTCDateTimeImmutableType extends DateTimeImmutableType | ||
{ | ||
private static ?\DateTimeZone $utc; | ||
|
||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | ||
{ | ||
if ($value instanceof \DateTimeImmutable) { | ||
if (self::getUtc()->getName() !== $value->getTimezone()->getName()) { | ||
$value = $value->setTimezone(self::getUtc()); | ||
} | ||
} | ||
|
||
return parent::convertToDatabaseValue($value, $platform); | ||
} | ||
|
||
public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTimeImmutable | ||
{ | ||
if (null === $value || $value instanceof \DateTimeImmutable) { | ||
return $value; | ||
} | ||
|
||
$converted = \DateTimeImmutable::createFromFormat( | ||
$platform->getDateTimeFormatString(), | ||
$value, | ||
self::getUtc() | ||
); | ||
|
||
if (false === $converted) { | ||
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); | ||
} | ||
|
||
return $converted; | ||
} | ||
|
||
private static function getUtc(): \DateTimeZone | ||
{ | ||
return self::$utc ??= new \DateTimeZone('UTC'); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Doctrine\Extensions\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\DateTimeType; | ||
|
||
class UTCDateTimeType extends DateTimeType | ||
{ | ||
private static ?\DateTimeZone $utc; | ||
|
||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | ||
{ | ||
if ($value instanceof \DateTime) { | ||
$value->setTimezone(self::getUtc()); | ||
} | ||
|
||
return parent::convertToDatabaseValue($value, $platform); | ||
} | ||
|
||
public function convertToPHPValue($value, AbstractPlatform $platform): \DateTime|\DateTimeInterface|null | ||
{ | ||
if (null === $value || $value instanceof \DateTime) { | ||
return $value; | ||
} | ||
|
||
$converted = \DateTime::createFromFormat( | ||
$platform->getDateTimeFormatString(), | ||
$value, | ||
self::getUtc() | ||
); | ||
|
||
if (false === $converted) { | ||
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString()); | ||
} | ||
|
||
return $converted; | ||
} | ||
|
||
private static function getUtc(): \DateTimeZone | ||
{ | ||
return self::$utc ??= new \DateTimeZone('UTC'); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\EasyAdmin; | ||
|
||
use App\Controller\Admin\DashboardController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; | ||
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface; | ||
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; | ||
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; | ||
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField; | ||
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField; | ||
use EasyCorp\Bundle\EasyAdminBundle\Field\TimeField; | ||
|
||
class DateTimeFieldConfigurator implements FieldConfiguratorInterface | ||
{ | ||
public function supports(FieldDto $field, EntityDto $entityDto): bool | ||
{ | ||
return | ||
DateTimeField::class === $field->getFieldFqcn() | ||
|| DateField::class === $field->getFieldFqcn() | ||
|| TimeField::class === $field->getFieldFqcn(); | ||
} | ||
|
||
public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void | ||
{ | ||
$field->setFormTypeOptions([ | ||
'model_timezone' => DashboardController::MODEL_TIMEZONE, | ||
'view_timezone' => DashboardController::VIEW_TIMEZONE, | ||
]); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\EasyAdmin; | ||
|
||
use App\Controller\Admin\DashboardController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; | ||
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterConfiguratorInterface; | ||
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; | ||
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; | ||
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDto; | ||
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter; | ||
|
||
class DateTimeFilterConfigurator implements FilterConfiguratorInterface | ||
{ | ||
public function supports(FilterDto $filterDto, ?FieldDto $fieldDto, EntityDto $entityDto, AdminContext $context): bool | ||
{ | ||
return DateTimeFilter::class === $filterDto->getFqcn(); | ||
} | ||
|
||
public function configure(FilterDto $filterDto, ?FieldDto $fieldDto, EntityDto $entityDto, AdminContext $context): void | ||
{ | ||
$filterDto->setFormTypeOptions([ | ||
'value_type_options' => [ | ||
// @see https://symfony.com/doc/current/reference/forms/types/date.html#field-options | ||
'model_timezone' => DashboardController::MODEL_TIMEZONE, | ||
'view_timezone' => DashboardController::VIEW_TIMEZONE, | ||
], | ||
]); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the “view” and “format” values from config (
.env.local
) would be really nice, but it may require a lot of effort.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are unlikely to change in the applications lifetime so will leave as is for now.