Skip to content
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

Added condition to check that we have an active connection #474

Open
wants to merge 3 commits into
base: 3.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 26 additions & 18 deletions Collector/MigrationsCollector.php
Expand Up @@ -10,6 +10,10 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Throwable;

use function count;
use function get_class;

class MigrationsCollector extends DataCollector
{
Expand All @@ -21,17 +25,25 @@ class MigrationsCollector extends DataCollector
public function __construct(DependencyFactory $dependencyFactory, MigrationsFlattener $migrationsFlattener)
{
$this->dependencyFactory = $dependencyFactory;
$this->flattener = $migrationsFlattener;
$this->flattener = $migrationsFlattener;
}

public function collect(Request $request, Response $response, \Throwable $exception = null)
public function collect(Request $request, Response $response, ?Throwable $exception = null)
Copy link
Author

Choose a reason for hiding this comment

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

Please note that running phpcbf is adding return type.

{
if (!empty($this->data)) {
if (! empty($this->data)) {
return;
}

$connection = $this->dependencyFactory->getConnection();
// before collecting migrations, we need to make sure that Application has
// at least opened a connection, otherwise it's not guaranteed that connection
// is even configured
if (! $connection->isConnected()) {
return;
}

$metadataStorage = $this->dependencyFactory->getMetadataStorage();
$planCalculator = $this->dependencyFactory->getMigrationPlanCalculator();
$planCalculator = $this->dependencyFactory->getMigrationPlanCalculator();

try {
$executedMigrations = $metadataStorage->getExecutedMigrations();
Expand All @@ -46,33 +58,29 @@ public function collect(Request $request, Response $response, \Throwable $except

$availableMigrations = $planCalculator->getMigrations();

$this->data['available_migrations_count'] = count($availableMigrations);
$unavailableMigrations = $executedMigrations->unavailableSubset($availableMigrations);
$this->data['available_migrations_count'] = count($availableMigrations);
$unavailableMigrations = $executedMigrations->unavailableSubset($availableMigrations);
$this->data['unavailable_migrations_count'] = count($unavailableMigrations);

$newMigrations = $availableMigrations->newSubset($executedMigrations);
$this->data['new_migrations'] = $this->flattener->flattenAvailableMigrations($newMigrations);
$newMigrations = $availableMigrations->newSubset($executedMigrations);
$this->data['new_migrations'] = $this->flattener->flattenAvailableMigrations($newMigrations);
$this->data['executed_migrations'] = $this->flattener->flattenExecutedMigrations($executedMigrations, $availableMigrations);

$this->data['storage'] = get_class($metadataStorage);
$configuration = $this->dependencyFactory->getConfiguration();
$storage = $configuration->getMetadataStorageConfiguration();
$configuration = $this->dependencyFactory->getConfiguration();
$storage = $configuration->getMetadataStorageConfiguration();
if ($storage instanceof TableMetadataStorageConfiguration) {
$this->data['table'] = $storage->getTableName();
$this->data['table'] = $storage->getTableName();
$this->data['column'] = $storage->getVersionColumnName();
}

$connection = $this->dependencyFactory->getConnection();
$this->data['driver'] = get_class($connection->getDriver());
$this->data['name'] = $connection->getDatabase();
$this->data['name'] = $connection->getDatabase();

$this->data['namespaces'] = $configuration->getMigrationDirectories();
}

/**
* @return string
*/
public function getName()
public function getName(): string
{
return 'doctrine_migrations';
}
Expand All @@ -82,7 +90,7 @@ public function getData()
return $this->data;
}

public function reset()
public function reset(): void
{
$this->data = [];
}
Expand Down
50 changes: 50 additions & 0 deletions Tests/Collector/MigrationsCollectorTest.php
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\Tests\Collector;

use Doctrine\Bundle\MigrationsBundle\Collector\MigrationsCollector;
use Doctrine\Bundle\MigrationsBundle\Collector\MigrationsFlattener;
use Doctrine\DBAL\Connection;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MigrationsCollectorTest extends TestCase
{
public function testCollectWithoutActiveConnection(): void
{
$dependencyFactory = $this->createMock(DependencyFactory::class);
$migrationsFlattener = $this->createMock(MigrationsFlattener::class);
$request = $this->createMock(Request::class);
$response = $this->createMock(Response::class);
$metadataStorage = $this->createMock(MetadataStorage::class);
$connection = $this->createMock(Connection::class);

$dependencyFactory
->method('getConnection')
->willReturn($connection);

$connection
->method('isConnected')
->willReturn(false);

$dependencyFactory
->expects($this->never())
->method('getMetadataStorage');

$dependencyFactory
->expects($this->never())
->method('getMigrationPlanCalculator');

$metadataStorage
->expects($this->never())
->method('getExecutedMigrations');

$target = new MigrationsCollector($dependencyFactory, $migrationsFlattener);
$target->collect($request, $response);
}
}