Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Messenger] DoctrineTransport: ensure auto setup is only done once #32308

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 7, 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 @@ -21,29 +21,27 @@
*/
class DoctrineIntegrationTest extends TestCase
{
/** @var \Doctrine\DBAL\Connection */
private $driverConnection;
/** @var Connection */
private $connection;
/** @var string */
private $sqliteFile;

/**
* @after
*/
public function cleanup()
protected function setUp(): void
{
@unlink(sys_get_temp_dir().'/symfony.messenger.sqlite');
$this->sqliteFile = sys_get_temp_dir().'/symfony.messenger.sqlite';
$dsn = getenv('MESSENGER_DOCTRINE_DSN') ?: 'sqlite:///'.$this->sqliteFile;
$this->driverConnection = DriverManager::getConnection(['url' => $dsn]);
$this->connection = new Connection([], $this->driverConnection);
}

/**
* @before
*/
public function createConnection()
protected function tearDown(): void
{
$dsn = getenv('MESSENGER_DOCTRINE_DSN') ?: 'sqlite:///'.sys_get_temp_dir().'/symfony.messenger.sqlite';
$this->driverConnection = DriverManager::getConnection(['url' => $dsn]);
$this->connection = new Connection([], $this->driverConnection);
// call send to auto-setup the table
$this->connection->setup();
// ensure the table is clean for tests
$this->driverConnection->exec('DELETE FROM messenger_messages');
$this->driverConnection->close();
if (file_exists($this->sqliteFile)) {
unlink($this->sqliteFile);
}
}
fabpot marked this conversation as resolved.
Show resolved Hide resolved

public function testConnectionSendAndGet()
Expand Down Expand Up @@ -75,6 +73,7 @@ public function testSendWithDelay()

public function testItRetrieveTheFirstAvailableMessage()
{
$this->connection->setup();
// insert messages
// one currently handled
$this->driverConnection->insert('messenger_messages', [
Expand Down Expand Up @@ -108,6 +107,7 @@ public function testItRetrieveTheFirstAvailableMessage()

public function testItCountMessages()
{
$this->connection->setup();
// insert messages
// one currently handled
$this->driverConnection->insert('messenger_messages', [
Expand Down Expand Up @@ -148,6 +148,7 @@ public function testItCountMessages()

public function testItRetrieveTheMessageThatIsOlderThanRedeliverTimeout()
{
$this->connection->setup();
$twoHoursAgo = new \DateTime('now');
$twoHoursAgo->modify('-2 hours');
$this->driverConnection->insert('messenger_messages', [
Expand All @@ -173,10 +174,7 @@ public function testItRetrieveTheMessageThatIsOlderThanRedeliverTimeout()

public function testTheTransportIsSetupOnGet()
{
// If the table does not exist and we call the get (i.e run messenger:consume) the table must be setup
// so first delete the tables
$this->driverConnection->exec('DROP TABLE messenger_messages');

$this->assertFalse($this->driverConnection->getSchemaManager()->tablesExist('messenger_messages'));
$this->assertNull($this->connection->get());

$this->connection->send('the body', ['my' => 'header']);
Expand Down
27 changes: 15 additions & 12 deletions 27 src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ class Connection
private $configuration = [];
private $driverConnection;
private $schemaSynchronizer;
private $autoSetup;
fabpot marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(array $configuration, DBALConnection $driverConnection, SchemaSynchronizer $schemaSynchronizer = null)
{
$this->configuration = array_replace_recursive(self::DEFAULT_OPTIONS, $configuration);
$this->driverConnection = $driverConnection;
$this->schemaSynchronizer = $schemaSynchronizer ?? new SingleDatabaseSynchronizer($this->driverConnection);
$this->autoSetup = $this->configuration['auto_setup'];
}

public function getConfiguration(): array
Expand Down Expand Up @@ -131,9 +133,7 @@ public function send(string $body, array $headers, int $delay = 0): string

public function get(): ?array
{
if ($this->configuration['auto_setup']) {
$this->setup();
}
get:
$this->driverConnection->beginTransaction();
try {
$query = $this->createAvailableMessagesQueryBuilder()
Expand Down Expand Up @@ -170,6 +170,11 @@ public function get(): ?array
} catch (\Throwable $e) {
$this->driverConnection->rollBack();

if ($this->autoSetup && $e instanceof TableNotFoundException) {
$this->setup();
goto get;
fabpot marked this conversation as resolved.
Show resolved Hide resolved
}

throw $e;
}
}
Expand Down Expand Up @@ -213,6 +218,8 @@ public function setup(): void
} else {
$this->driverConnection->getConfiguration()->setFilterSchemaAssetsExpression($assetFilter);
}

$this->autoSetup = false;
fabpot marked this conversation as resolved.
Show resolved Hide resolved
}

public function getMessageCount(): int
Expand All @@ -226,10 +233,6 @@ public function getMessageCount(): int

public function findAll(int $limit = null): array
{
if ($this->configuration['auto_setup']) {
$this->setup();
}

$queryBuilder = $this->createAvailableMessagesQueryBuilder();
if (null !== $limit) {
$queryBuilder->setMaxResults($limit);
Expand All @@ -244,10 +247,6 @@ public function findAll(int $limit = null): array

public function find($id): ?array
{
if ($this->configuration['auto_setup']) {
$this->setup();
}

$queryBuilder = $this->createQueryBuilder()
->where('m.id = ?');

Expand Down Expand Up @@ -288,8 +287,12 @@ private function executeQuery(string $sql, array $parameters = [])
$stmt = $this->driverConnection->prepare($sql);
$stmt->execute($parameters);
} catch (TableNotFoundException $e) {
if ($this->driverConnection->isTransactionActive()) {
throw $e;
}

// create table
if (!$this->driverConnection->isTransactionActive() && $this->configuration['auto_setup']) {
if ($this->autoSetup) {
$this->setup();
}
// statement not prepared ? SQLite throw on exception on prepare if the table does not exist
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.