diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php index afc031fa2..77f5d9dcc 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php @@ -12,8 +12,10 @@ use Symfony\Component\Console\Output\OutputInterface; use function array_map; +use function dirname; use function getcwd; use function implode; +use function is_dir; use function is_string; use function is_writable; use function sprintf; @@ -130,7 +132,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int : Direction::UP; $path = $input->getOption('write-sql') ?? getcwd(); - if (is_string($path) && ! is_writable($path)) { + + if (is_string($path) && ! $this->isPathWritable($path)) { $this->io->error(sprintf('The path "%s" not writeable!', $path)); return 1; @@ -161,4 +164,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + + private function isPathWritable(string $path): bool + { + return is_writable($path) || is_dir($path) || is_writable(dirname($path)); + } } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php index ffb2329c5..977b68cf6 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php @@ -15,8 +15,10 @@ use Symfony\Component\Console\Output\OutputInterface; use function count; +use function dirname; use function getcwd; use function in_array; +use function is_dir; use function is_string; use function is_writable; use function sprintf; @@ -143,7 +145,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $versionAlias = $input->getArgument('version'); $path = $input->getOption('write-sql') ?? getcwd(); - if (is_string($path) && ! is_writable($path)) { + + if (is_string($path) && ! $this->isPathWritable($path)) { $this->io->error(sprintf('The path "%s" not writeable!', $path)); return 1; @@ -279,4 +282,9 @@ private function exitForAlias(string $versionAlias): int return 0; } + + private function isPathWritable(string $path): bool + { + return is_writable($path) || is_dir($path) || is_writable(dirname($path)); + } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php index 0c6a523b9..f299f44fd 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/ExecuteCommandTest.php @@ -94,10 +94,12 @@ public function getWriteSqlValues(): array [true, false, null], [true, null, getcwd()], [true, __DIR__ . '/_files', __DIR__ . '/_files'], + [true, __DIR__ . '/_files/run.sql', __DIR__ . '/_files/run.sql'], [false, false, null], [false, null, getcwd()], [false, __DIR__ . '/_files', __DIR__ . '/_files'], + [true, __DIR__ . '/_files/run.sql', __DIR__ . '/_files/run.sql'], ]; }