From 225edaa323c10787e5d35c9222a7c15ed6655bd5 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Tue, 27 Jul 2021 15:58:22 +0200 Subject: [PATCH] the write-sql option can be a filename --- .../Tools/Console/Command/ExecuteCommand.php | 10 +++++++++- .../Tools/Console/Command/MigrateCommand.php | 10 +++++++++- .../Tests/Tools/Console/Command/ExecuteCommandTest.php | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/ExecuteCommand.php index afc031fa22..77f5d9dcc8 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 ffb2329c5b..977b68cf69 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 0c6a523b9d..f299f44fd1 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'], ]; }