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

Remove migration table from generated comparison schema #1418

Open
wants to merge 4 commits into
base: 3.7.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Event\Listeners;

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Doctrine\ORM\Tools\ToolEvents;

class RemoveMigrationTableFromSchemaListener implements EventSubscriber
{
private Configuration $configuration;

public function __construct(
DependencyFactory $dependencyFactory,
) {
$this->configuration = $dependencyFactory->getConfiguration();
}

/** {@inheritDoc} */
public function getSubscribedEvents()

Check warning on line 26 in lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php#L26

Added line #L26 was not covered by tests
{
return [
ToolEvents::postGenerateSchema,
ToolEvents::postGenerateComparisonSchema,

Check failure on line 30 in lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Access to undefined constant Doctrine\ORM\Tools\ToolEvents::postGenerateComparisonSchema.
];

Check warning on line 31 in lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php#L28-L31

Added lines #L28 - L31 were not covered by tests
}

public function postGenerateSchema(GenerateSchemaEventArgs $args): void
{
$this->removeMigrationsTableFromSchema($args->getSchema());
}

public function postGenerateComparisonSchema(GenerateSchemaEventArgs $args): void

Check warning on line 39 in lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php#L39

Added line #L39 was not covered by tests
{
$this->removeMigrationsTableFromSchema($args->getSchema());

Check warning on line 41 in lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php#L41

Added line #L41 was not covered by tests
}

private function removeMigrationsTableFromSchema(Schema $schema): void
{
$metadataConfiguration = $this->configuration->getMetadataStorageConfiguration();

if (! ($metadataConfiguration instanceof TableMetadataStorageConfiguration)) {
return;
}

$tableName = $metadataConfiguration->getTableName();

if (! $schema->hasTable($tableName)) {
return;
}

$schema->dropTable($tableName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getPlanForVersions(array $versions, string $direction): Migratio
$availableMigrations = array_filter(
$migrationsToCheck,
// in_array third parameter is intentionally false to force object to string casting
static fn (AvailableMigration $availableMigration): bool => in_array($availableMigration->getVersion(), $versions, false)
static fn (AvailableMigration $availableMigration): bool => in_array($availableMigration->getVersion(), $versions, false),
);

$planItems = array_map(static fn (AvailableMigration $availableMigration): MigrationPlan => new MigrationPlan($availableMigration->getVersion(), $availableMigration->getMigration(), $direction), $availableMigrations);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Event\Listeners;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Event\Listeners\RemoveMigrationTableFromSchemaListener;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Tests\MigrationTestCase;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;

class RemoveMigrationTableFromSchemaListenerTest extends MigrationTestCase
{
private RemoveMigrationTableFromSchemaListener $listener;
private Configuration $configuration;
private EntityManagerInterface $entityManager;

public function testListenerRemovesMigrationSchema(): void
{
$metadataConfiguration = new TableMetadataStorageConfiguration();
$tableName = $metadataConfiguration->getTableName();
$this->configuration->setMetadataStorageConfiguration($metadataConfiguration);

$schema = new Schema();
$schema->createTable($tableName);

static::assertTrue($schema->hasTable($tableName));

$this->listener->postGenerateSchema(new GenerateSchemaEventArgs($this->entityManager, $schema));

static::assertFalse($schema->hasTable($tableName));
}

public function testListenerIgnoresMissingTable(): void
{
$metadataConfiguration = new TableMetadataStorageConfiguration();
$tableName = $metadataConfiguration->getTableName();
$this->configuration->setMetadataStorageConfiguration($metadataConfiguration);

$schema = new Schema();

static::assertFalse($schema->hasTable($tableName));

$this->listener->postGenerateSchema(new GenerateSchemaEventArgs($this->entityManager, $schema));

static::assertFalse($schema->hasTable($tableName));
}

public function testListenerIgnoresMissingConfiguration(): void
{
static::expectNotToPerformAssertions();

$this->listener->postGenerateSchema(new GenerateSchemaEventArgs($this->entityManager, new Schema()));
}

protected function setUp(): void
{
$this->entityManager = $this->createMock(EntityManager::class);

$this->configuration = new Configuration();

$dependencyFactory = DependencyFactory::fromEntityManager(
new ExistingConfiguration($this->configuration),
new ExistingEntityManager($this->entityManager),
);

$this->listener = new RemoveMigrationTableFromSchemaListener($dependencyFactory);
}
}