Skip to content

Commit 0f60523

Browse files
fix migrations of dates with wrong timezone (#3729)
2 parents 9138595 + 6b0e498 commit 0f60523

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/Migrations/Version20240111072911.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use DateTime;
88
use DateTimeZone;
99
use Doctrine\DBAL\Schema\Schema;
10-
use Shopsys\FrameworkBundle\Component\Domain\Domain;
1110
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;
1211
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1312

@@ -20,14 +19,27 @@ class Version20240111072911 extends AbstractMigration implements ContainerAwareI
2019
*/
2120
public function up(Schema $schema): void
2221
{
23-
$firstDomainTimeZone = $this->getDomain()->getDomainConfigById(Domain::FIRST_DOMAIN_ID)->getDateTimeZone();
24-
$utcTimeZone = new DateTimeZone('UTC');
25-
$dateTime = new DateTime('now', $utcTimeZone);
26-
$timeOffsetInHours = timezone_offset_get($firstDomainTimeZone, $dateTime) / 3600;
27-
28-
$this->sql(
29-
sprintf('UPDATE blog_articles SET publish_date = publish_date - INTERVAL \'%d hour\';', $timeOffsetInHours),
30-
);
22+
$blogArticles = $this->sql('SELECT id, publish_date FROM blog_articles')->fetchAllAssociative();
23+
24+
foreach ($blogArticles as $blogArticle) {
25+
$dateTime = new DateTime(
26+
$blogArticle['publish_date'],
27+
new DateTimeZone('Europe/Prague'),
28+
);
29+
$dateTime->setTimezone(new DateTimeZone('UTC'));
30+
31+
$this->sql(
32+
'UPDATE blog_articles SET publish_date = :publishDate WHERE id = :id',
33+
[
34+
'publishDate' => $dateTime,
35+
'id' => $blogArticle['id'],
36+
],
37+
[
38+
'publishDate' => 'datetime',
39+
'id' => 'integer',
40+
],
41+
);
42+
}
3143
}
3244

3345
/**

0 commit comments

Comments
 (0)