Skip to content

Commit

Permalink
bernardphp#251 checking for queue existence instead of bashing the DB…
Browse files Browse the repository at this point in the history
… with constraint violation exceptions
  • Loading branch information
Ocramius committed Aug 22, 2016
1 parent 0dc337c commit 45026ec
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Bernard/Driver/DoctrineDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bernard\Driver;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\ConstraintViolationException;

/**
* Driver supporting Doctrine DBAL
Expand Down Expand Up @@ -34,12 +35,30 @@ public function listQueues()

/**
* {@inheritDoc}
*
* @throws \Exception
*/
public function createQueue($queueName)
{
try {
$this->connection->insert('bernard_queues', array('name' => $queueName));
} catch (\Exception $e) {
$this->connection->transactional(function () use ($queueName) {
$queueExistsQb = $this->connection->createQueryBuilder();

$queueExists = $queueExistsQb
->select('name')
->from('bernard_queues')
->where($queueExistsQb->expr()->eq('name', ':name'))
->setParameter('name', $queueName)
->execute();

if ($queueExists->fetch()) {
// queue was already created
return;
}

$this->connection->insert('bernard_queues', array('name' => $queueName));
});
} catch (ConstraintViolationException $ignored) {
// Because SQL server does not support a portable INSERT ON IGNORE syntax
// this ignores error based on primary key.
}
Expand Down

0 comments on commit 45026ec

Please sign in to comment.