Unverified Commit 4e92005c authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3241 from morozov/issues/3237

Removed dbal:import CLI command
parents fbc7fb6b d3f7686e
# Upgrade to 3.0 # Upgrade to 3.0
## BC BREAK: Removed dbal:import CLI command
The `dbal:import` CLI command has been removed since it only worked with PDO-based drivers by relying on a non-documented behavior of the extension, and it was impossible to make it work with other drivers.
Please use other database client applications for import, e.g.:
* For MySQL and MariaDB: `mysql [dbname] < data.sql`.
* For PostgreSQL: `psql [dbname] < data.sql`.
* For SQLite: `sqlite3 /path/to/file.db < data.sql`.
## BC BREAK: Removed support for DB-generated UUIDs ## BC BREAK: Removed support for DB-generated UUIDs
The support for DB-generated UUIDs was removed as non-portable. The support for DB-generated UUIDs was removed as non-portable.
......
<?php
namespace Doctrine\DBAL\Tools\Console\Command;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOStatement;
use InvalidArgumentException;
use PDOException;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use const PHP_EOL;
use function assert;
use function file_exists;
use function file_get_contents;
use function is_readable;
use function realpath;
use function sprintf;
/**
* Task for executing arbitrary SQL that can come from a file or directly from
* the command line.
*
* @deprecated Use a database client application instead
*/
class ImportCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('dbal:import')
->setDescription('Import SQL file(s) directly to Database.')
->setDefinition([new InputArgument(
'file',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'File path(s) of SQL to be executed.'
),
])
->setHelp(<<<EOT
Import SQL file(s) directly to Database.
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$conn = $this->getHelper('db')->getConnection();
$fileNames = $input->getArgument('file');
if ($fileNames === null) {
return null;
}
foreach ((array) $fileNames as $fileName) {
$filePath = realpath($fileName);
// Phar compatibility.
if ($filePath === false) {
$filePath = $fileName;
}
if (! file_exists($filePath)) {
throw new InvalidArgumentException(
sprintf("SQL file '<info>%s</info>' does not exist.", $filePath)
);
} elseif (! is_readable($filePath)) {
throw new InvalidArgumentException(
sprintf("SQL file '<info>%s</info>' does not have read permissions.", $filePath)
);
}
$output->write(sprintf("Processing file '<info>%s</info>'... ", $filePath));
$sql = file_get_contents($filePath);
if ($conn instanceof PDOConnection) {
// PDO Drivers
try {
$lines = 0;
$stmt = $conn->prepare($sql);
assert($stmt instanceof PDOStatement);
$stmt->execute();
do {
// Required due to "MySQL has gone away!" issue
$stmt->fetch();
$stmt->closeCursor();
$lines++;
} while ($stmt->nextRowset());
$output->write(sprintf('%d statements executed!', $lines) . PHP_EOL);
} catch (PDOException $e) {
$output->write('error!' . PHP_EOL);
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}
} else {
// Non-PDO Drivers (ie. OCI8 driver)
$stmt = $conn->prepare($sql);
$rs = $stmt->execute();
if (! $rs) {
$error = $stmt->errorInfo();
$output->write('error!' . PHP_EOL);
throw new RuntimeException($error[2], $error[0]);
}
$output->writeln('OK!' . PHP_EOL);
$stmt->closeCursor();
}
}
return null;
}
}
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace Doctrine\DBAL\Tools\Console; namespace Doctrine\DBAL\Tools\Console;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand; use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand; use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
...@@ -56,7 +55,6 @@ class ConsoleRunner ...@@ -56,7 +55,6 @@ class ConsoleRunner
{ {
$cli->addCommands([ $cli->addCommands([
new RunSqlCommand(), new RunSqlCommand(),
new ImportCommand(),
new ReservedWordsCommand(), new ReservedWordsCommand(),
]); ]);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment