diff --git a/composer.json b/composer.json index 339a790..92b7c43 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "doctrine/orm": "^2.17 || ^3.0", "symfony/doctrine-bridge": "^6.4 || ^7.0", "doctrine/dbal": "^3.4", - "macpaw/schema-context-bundle": "^1.1" + "macpaw/schema-context-bundle": "^2.0" }, "require-dev": { "doctrine/migrations": "^3.6", diff --git a/src/Command/Doctrine/AbstractDoctrineSchemaCommand.php b/src/Command/Doctrine/AbstractDoctrineSchemaCommand.php index c0d93e0..ff142a8 100644 --- a/src/Command/Doctrine/AbstractDoctrineSchemaCommand.php +++ b/src/Command/Doctrine/AbstractDoctrineSchemaCommand.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Error; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -15,6 +16,7 @@ abstract class AbstractDoctrineSchemaCommand extends Command public function __construct( string $commandName, protected readonly Connection $connection, + protected readonly BaggageSchemaResolver $schemaResolver, ) { parent::__construct($commandName); } @@ -53,6 +55,7 @@ protected function isSchemaExist(string $schema): bool protected function switchToSchema(string $schema): void { + $this->schemaResolver->setSchema($schema); $quotedSchema = $this->connection->quoteIdentifier($schema); $this->connection->executeStatement("SET search_path TO {$quotedSchema}"); diff --git a/src/Command/Doctrine/AbstractNestingDoctrineSchemaCommand.php b/src/Command/Doctrine/AbstractNestingDoctrineSchemaCommand.php index 4d63051..ab9e95c 100644 --- a/src/Command/Doctrine/AbstractNestingDoctrineSchemaCommand.php +++ b/src/Command/Doctrine/AbstractNestingDoctrineSchemaCommand.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Error; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; @@ -19,8 +20,9 @@ public function __construct( string $commandName, private readonly Command $parentCommand, Connection $connection, + BaggageSchemaResolver $schemaResolver, ) { - parent::__construct($commandName, $connection); + parent::__construct($commandName, $connection, $schemaResolver); } protected function configure(): void @@ -76,10 +78,6 @@ protected function runCommand(string $commandName, InputInterface $input, Output $options = []; foreach ($input->getOptions() as $name => $value) { - if ($value === null) { - continue; - } - if ($this->getDefinition()->getOptions()[$name]->getDefault() === $value) { continue; } diff --git a/src/Command/Doctrine/DoctrineSchemaDropCommand.php b/src/Command/Doctrine/DoctrineSchemaDropCommand.php index 91102f1..562b723 100644 --- a/src/Command/Doctrine/DoctrineSchemaDropCommand.php +++ b/src/Command/Doctrine/DoctrineSchemaDropCommand.php @@ -5,6 +5,7 @@ namespace Macpaw\PostgresSchemaBundle\Command\Doctrine; use Doctrine\DBAL\Connection; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -16,9 +17,10 @@ class DoctrineSchemaDropCommand extends AbstractDoctrineSchemaCommand */ public function __construct( Connection $connection, + BaggageSchemaResolver $schemaResolver, private readonly array $disallowedSchemaNames = [], ) { - parent::__construct('doctrine:database:schema:drop', $connection); + parent::__construct('doctrine:database:schema:drop', $connection, $schemaResolver); } protected function execute( diff --git a/src/Command/Doctrine/DoctrineSchemaFixturesLoadCommand.php b/src/Command/Doctrine/DoctrineSchemaFixturesLoadCommand.php index d3400b8..2a3ef07 100644 --- a/src/Command/Doctrine/DoctrineSchemaFixturesLoadCommand.php +++ b/src/Command/Doctrine/DoctrineSchemaFixturesLoadCommand.php @@ -6,6 +6,7 @@ use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand; use Doctrine\DBAL\Connection; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -19,9 +20,10 @@ class DoctrineSchemaFixturesLoadCommand extends AbstractNestingDoctrineSchemaCom public function __construct( LoadDataFixturesDoctrineCommand $parentCommand, Connection $connection, + BaggageSchemaResolver $schemaResolver, private readonly array $disallowedSchemaNames = [], ) { - parent::__construct('doctrine:schema:fixtures:load', $parentCommand, $connection); + parent::__construct('doctrine:schema:fixtures:load', $parentCommand, $connection, $schemaResolver); } protected function execute( diff --git a/src/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommand.php b/src/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommand.php index 547b1b6..3d7d9e7 100644 --- a/src/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommand.php +++ b/src/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommand.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -16,8 +17,9 @@ class DoctrineSchemaMigrationsMigrateCommand extends AbstractNestingDoctrineSche public function __construct( MigrateCommand $parentCommand, Connection $connection, + BaggageSchemaResolver $schemaResolver, ) { - parent::__construct('doctrine:schema:migrations:migrate', $parentCommand, $connection); + parent::__construct('doctrine:schema:migrations:migrate', $parentCommand, $connection, $schemaResolver); } protected function execute( diff --git a/tests/Command/Doctrine/DoctrineSchemaDropCommandTest.php b/tests/Command/Doctrine/DoctrineSchemaDropCommandTest.php index 7a1dbf1..7965ebc 100644 --- a/tests/Command/Doctrine/DoctrineSchemaDropCommandTest.php +++ b/tests/Command/Doctrine/DoctrineSchemaDropCommandTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Macpaw\PostgresSchemaBundle\Command\Doctrine\DoctrineSchemaDropCommand; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Command\Command; @@ -20,7 +21,8 @@ class DoctrineSchemaDropCommandTest extends TestCase protected function setUp(): void { $this->connection = $this->createMock(Connection::class); - $this->command = new DoctrineSchemaDropCommand($this->connection, ['public']); + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); + $this->command = new DoctrineSchemaDropCommand($this->connection, $resolver, ['public']); } public function testSuccess(): void diff --git a/tests/Command/Doctrine/DoctrineSchemaFixturesLoadCommandTest.php b/tests/Command/Doctrine/DoctrineSchemaFixturesLoadCommandTest.php index 7125640..6d96f6d 100644 --- a/tests/Command/Doctrine/DoctrineSchemaFixturesLoadCommandTest.php +++ b/tests/Command/Doctrine/DoctrineSchemaFixturesLoadCommandTest.php @@ -7,6 +7,7 @@ use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand; use Doctrine\DBAL\Connection; use Macpaw\PostgresSchemaBundle\Command\Doctrine\DoctrineSchemaFixturesLoadCommand; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; @@ -33,8 +34,14 @@ protected function setUp(): void ->willReturn(new InputDefinition([ new InputOption('no-interaction'), ])); + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); - $this->command = new DoctrineSchemaFixturesLoadCommand($this->parentCommand, $this->connection, ['public']); + $this->command = new DoctrineSchemaFixturesLoadCommand( + $this->parentCommand, + $this->connection, + $resolver, + ['public'] + ); $this->command->setApplication($this->application); } diff --git a/tests/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommandTest.php b/tests/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommandTest.php index 61acefc..0533070 100644 --- a/tests/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommandTest.php +++ b/tests/Command/Doctrine/DoctrineSchemaMigrationsMigrateCommandTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Connection; use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; use Macpaw\PostgresSchemaBundle\Command\Doctrine\DoctrineSchemaMigrationsMigrateCommand; +use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; @@ -32,8 +33,9 @@ protected function setUp(): void ->willReturn(new InputDefinition([ new InputOption('no-interaction'), ])); + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); - $this->command = new DoctrineSchemaMigrationsMigrateCommand($this->parentCommand, $this->connection); + $this->command = new DoctrineSchemaMigrationsMigrateCommand($this->parentCommand, $this->connection, $resolver); $this->application = $this->createMock(Application::class); $this->command->setApplication($this->application); } diff --git a/tests/Doctrine/SchemaConnectionTest.php b/tests/Doctrine/SchemaConnectionTest.php index e114ce7..ba03ebc 100644 --- a/tests/Doctrine/SchemaConnectionTest.php +++ b/tests/Doctrine/SchemaConnectionTest.php @@ -38,7 +38,7 @@ public function testConnectSetsSearchPath(): void $connection->method('getDatabasePlatform')->willReturn($platform); $connection->method('fetchOne')->willReturn(true); - $resolver = new BaggageSchemaResolver(); + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); $resolver->setSchema('test_schema'); @@ -70,7 +70,7 @@ public function testConnectSetsSearchPathWithSpecialChars(): void $connection->method('getDatabasePlatform')->willReturn($platform); $connection->method('fetchOne')->willReturn(true); - $resolver = new BaggageSchemaResolver(); + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); $resolver->setSchema('test-schema/foo'); @@ -85,12 +85,24 @@ public function testConnectSkipsWhenNoSchema(): void { $driverConnection = $this->createMock(DriverConnection::class); + $driverConnection->expects($this->once()) + ->method('exec') + ->with('SET search_path TO "public"'); + $driver = $this->createMock(Driver::class); $driver->method('connect')->willReturn($driverConnection); - $connection = new SchemaConnection([], $driver, new Configuration()); - $resolver = new BaggageSchemaResolver(); + $platform = new PostgreSQLPlatform(); + $connection = $this->getMockBuilder(SchemaConnection::class) + ->setConstructorArgs([[], $driver, new Configuration(), new EventManager()]) + ->onlyMethods(['getDatabasePlatform', 'fetchOne']) + ->getMock(); + + $connection->method('getDatabasePlatform')->willReturn($platform); + $connection->method('fetchOne')->willReturn(true); + + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); SchemaConnection::setSchemaResolver($resolver); @@ -114,7 +126,7 @@ public function testThrowsForUnsupportedPlatform(): void $connection->method('getDatabasePlatform')->willReturn($platform); - $resolver = new BaggageSchemaResolver(); + $resolver = new BaggageSchemaResolver('public', 'development', ['development']); $resolver->setSchema('test_schema');