53 lines
1.9 KiB
PHP
Executable File
53 lines
1.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
class DbWipeCommand extends Command
|
|
{
|
|
protected static $defaultName = 'db:wipe';
|
|
protected static $defaultDescription = 'Drop all tables, views, and types';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->setHelp('This command drops all tables from the database without removing the database itself.')
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the operation to run');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
if (!$input->getOption('force')) {
|
|
$output->writeln('<error>Atenção: Este comando irá apagar TODAS as tabelas do banco de dados!</error>');
|
|
$output->writeln('<comment>Use a opção --force para confirmar.</comment>');
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$output->writeln('<info>Limpando banco de dados...</info>');
|
|
|
|
$schema = Capsule::schema();
|
|
$tables = Capsule::connection()->select('SHOW FULL TABLES WHERE Table_type = "BASE TABLE"');
|
|
$dbName = Capsule::connection()->getDatabaseName();
|
|
$key = "Tables_in_{$dbName}";
|
|
|
|
Capsule::connection()->statement('SET FOREIGN_KEY_CHECKS = 0');
|
|
|
|
foreach ($tables as $table) {
|
|
$tableName = $table->$key;
|
|
$output->write("Dropando tabela: <comment>{$tableName}</comment>... ");
|
|
$schema->drop($tableName);
|
|
$output->writeln('<info>OK</info>');
|
|
}
|
|
|
|
Capsule::connection()->statement('SET FOREIGN_KEY_CHECKS = 1');
|
|
|
|
$output->writeln('<info>Banco de dados limpo com sucesso!</info>');
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|