Skip to content

Commit

Permalink
Merge branch '3.8.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.8.x:
  PHPStan: Remove obsolete rule (#1437)
  Freeze migrations before executing sql
  Bump doctrine/.github from 4.0.0 to 5.0.1 (#1429)
  Bump ramsey/composer-install from 2 to 3 (#1428)
  Setup Dependabot for Github actions
  Upgrade codecov/codecov-action
  Ask for a namespace of when more one path
  • Loading branch information
derrabus committed Jun 26, 2024
2 parents 2f1e84f + 87a7ce0 commit 2a6b096
Show file tree
Hide file tree
Showing 19 changed files with 400 additions and 78 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "CI"
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ on:
jobs:
coding-standards:
name: "Coding Standards"
uses: "doctrine/.github/.github/workflows/coding-standards.yml@4.0.0"
uses: "doctrine/.github/.github/workflows/coding-standards.yml@5.0.1"
2 changes: 1 addition & 1 deletion .github/workflows/composer-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ on:
jobs:
composer-lint:
name: "Composer Lint"
uses: "doctrine/.github/.github/workflows/composer-lint.yml@4.0.0"
uses: "doctrine/.github/.github/workflows/composer-lint.yml@5.0.1"
6 changes: 4 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
run: "composer config minimum-stability dev"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--ignore-platform-req=php+"
Expand Down Expand Up @@ -97,6 +97,8 @@ jobs:
path: "reports"

- name: "Upload to Codecov"
uses: "codecov/codecov-action@v3"
uses: "codecov/codecov-action@v4"
with:
directory: reports
env:
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}"
2 changes: 1 addition & 1 deletion .github/workflows/release-on-milestone-closed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
release:
name: "Git tag, release & create merge-up PR"
uses: "doctrine/.github/.github/workflows/release-on-milestone-closed.yml@4.0.0"
uses: "doctrine/.github/.github/workflows/release-on-milestone-closed.yml@5.0.1"
secrets:
GIT_AUTHOR_EMAIL: ${{ secrets.GIT_AUTHOR_EMAIL }}
GIT_AUTHOR_NAME: ${{ secrets.GIT_AUTHOR_NAME }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
extensions: "pdo_sqlite"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "${{ matrix.dependencies }}"

Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ parameters:
-
message: '~^Call to an undefined method Symfony\\Component\\Console\\Output\\OutputInterface\:\:getErrorOutput\(\)\.$~'
path: src/Tools/Console/ConsoleLogger.php
-
message: '~^Method Doctrine\\Migrations\\Tests\\Stub\\DoctrineRegistry::getService\(\) should return Doctrine\\Persistence\\ObjectManager but returns Doctrine\\DBAL\\Connection\|Doctrine\\ORM\\EntityManager~'
path: tests/Stub/DoctrineRegistry.php

# https://github.com/phpstan/phpstan/issues/5982
-
Expand Down
12 changes: 12 additions & 0 deletions src/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\FrozenMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\SkipMigration;
Expand All @@ -36,6 +37,8 @@ abstract class AbstractMigration
/** @var Query[] */
private array $plannedSql = [];

private bool $frozen = false;

public function __construct(Connection $connection, private readonly LoggerInterface $logger)
{
$this->connection = $connection;
Expand Down Expand Up @@ -125,6 +128,10 @@ protected function addSql(
array $params = [],
array $types = [],
): void {
if ($this->frozen) {
throw FrozenMigration::new();
}

$this->plannedSql[] = new Query($sql, $params, $types);
}

Expand All @@ -134,6 +141,11 @@ public function getSql(): array
return $this->plannedSql;
}

public function freeze(): void
{
$this->frozen = true;
}

protected function write(string $message): void
{
$this->logger->notice($message, ['migration' => $this]);
Expand Down
15 changes: 15 additions & 0 deletions src/Exception/FrozenMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use LogicException;

final class FrozenMigration extends LogicException implements MigrationException
{
public static function new(): self
{
return new self('The migration is frozen and cannot be edited anymore.');
}
}
19 changes: 1 addition & 18 deletions src/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Doctrine\SqlFormatter\SqlFormatter;
use OutOfBoundsException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function addslashes;
use function assert;
use function class_exists;
use function count;
use function filter_var;
use function is_string;
use function key;
use function sprintf;

use const FILTER_VALIDATE_BOOLEAN;
Expand Down Expand Up @@ -107,10 +103,6 @@ protected function execute(
$allowEmptyDiff = $input->getOption('allow-empty-diff');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
$fromEmptySchema = $input->getOption('from-empty-schema');
$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

if ($formatted) {
if (! class_exists(SqlFormatter::class)) {
Expand All @@ -120,16 +112,7 @@ protected function execute(
}
}

$configuration = $this->getDependencyFactory()->getConfiguration();

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null) {
$namespace = key($dirs);
} elseif (! isset($dirs[$namespace])) {
throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();
Expand Down
38 changes: 38 additions & 0 deletions src/Tools/Console/Command/DoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function array_keys;
use function assert;
use function count;
use function is_string;
use function key;
use function sprintf;

/**
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
Expand Down Expand Up @@ -138,4 +144,36 @@ private function setNamedEmOrConnection(InputInterface $input): void
return;
}
}

final protected function getNamespace(InputInterface $input, OutputInterface $output): string
{
$configuration = $this->getDependencyFactory()->getConfiguration();

$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null && count($dirs) === 1) {
$namespace = key($dirs);
} elseif ($namespace === null && count($dirs) > 1) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Please choose a namespace (defaults to the first one)',
array_keys($dirs),
0,
);
$namespace = $helper->ask($input, $output, $question);
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
}

if (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace "%s"', $namespace));
}

assert(is_string($namespace));

return $namespace;
}
}
13 changes: 1 addition & 12 deletions src/Tools/Console/Command/DumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
use Symfony\Component\Console\Output\OutputInterface;

use function addslashes;
use function assert;
use function class_exists;
use function is_string;
use function key;
use function sprintf;
use function str_contains;

Expand Down Expand Up @@ -88,15 +85,7 @@ public function execute(
}
}

$configuration = $this->getDependencyFactory()->getConfiguration();

$namespace = $input->getOption('namespace');
if ($namespace === null) {
$dirs = $configuration->getMigrationDirectories();
$namespace = key($dirs);
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$this->checkNoPreviousDumpExistsForNamespace($namespace);

Expand Down
20 changes: 1 addition & 19 deletions src/Tools/Console/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

namespace Doctrine\Migrations\Tools\Console\Command;

use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function assert;
use function is_string;
use function key;
use function sprintf;

/**
Expand Down Expand Up @@ -44,23 +40,9 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$configuration = $this->getDependencyFactory()->getConfiguration();

$migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator();

$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null) {
$namespace = key($dirs);
} elseif (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);

Expand Down
2 changes: 2 additions & 0 deletions src/Version/DbalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private function executeMigration(
$this->addSql(new Query($sql));
}

$migration->freeze();

if (count($this->sql) !== 0) {
if (! $configuration->isDryRun()) {
$this->executeResult($configuration);
Expand Down
10 changes: 10 additions & 0 deletions tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\FrozenMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\SkipMigration;
use Doctrine\Migrations\Query\Query;
Expand Down Expand Up @@ -47,6 +48,15 @@ public function testAddSql(): void
self::assertEquals([new Query('SELECT 1', [1], [2])], $this->migration->getSql());
}

public function testThrowFrozenMigrationException(): void
{
$this->expectException(FrozenMigration::class);
$this->expectExceptionMessage('The migration is frozen and cannot be edited anymore.');

$this->migration->freeze();
$this->migration->exposedAddSql('SELECT 1', [1], [2]);
}

public function testWarnIfOutputMessage(): void
{
$this->migration->warnIf(true, 'Warning was thrown');
Expand Down
41 changes: 41 additions & 0 deletions tests/Tools/Console/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

use function array_map;
use function explode;
use function sprintf;
use function sys_get_temp_dir;
use function trim;

Expand Down Expand Up @@ -139,6 +142,44 @@ public function testExecutedUnavailableMigrationsCancel(): void
self::assertSame(3, $statusCode);
}

/** @return array<string, array{int|null, string}> */
public static function getSelectedNamespace(): array
{
return [
'no' => [null, 'FooNs'],
'first' => [0, 'FooNs'],
'two' => [1, 'FooNs2'],
];
}

/** @dataProvider getSelectedNamespace */
public function testExecuteWithMultipleDirectories(int|null $input, string $namespace): void
{
$this->migrationStatusCalculator
->method('getNewMigrations')
->willReturn(new AvailableMigrationsList([]));

$this->migrationStatusCalculator
->method('getExecutedUnavailableMigrations')
->willReturn(new ExecutedMigrationsList([]));

$this->configuration->addMigrationsDirectory('FooNs2', sys_get_temp_dir());

$this->diffCommand->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));

$this->migrationDiffGenerator->expects(self::once())->method('generate');

$this->diffCommandTester->setInputs([$input]);
$this->diffCommandTester->execute([]);

$output = $this->diffCommandTester->getDisplay(true);

self::assertStringContainsString('Please choose a namespace (defaults to the first one)', $output);
self::assertStringContainsString('[0] FooNs', $output);
self::assertStringContainsString('[1] FooNs2', $output);
self::assertStringContainsString(sprintf('You have selected the "%s" namespace', $namespace), $output);
}

protected function setUp(): void
{
$this->migrationDiffGenerator = $this->createMock(DiffGenerator::class);
Expand Down
Loading

0 comments on commit 2a6b096

Please sign in to comment.