Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\DoctrineDbalStore;

final class LockStoreSchemaSubscriber extends AbstractSchemaSubscriber
{
/**
* @param iterable<mixed, PersistingStoreInterface> $stores
*/
public function __construct(private iterable $stores)
{
}

public function postGenerateSchema(GenerateSchemaEventArgs $event): void
{
$connection = $event->getEntityManager()->getConnection();

foreach ($this->stores as $store) {
if (!$store instanceof DoctrineDbalStore) {
continue;
}

$store->configureSchema($event->getSchema(), $this->getIsSameDatabaseChecker($connection));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1898,8 +1898,10 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
foreach ($resourceStores as $resourceStore) {
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
$storeDefinition = new Definition(PersistingStoreInterface::class);
$storeDefinition->setFactory([StoreFactory::class, 'createStore']);
$storeDefinition->setArguments([$resourceStore]);
$storeDefinition
->setFactory([StoreFactory::class, 'createStore'])
->setArguments([$resourceStore])
->addTag('lock.store');

$container->setDefinition($storeDefinitionId = '.lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition);

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Create migration for lock table when DoctrineDbalStore is used

6.0
---

Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Lock/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,21 @@ public function createTable(): void

/**
* Adds the Table to the Schema if it doesn't exist.
*
* @param \Closure $isSameDatabase
*/
public function configureSchema(Schema $schema): void
public function configureSchema(Schema $schema/* , \Closure $isSameDatabase */): void
{
if ($schema->hasTable($this->table)) {
return;
}

$isSameDatabase = 1 < \func_num_args() ? func_get_arg(1) : static fn () => true;

if (!$isSameDatabase($this->conn->executeStatement(...))) {
return;
}

$table = $schema->createTable($this->table);
$table->addColumn($this->idCol, 'string', ['length' => 64]);
$table->addColumn($this->tokenCol, 'string', ['length' => 44]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\DoctrineDbalStore;
Expand Down Expand Up @@ -208,4 +209,39 @@ public function testCreatesTableOutsideTransaction()

$store->save($key);
}

public function testConfigureSchemaDifferentDatabase()
{
$conn = $this->createMock(Connection::class);
$someFunction = function () { return false; };
$schema = new Schema();

$dbalStore = new DoctrineDbalStore($conn);
$dbalStore->configureSchema($schema, $someFunction);
$this->assertFalse($schema->hasTable('lock_keys'));
}

public function testConfigureSchemaSameDatabase()
{
$conn = $this->createMock(Connection::class);
$someFunction = function () { return true; };
$schema = new Schema();

$dbalStore = new DoctrineDbalStore($conn);
$dbalStore->configureSchema($schema, $someFunction);
$this->assertTrue($schema->hasTable('lock_keys'));
}

public function testConfigureSchemaTableExists()
{
$conn = $this->createMock(Connection::class);
$schema = new Schema();
$schema->createTable('lock_keys');

$dbalStore = new DoctrineDbalStore($conn);
$someFunction = function () { return true; };
$dbalStore->configureSchema($schema, $someFunction);
$table = $schema->getTable('lock_keys');
$this->assertEmpty($table->getColumns(), 'The table was not overwritten');
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/Tests/Store/PdoStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @author Jérémy Derussé <[email protected]>
*
* @requires extension pdo_sqlite
*
* @group integration
*/
class PdoStoreTest extends AbstractStoreTest
Expand Down