From b0f62cadade8ac60b9ecb4e5b8159acd7c5b2f8e Mon Sep 17 00:00:00 2001 From: torhoehn Date: Wed, 21 Sep 2022 21:13:17 +0200 Subject: [PATCH] add option to drop views during DropCommand --- .../Magento/Command/Database/DropCommand.php | 13 ++-- .../Util/Console/Helper/DatabaseHelper.php | 71 +++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/N98/Magento/Command/Database/DropCommand.php b/src/N98/Magento/Command/Database/DropCommand.php index 9f2422244..7fb068891 100755 --- a/src/N98/Magento/Command/Database/DropCommand.php +++ b/src/N98/Magento/Command/Database/DropCommand.php @@ -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 = <<getHelper('database'); if ($input->getOption('force')) { - $shouldDrop = true; + $dropIsConfirmed = true; } else { $question = new ConfirmationQuestion( sprintf( @@ -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; diff --git a/src/N98/Util/Console/Helper/DatabaseHelper.php b/src/N98/Util/Console/Helper/DatabaseHelper.php index 2f5b2926c..60b6368bf 100644 --- a/src/N98/Util/Console/Helper/DatabaseHelper.php +++ b/src/N98/Util/Console/Helper/DatabaseHelper.php @@ -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 * @@ -747,6 +801,23 @@ public function dropTables(OutputInterface $output) $output->writeln('Dropped database tables ' . $count . ' tables dropped'); } + /** + * @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('Dropped database views ' . $count . ' views dropped'); + } + /** * @param OutputInterface $output * @throws \Magento\Framework\Exception\FileSystemException