-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[HttpFoundation] Create migration for session table when pdo handler is used #48059
Merged
nicolas-grekas
merged 1 commit into
symfony:6.3
from
alli83:http-foundation-create-automatically-session-table-with-pdo-connection
Dec 16, 2022
+265
−70
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bridge/Doctrine/SchemaListener/AbstractSchemaSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\SchemaListener; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception\TableNotFoundException; | ||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use Doctrine\ORM\Tools\ToolEvents; | ||
|
||
abstract class AbstractSchemaSubscriber implements EventSubscriber | ||
{ | ||
abstract public function postGenerateSchema(GenerateSchemaEventArgs $event): void; | ||
|
||
public function getSubscribedEvents(): array | ||
{ | ||
if (!class_exists(ToolEvents::class)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
ToolEvents::postGenerateSchema, | ||
]; | ||
} | ||
|
||
protected function getIsSameDatabaseChecker(Connection $connection): \Closure | ||
{ | ||
return static function (\Closure $exec) use ($connection): bool { | ||
$checkTable = 'schema_subscriber_check_'.bin2hex(random_bytes(7)); | ||
$connection->executeStatement(sprintf('CREATE TABLE %s (id INTEGER NOT NULL)', $checkTable)); | ||
|
||
try { | ||
$exec(sprintf('DROP TABLE %s', $checkTable)); | ||
} catch (\Exception) { | ||
// ignore | ||
} | ||
|
||
try { | ||
$connection->executeStatement(sprintf('DROP TABLE %s', $checkTable)); | ||
|
||
return false; | ||
} catch (TableNotFoundException) { | ||
return true; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Symfony/Bridge/Doctrine/SchemaListener/PdoSessionHandlerSchemaSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\SchemaListener; | ||
|
||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
|
||
final class PdoSessionHandlerSchemaSubscriber extends AbstractSchemaSubscriber | ||
{ | ||
private iterable $pdoSessionHandlers; | ||
|
||
/** | ||
* @param iterable<mixed, PdoSessionHandler> $pdoSessionHandlers | ||
*/ | ||
public function __construct(iterable $pdoSessionHandlers) | ||
{ | ||
$this->pdoSessionHandlers = $pdoSessionHandlers; | ||
} | ||
|
||
public function postGenerateSchema(GenerateSchemaEventArgs $event): void | ||
{ | ||
$connection = $event->getEntityManager()->getConnection(); | ||
|
||
foreach ($this->pdoSessionHandlers as $pdoSessionHandler) { | ||
$pdoSessionHandler->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Symfony/Bridge/Doctrine/Tests/SchemaListener/PdoSessionHandlerSchemaSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\SchemaListener; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaSubscriber; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
|
||
class PdoSessionHandlerSchemaSubscriberTest extends TestCase | ||
{ | ||
public function testPostGenerateSchemaPdo() | ||
{ | ||
$schema = new Schema(); | ||
$dbalConnection = $this->createMock(Connection::class); | ||
$entityManager = $this->createMock(EntityManagerInterface::class); | ||
$entityManager->expects($this->once()) | ||
->method('getConnection') | ||
->willReturn($dbalConnection); | ||
$event = new GenerateSchemaEventArgs($entityManager, $schema); | ||
|
||
$pdoSessionHandler = $this->createMock(PdoSessionHandler::class); | ||
$pdoSessionHandler->expects($this->once()) | ||
->method('configureSchema') | ||
->with($schema, fn() => true); | ||
|
||
$subscriber = new PdoSessionHandlerSchemaSubscriber([$pdoSessionHandler]); | ||
$subscriber->postGenerateSchema($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.3 | ||
--- | ||
|
||
* Create migration for session table when pdo handler is used | ||
|
||
6.2 | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When integrating a component with doctrine migrations, we want to known whether the database used by doctrine is the same as the one used by the component. In e.g. Messenger, this can be done by comparing the connection objects for identity, but in cases where several connections are used (eg session / lock use a different connection), then this doesn't work.
This method implements this check by creating a temporary table: if the component's connection can drop this table, then we're on the same db.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. Thank you 🙏