Skip to content

Commit 68bd94a

Browse files
authored
Fix #6319. Block piping to sql:cli command (#6436)
* Block piping to sql:cli command * PHPCS
1 parent 5a216fc commit 68bd94a

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/Commands/sql/SqlCliCommand.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Drush\Commands\sql;
66

7-
use Consolidation\SiteProcess\Util\Tty;
87
use Drush\Attributes as CLI;
98
use Drush\Boot\DrupalBootLevels;
109
use Drush\Command\HelpLinks;
@@ -19,7 +18,6 @@
1918
use Symfony\Component\Console\Command\Command;
2019
use Symfony\Component\Console\Input\InputInterface;
2120
use Symfony\Component\Console\Input\InputOption;
22-
use Symfony\Component\Console\Input\StreamableInputInterface;
2321
use Symfony\Component\Console\Output\OutputInterface;
2422

2523
#[AsCommand(
@@ -57,6 +55,10 @@ protected function configure(): void
5755

5856
public function execute(InputInterface $input, OutputInterface $output): int
5957
{
58+
if ($this->hasPipedInput()) {
59+
throw new RuntimeException('Instead of piping SQL to sql:cli, it is faster to use sql:connect. See the Examples at https://www.drush.org/latest/commands/sql_connect/#examples');
60+
}
61+
6062
$sql = SqlBase::create($input->getOptions());
6163
$program = $sql->command();
6264
if (!self::programExists($program)) {
@@ -65,19 +67,25 @@ public function execute(InputInterface $input, OutputInterface $output): int
6567
}
6668

6769
$process = $this->processManager->shell($sql->connect(), null, $sql->getEnv());
68-
if (!Tty::isTtySupported()) {
69-
$this->logger->warning('It is slow to pass large amounts of data via stdin to the sql:cli command. See the Examples at https://www.drush.org/latest/commands/sql_cli/ for an alternative using sql:connect.');
70-
// See https://github.com/symfony/symfony/issues/37835#issuecomment-674386588.
71-
// If testing this will get input added by `CommandTester::setInputs` method.
72-
$inputStream = ($input instanceof StreamableInputInterface) ? $input->getStream() : null;
73-
// If nothing from input stream use STDIN instead.
74-
$inputStream = $inputStream ?? STDIN;
75-
// $data = stream_get_contents($inputStream);
76-
$process->setInput($inputStream);
77-
} else {
78-
$process->setTty((bool) $this->drushConfig->get('ssh.tty', $input->isInteractive()));
79-
}
70+
// No longer needed?
71+
// if (Tty::isTtySupported()) {
72+
// $process->setTty((bool) $this->drushConfig->get('ssh.tty', $input->isInteractive()));
73+
// }
8074
$process->mustRun($process->showRealtime());
8175
return Command::SUCCESS;
8276
}
77+
78+
/**
79+
* Test if there is input waiting on STDIN
80+
*/
81+
protected function hasPipedInput(): bool
82+
{
83+
$streams = [STDIN]; // note STDIN here is not a string
84+
$write_array = [];
85+
$except_array = [];
86+
$seconds = 0; // zero seconds on timeout since this is just for testing stream change
87+
$streamCount = @stream_select($streams, $write_array, $except_array, $seconds);
88+
89+
return (bool) $streamCount;
90+
}
8391
}

tests/integration/SqlCliTest.php renamed to tests/integration/SqlCommandsTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Unish;
1111

12+
use Drush\Commands\sql\SqlCliCommand;
1213
use Drush\Commands\sql\SqlCommands;
1314
use Drush\Sql\SqlBase;
1415
use Symfony\Component\Filesystem\Path;
@@ -17,18 +18,21 @@
1718
* @group commands
1819
* @group sql
1920
*/
20-
class SqlCliTest extends UnishIntegrationTestCase
21+
class SqlCommandsTest extends UnishIntegrationTestCase
2122
{
22-
public function testSqlCli()
23+
public function testSqlQuery()
2324
{
24-
if ($this->isWindows()) {
25-
$this->markTestSkipped('sql:cli stdin tests do not work on Windows.');
26-
}
2725

2826
// @todo Ensure SQL dumps can be imported via sql:cli via stdin.
2927
$this->drush(SqlCommands::QUERY, [], ['file' => Path::join(__DIR__, 'resources/sqlcli.sql')], self::EXIT_SUCCESS);
3028
$sql = SqlBase::create();
3129
$tables = $sql->listTables();
3230
$this->assertContains('sqlcli', $tables);
3331
}
32+
33+
public function testSqlCli()
34+
{
35+
// @todo Ensure SQL dumps *cannot* be imported via sql:cli via stdin.
36+
$this->drush(SqlCliCommand::NAME, [], ['file' => Path::join(__DIR__, 'resources/sqlcli.sql')], self::EXIT_ERROR);
37+
}
3438
}

0 commit comments

Comments
 (0)