-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from absolvent/issue/ATP-679
ATP-679 universal drop all tables command
- Loading branch information
Showing
4 changed files
with
660 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Absolvent\api\Console\Commands\Db; | ||
|
||
use Illuminate\Console\Command; | ||
use DB; | ||
|
||
final class DbDropTables extends Command | ||
{ | ||
const SIGNATURE = 'db:drop:tables {--y|yes}'; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = self::SIGNATURE; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Drop all database tables (RUN ONLY IN NON-PRODUCTION ENV)'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
if (env('APP_ENV') === 'production') { | ||
$this->output->writeln("Cannot run this command in production!"); | ||
return -2; | ||
} | ||
|
||
if (!$this->input->getOption('yes') && !$this->confirm('Confirm drop all tables in the database?')) { | ||
$this->output->error('Drop Tables command aborted'); | ||
return -1; | ||
} | ||
|
||
$tables = collect(DB::select('SHOW TABLES')); | ||
|
||
if ($tables->isEmpty()) { | ||
$this->output->warning('No tables to drop'); | ||
return 0; | ||
} | ||
|
||
$dropList = $tables->map(function ($table) { | ||
return head((array) $table); | ||
})->implode(','); | ||
|
||
DB::statement('SET FOREIGN_KEY_CHECKS = 0'); | ||
DB::statement("DROP TABLE $dropList"); | ||
DB::statement('SET FOREIGN_KEY_CHECKS = 1'); | ||
|
||
$this->comment('All tables dropped'); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.