Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] use database platform to convert correctly the DateTime #32456

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

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public function testGetAMessageWillChangeItsStatus()
$queryBuilder
->method('getParameters')
->willReturn([]);
$queryBuilder
->method('getParameterTypes')
->willReturn([]);
$driverConnection
->method('prepare')
->method('executeQuery')
->willReturn($stmt);

$connection = new Connection([], $driverConnection, $schemaSynchronizer);
Expand All @@ -65,13 +68,17 @@ public function testGetWithNoPendingMessageWillReturnNull()
$queryBuilder
->method('getParameters')
->willReturn([]);
$queryBuilder
->method('getParameterTypes')
->willReturn([]);
$driverConnection->expects($this->once())
->method('createQueryBuilder')
->willReturn($queryBuilder);
$driverConnection->method('prepare')
->willReturn($stmt);
$driverConnection->expects($this->never())
->method('update');
$driverConnection
->method('executeQuery')
->willReturn($stmt);

$connection = new Connection([], $driverConnection, $schemaSynchronizer);
$doctrineEnvelope = $connection->get();
Expand Down Expand Up @@ -273,7 +280,7 @@ public function testFind()
->method('getParameters')
->willReturn([]);
$driverConnection
->method('prepare')
->method('executeQuery')
->willReturn($stmt);

$connection = new Connection([], $driverConnection, $schemaSynchronizer);
Expand Down Expand Up @@ -316,8 +323,11 @@ public function testFindAll()
$queryBuilder
->method('getParameters')
->willReturn([]);
$queryBuilder
->method('getParameterTypes')
->willReturn([]);
$driverConnection
->method('prepare')
->method('executeQuery')
->willReturn($stmt);

$connection = new Connection([], $driverConnection, $schemaSynchronizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ public function testItRetrieveTheFirstAvailableMessage()
'body' => '{"message": "Hi handled"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'delivered_at' => Connection::formatDateTime(new \DateTime()),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'delivered_at' => $this->formatDateTime(new \DateTime()),
]);
// one available later
$this->driverConnection->insert('messenger_messages', [
'body' => '{"message": "Hi delayed"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 13:00:00')),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 13:00:00')),
]);
// one available
$this->driverConnection->insert('messenger_messages', [
'body' => '{"message": "Hi available"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:30:00')),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:30:00')),
]);

$encoded = $this->connection->get();
Expand All @@ -114,33 +114,33 @@ public function testItCountMessages()
'body' => '{"message": "Hi handled"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'delivered_at' => Connection::formatDateTime(new \DateTime()),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'delivered_at' => $this->formatDateTime(new \DateTime()),
]);
// one available later
$this->driverConnection->insert('messenger_messages', [
'body' => '{"message": "Hi delayed"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime((new \DateTime())->modify('+1 minute')),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime((new \DateTime())->modify('+1 minute')),
]);
// one available
$this->driverConnection->insert('messenger_messages', [
'body' => '{"message": "Hi available"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:30:00')),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:30:00')),
]);
// another available
$this->driverConnection->insert('messenger_messages', [
'body' => '{"message": "Hi available"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:30:00')),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:30:00')),
]);

$this->assertSame(2, $this->connection->getMessageCount());
Expand All @@ -155,16 +155,16 @@ public function testItRetrieveTheMessageThatIsOlderThanRedeliverTimeout()
'body' => '{"message": "Hi requeued"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'delivered_at' => Connection::formatDateTime($twoHoursAgo),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'delivered_at' => $this->formatDateTime($twoHoursAgo),
]);
$this->driverConnection->insert('messenger_messages', [
'body' => '{"message": "Hi available"}',
'headers' => json_encode(['type' => DummyMessage::class]),
'queue_name' => 'default',
'created_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => Connection::formatDateTime(new \DateTime('2019-03-15 12:30:00')),
'created_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:00:00')),
'available_at' => $this->formatDateTime(new \DateTime('2019-03-15 12:30:00')),
]);

