Skip to content

Commit

Permalink
bug #36966 Fix extra SQL support in Doctrine migrations (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.1 branch.

Discussion
----------

Fix extra SQL support in Doctrine migrations

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

/cc @weaverryan

Commits
-------

1d1f3e1 Fix extra SQL support in Doctrine migrations
  • Loading branch information
fabpot committed May 26, 2020
2 parents 4739835 + 1d1f3e1 commit 7f6934c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public function onSchemaCreateTable(SchemaCreateTableEventArgs $event): void
continue;
}

$extraSql = $transport->getExtraSetupSqlForTable($table);
if (null === $extraSql) {
if (!$extraSql = $transport->getExtraSetupSqlForTable($table)) {
continue;
}

Expand All @@ -79,7 +78,9 @@ public function onSchemaCreateTable(SchemaCreateTableEventArgs $event): void
* the only way to inject some extra SQL.
*/
$event->addSql($createTableSql);
$event->addSql($extraSql);
foreach ($extraSql as $sql) {
$event->addSql($sql);
}
$event->preventDefault();

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testOnSchemaCreateTable()
$doctrineTransport->expects($this->once())
->method('getExtraSetupSqlForTable')
->with($table)
->willReturn('ALTER TABLE pizza ADD COLUMN extra_cheese boolean');
->willReturn(['ALTER TABLE pizza ADD COLUMN extra_cheese boolean']);

// we use the platform to generate the full create table sql
$platform->expects($this->once())
Expand All @@ -87,7 +87,7 @@ public function testOnSchemaCreateTableNoExtraSql()
$doctrineTransport = $this->createMock(DoctrineTransport::class);
$doctrineTransport->expects($this->once())
->method('getExtraSetupSqlForTable')
->willReturn(null);
->willReturn([]);

$platform->expects($this->never())
->method('getCreateTableSQL');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testGetExtraSetupSql()

$table = new Table('queue_table');
$table->addOption('_symfony_messenger_table_name', 'queue_table');
$this->assertStringContainsString('CREATE TRIGGER', $connection->getExtraSetupSqlForTable($table));
$this->assertStringContainsString('CREATE TRIGGER', implode("\n", $connection->getExtraSetupSqlForTable($table)));
}

public function testGetExtraSetupSqlWrongTable()
Expand All @@ -62,6 +62,6 @@ public function testGetExtraSetupSqlWrongTable()

$table = new Table('queue_table');
// don't set the _symfony_messenger_table_name option
$this->assertNull($connection->getExtraSetupSqlForTable($table));
$this->assertSame([], $connection->getExtraSetupSqlForTable($table));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ public function configureSchema(Schema $schema, DBALConnection $forConnection):
/**
* @internal
*/
public function getExtraSetupSqlForTable(Table $createdTable): ?string
public function getExtraSetupSqlForTable(Table $createdTable): array
{
return null;
return [];
}

private function createAvailableMessagesQueryBuilder(): QueryBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ public function configureSchema(Schema $schema, DbalConnection $forConnection):

/**
* Adds extra SQL if the given table was created by the Connection.
*
* @return string[]
*/
public function getExtraSetupSqlForTable(Table $createdTable): ?string
public function getExtraSetupSqlForTable(Table $createdTable): array
{
return $this->connection->getExtraSetupSqlForTable($createdTable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,43 +85,43 @@ public function setup(): void
{
parent::setup();

$this->driverConnection->exec($this->getTriggerSql());
$this->driverConnection->exec(implode("\n", $this->getTriggerSql()));
}

public function getExtraSetupSqlForTable(Table $createdTable): ?string
/**
* @return string[]
*/
public function getExtraSetupSqlForTable(Table $createdTable): array
{
if (!$createdTable->hasOption(self::TABLE_OPTION_NAME)) {
return null;
return [];
}

if ($createdTable->getOption(self::TABLE_OPTION_NAME) !== $this->configuration['table_name']) {
return null;
return [];
}

return $this->getTriggerSql();
}

private function getTriggerSql(): string
private function getTriggerSql(): array
{
return sprintf(<<<'SQL'
LOCK TABLE %1$s;
-- create trigger function
return [
sprintf('LOCK TABLE %s;', $this->configuration['table_name']),
// create trigger function
sprintf(<<<'SQL'
CREATE OR REPLACE FUNCTION notify_%1$s() RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('%1$s', NEW.queue_name::text);
RETURN NEW;
BEGIN
PERFORM pg_notify('%1$s', NEW.queue_name::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- register trigger
DROP TRIGGER IF EXISTS notify_trigger ON %1$s;
CREATE TRIGGER notify_trigger
AFTER INSERT
ON %1$s
FOR EACH ROW EXECUTE PROCEDURE notify_%1$s();
SQL
, $this->configuration['table_name']);
, $this->configuration['table_name']),
// register trigger
sprintf('DROP TRIGGER IF EXISTS notify_trigger ON %s;', $this->configuration['table_name']),
sprintf('CREATE TRIGGER notify_trigger AFTER INSERT ON %1$s FOR EACH ROW EXECUTE PROCEDURE notify_%1$s();', $this->configuration['table_name']),
];
}

private function unlisten()
Expand Down

0 comments on commit 7f6934c

Please sign in to comment.