Skip to content

Commit

Permalink
Merge pull request #952 from goetas/default-down-migration
Browse files Browse the repository at this point in the history
Make the down() migration optional
  • Loading branch information
goetas authored Mar 28, 2020
2 parents aa89dfb + b21086a commit 96a6832
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Here is a list of the most important changes:
use `Doctrine\Migrations\DependencyFactory` instead
- Namespace `Doctrine\Migrations\AbstractMigration`
- CHANGED: The method `Doctrine\Migrations\AbstractMigration#__construct()` changed signature into `(Doctrine\DBAL\Connection $conn, PSR\Log\LoggerInterface $logger)`
- CHANGED: The method `Doctrine\Migrations\AbstractMigration#down()` is not abstract anymore, the default implementation will abort the migration process
- REMOVED: Property `Doctrine\Migrations\AbstractMigration#$version` was removed
- Namespace `Doctrine\Migrations\Provider`
- REMOVED: Class `Doctrine\Migrations\Provider\SchemaProviderInterface` has been deleted
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\Migrations\Exception\SkipMigration;
use Doctrine\Migrations\Query\Query;
use Psr\Log\LoggerInterface;
use function sprintf;

/**
* The AbstractMigration class is for end users to extend from when creating migrations. Extend this class
Expand Down Expand Up @@ -129,7 +130,10 @@ abstract public function up(Schema $schema) : void;
/**
* @throws MigrationException|DBALException
*/
abstract public function down(Schema $schema) : void;
public function down(Schema $schema) : void
{
$this->abortIf(true, sprintf('No down() migration implemented for "%s"', static::class));
}

/**
* @param mixed[] $params
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Doctrine\Migrations\Tests;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\SkipMigration;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tests\Stub\AbstractMigrationStub;
use Doctrine\Migrations\Tests\Stub\AbstractMigrationWithoutDownStub;

class AbstractMigrationTest extends MigrationTestCase
{
Expand All @@ -25,6 +27,16 @@ protected function setUp() : void
$this->migration = new AbstractMigrationStub($this->getSqliteConnection(), $this->logger);
}

public function testDownMigrationIsOptional() : void
{
$this->expectException(AbortMigration::class);
$this->expectExceptionMessage('No down() migration implemented for "Doctrine\Migrations\Tests\Stub\AbstractMigrationWithoutDownStub"');

$migration = new AbstractMigrationWithoutDownStub($this->getSqliteConnection(), $this->logger);
$schema = $this->createStub(Schema::class);
$migration->down($schema);
}

public function testGetDescriptionReturnsEmptyString() : void
{
self::assertSame('', $this->migration->getDescription());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Stub;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

class AbstractMigrationWithoutDownStub extends AbstractMigration
{
public function up(Schema $schema) : void
{
}
}

0 comments on commit 96a6832

Please sign in to comment.