$next = $this->connection->get();
Expand All @@ -181,4 +181,9 @@ public function testTheTransportIsSetupOnGet()
$envelope = $this->connection->get();
$this->assertEquals('the body', $envelope['body']);
}

private function formatDateTime(\DateTime $dateTime)
{
return $dateTime->format($this->driverConnection->getDatabasePlatform()->getDateTimeFormatString());
}
}
45 changes: 23 additions & 22 deletions src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ public function send(string $body, array $headers, int $delay = 0): string
$body,
json_encode($headers),
$this->configuration['queue_name'],
self::formatDateTime($now),
self::formatDateTime($availableAt),
$now,
$availableAt,
], [
null,
null,
null,
Type::DATETIME,
Type::DATETIME,
]);

return $this->driverConnection->lastInsertId();
Expand All @@ -143,7 +149,8 @@ public function get(): ?array
// use SELECT ... FOR UPDATE to lock table
$doctrineEnvelope = $this->executeQuery(
$query->getSQL().' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
$query->getParameters()
$query->getParameters(),
$query->getParameterTypes()
)->fetch();

if (false === $doctrineEnvelope) {
Expand All @@ -160,8 +167,10 @@ public function get(): ?array
->where('id = ?');
$now = new \DateTime();
$this->executeQuery($queryBuilder->getSQL(), [
self::formatDateTime($now),
$now,
$doctrineEnvelope['id'],
], [
Type::DATETIME,
]);

$this->driverConnection->commit();
Expand Down Expand Up @@ -228,7 +237,7 @@ public function getMessageCount(): int
->select('COUNT(m.id) as message_count')
->setMaxResults(1);

return $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters())->fetchColumn();
return $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes())->fetchColumn();
}

public function findAll(int $limit = null): array
Expand All @@ -238,7 +247,7 @@ public function findAll(int $limit = null): array
$queryBuilder->setMaxResults($limit);
}

$data = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters())->fetchAll();
$data = $this->executeQuery($queryBuilder->getSQL(), $queryBuilder->getParameters(), $queryBuilder->getParameterTypes())->fetchAll();

return array_map(function ($doctrineEnvelope) {
return $this->decodeEnvelopeHeaders($doctrineEnvelope);
Expand Down Expand Up @@ -267,9 +276,12 @@ private function createAvailableMessagesQueryBuilder(): QueryBuilder
->andWhere('m.available_at <= ?')
->andWhere('m.queue_name = ?')
->setParameters([
self::formatDateTime($redeliverLimit),
self::formatDateTime($now),
$redeliverLimit,
$now,
$this->configuration['queue_name'],
], [
Type::DATETIME,
Type::DATETIME,
]);
}

Expand All @@ -280,12 +292,10 @@ private function createQueryBuilder(): QueryBuilder
->from($this->configuration['table_name'], 'm');
}

private function executeQuery(string $sql, array $parameters = [])
private function executeQuery(string $sql, array $parameters = [], array $types = [])
{
$stmt = null;
try {
$stmt = $this->driverConnection->prepare($sql);
$stmt->execute($parameters);
$stmt = $this->driverConnection->executeQuery($sql, $parameters, $types);
} catch (TableNotFoundException $e) {
if ($this->driverConnection->isTransactionActive()) {
throw $e;
Expand All @@ -295,11 +305,7 @@ private function executeQuery(string $sql, array $parameters = [])
if ($this->autoSetup) {
$this->setup();
}
// statement not prepared ? SQLite throw on exception on prepare if the table does not exist
if (null === $stmt) {
$stmt = $this->driverConnection->prepare($sql);
}
$stmt->execute($parameters);
$stmt = $this->driverConnection->executeQuery($sql, $parameters, $types);
}

return $stmt;
Expand Down Expand Up @@ -332,11 +338,6 @@ private function getSchema(): Schema
return $schema;
}

public static function formatDateTime(\DateTimeInterface $dateTime)
{
return $dateTime->format('Y-m-d\TH:i:s');
}

private function decodeEnvelopeHeaders(array $doctrineEnvelope): array
{
$doctrineEnvelope['headers'] = json_decode($doctrineEnvelope['headers'], true);
Expand Down