8000 bug #36966 Fix extra SQL support in Doctrine migrations (fabpot) · cvmiert/symfony@7f6934c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f6934c

Browse files
committed
bug symfony#36966 Fix extra SQL support in Doctrine migrations (fabpot)
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
2 parents 4739835 + 1d1f3e1 commit 7f6934c

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

src/Symfony/Bridge/Doctrine/SchemaListener/MessengerTransportDoctrineSchemaSubscriber.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public function onSchemaCreateTable(SchemaCreateTableEventArgs $event): void
6464
continue;
6565
}
6666

67-
$extraSql = $transport->getExtraSetupSqlForTable($table);
68-
if (null === $extraSql) {
67+
if (!$extraSql = $transport->getExtraSetupSqlForTable($table)) {
6968
continue;
7069
}
7170

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

8586
return;

src/Symfony/Bridge/Doctrine/Tests/SchemaListener/MessengerTransportDoctrineSchemaSubscriberTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testOnSchemaCreateTable()
6161
$doctrineTransport->expects($this->once())
6262
->method('getExtraSetupSqlForTable')
6363
->with($table)
64-
->willReturn('ALTER TABLE pizza ADD COLUMN extra_cheese boolean');
64+
->willReturn(['ALTER TABLE pizza ADD COLUMN extra_cheese boolean']);
6565

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

9292
$platform->expects($this->never())
9393
->method('getCreateTableSQL');

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/PostgreSqlConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testGetExtraSetupSql()
5252

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

5858
public function testGetExtraSetupSqlWrongTable()
@@ -62,6 +62,6 @@ public function testGetExtraSetupSqlWrongTable()
6262

6363
$table = new Table('queue_table');
6464
// don't set the _symfony_messenger_table_name option
65-
$this->assertNull($connection->getExtraSetupSqlForTable($table));
65 8000 +
$this->assertSame([], $connection->getExtraSetupSqlForTable($table));
6666
}
6767
}

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ public function configureSchema(Schema $schema, DBALConnection $forConnection):
313313
/**
314314
* @internal
315315
*/
316-
public function getExtraSetupSqlForTable(Table $createdTable): ?string
316+
public function getExtraSetupSqlForTable(Table $createdTable): array
317317
{
318-
return null;
318+
return [];
319319
}
320320

321321
private function createAvailableMessagesQueryBuilder(): QueryBuilder

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/DoctrineTransport.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ public function configureSchema(Schema $schema, DbalConnection $forConnection):
111111

112112
/**
113113
* Adds extra SQL if the given table was created by the Connection.
114+
*
115+
* @return string[]
114116
*/
115-
public function getExtraSetupSqlForTable(Table $createdTable): ?string
117+
public function getExtraSetupSqlForTable(Table $createdTable): array
116118
{
117119
return $this->connection->getExtraSetupSqlForTable($createdTable);
118120
}

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/PostgreSqlConnection.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,43 +85,43 @@ public function setup(): void
8585
{
8686
parent::setup();
8787

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

91-
public function getExtraSetupSqlForTable(Table $createdTable): ?string
91+
/**
92+
* @return string[]
93+
*/
94+
public function getExtraSetupSqlForTable(Table $createdTable): array
9295
{
9396
if (!$createdTable->hasOption(self::TABLE_OPTION_NAME)) {
94-
return null;
97+
return [];
9598
}
9699

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

101104
return $this->getTriggerSql();
102105
}
103106

104-
private function getTriggerSql(): string
107+
private function getTriggerSql(): array
105108
{
106-
return sprintf(<<<'SQL'
107-
LOCK TABLE %1$s;
108-
-- create trigger function
109+
return [
110+
sprintf('LOCK TABLE %s;', $this->configuration['table_name']),
111+
// create trigger function
112+
sprintf(<<<'SQL'
109113
CREATE OR REPLACE FUNCTION notify_%1$s() RETURNS TRIGGER AS $$
110-
BEGIN
111-
PERFORM pg_notify('%1$s', NEW.queue_name::text);
112-
RETURN NEW;
114+
BEGIN
115+
PERFORM pg_notify('%1$s', NEW.queue_name::text);
116+
RETURN NEW;
113117
END;
114118
$$ LANGUAGE plpgsql;
115-
116-
-- register trigger
117-
DROP TRIGGER IF EXISTS notify_trigger ON %1$s;
118-
119-
CREATE TRIGGER notify_trigger
120-
AFTER INSERT
121-
ON %1$s
122-
FOR EACH ROW EXECUTE PROCEDURE notify_%1$s();
123119
SQL
124-
, $this->configuration['table_name']);
120+
, $this->configuration['table_name']),
121+
// register trigger
122+
sprintf('DROP TRIGGER IF EXISTS notify_trigger ON %s;', $this->configuration['table_name']),
123+
sprintf('CREATE TRIGGER notify_trigger AFTER INSERT ON %1$s FOR EACH ROW EXECUTE PROCEDURE notify_%1$s();', $this->configuration['table_name']),
124+
];
125125
}
126126

127127
private function unlisten()

0 commit comments

Comments
 (0)
0