Skip to content

Commit

Permalink
add option to drop views during DropCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
torhoehn authored and cmuench committed Jan 19, 2023
1 parent 46b35dc commit b0f62ca
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/N98/Magento/Command/Database/DropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected function configure()
$this
->setName('db:drop')
->addOption('tables', 't', InputOption::VALUE_NONE, 'Drop all tables instead of dropping the database')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force (all passed options will be forced)')
->addOption('views', 'v', InputOption::VALUE_NONE, 'Also drop views instead of database/tables only')
->setDescription('Drop current database');

$help = <<<HELP
Expand Down Expand Up @@ -50,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dbHelper = $this->getHelper('database');

if ($input->getOption('force')) {
$shouldDrop = true;
$dropIsConfirmed = true;
} else {
$question = new ConfirmationQuestion(
sprintf(
Expand All @@ -60,19 +61,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
false
);

$shouldDrop = $questionHelper->ask(
$dropIsConfirmed = $questionHelper->ask(
$input,
$output,
$question
);
}

if ($shouldDrop) {
if ($dropIsConfirmed) {
if ($input->getOption('tables')) {
$dbHelper->dropTables($output);
} else {
$dbHelper->dropDatabase($output);
}

if ($input->getOption('views')) {
$dbHelper->dropViews($output);
}
}

return Command::SUCCESS;
Expand Down
71 changes: 71 additions & 0 deletions src/N98/Util/Console/Helper/DatabaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,60 @@ public function getTables($withoutPrefix = null)
return $result;
}

/**
* Get list of database tables
*
* @param bool|null $withoutPrefix [optional] remove prefix from the returned table names. prefix is obtained from
* magento database configuration. defaults to false.
*
* @return array
* @throws RuntimeException
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function getViews($withoutPrefix = null)
{
$withoutPrefix = (bool) $withoutPrefix;

$db = $this->getConnection();
$prefix = $this->dbSettings['prefix'];
$prefixLength = strlen($prefix);

$column = $columnName = 'table_name';

$input = [];

if ($withoutPrefix && $prefixLength) {
$column = sprintf('SUBSTRING(%1$s FROM 1 + CHAR_LENGTH(:name)) %1$s', $columnName);
$input[':name'] = $prefix;
}

$condition = 'table_schema = database()';

if ($prefixLength) {
$escape = '=';
$condition .= sprintf(" AND %s LIKE :like ESCAPE '%s'", $columnName, $escape);
$input[':like'] = $this->quoteLike($prefix, $escape) . '%';
}

$condition .= " AND LIKE 'view%'";

$query = sprintf('SELECT %s FROM information_schema.views WHERE %s;', $column, $condition);
$statement = $db->prepare($query, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$result = $statement->execute($input);

if (!$result) {
// @codeCoverageIgnoreStart
$this->throwRuntimeException(
$statement,
sprintf('Failed to obtain tables from database: %s', var_export($query, true))
);
} // @codeCoverageIgnoreEnd

$result = $statement->fetchAll(PDO::FETCH_COLUMN, 0);

return $result;
}

/**
* throw a runtime exception and provide error info for the statement if available
*
Expand Down Expand Up @@ -747,6 +801,23 @@ public function dropTables(OutputInterface $output)
$output->writeln('<info>Dropped database tables</info> <comment>' . $count . ' tables dropped</comment>');
}

/**
* @param OutputInterface $output
* @return void
*/
public function dropViews(OutputInterface $output)
{
$result = $this->getViews();
$query = '';
$count = 0;
foreach ($result as $viewName) {
$query .= 'DROP VIEW IF EXISTS `' . $viewName . '`; ';
$count++;
}
$this->getConnection()->query($query);
$output->writeln('<info>Dropped database views</info> <comment>' . $count . ' views dropped</comment>');
}

/**
* @param OutputInterface $output
* @throws \Magento\Framework\Exception\FileSystemException
Expand Down

0 comments on commit b0f62ca

Please sign in to comment.