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

Fix extra SQL support in Doctrine migrations #36966

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
May 26, 2020
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 @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix: addSql() onlt works if it is given a single SQL statement.

}
$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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.