Skip to content

Commit

Permalink
Merge pull request #3 from absolvent/issue/ATP-679
Browse files Browse the repository at this point in the history
ATP-679 universal drop all tables command
  • Loading branch information
majkel89 authored Feb 16, 2018
2 parents c53350f + fee078d commit d8b7a52
Show file tree
Hide file tree
Showing 4 changed files with 660 additions and 400 deletions.
62 changes: 62 additions & 0 deletions app/Console/Commands/Db/DbDropTables.php
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;
}
}
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"laravel/framework": "5.4.*",
"league/fractal": "^0.15.0",
"league/uri": "^5.0",
"riverline/multipart-parser": "^1.1",
"symfony/yaml": "^3.2"
},
"require-dev": {
Expand All @@ -32,6 +33,12 @@
}
},
"scripts": {
"build": [

],
"clean": [

],
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
Expand Down
Loading

0 comments on commit d8b7a52

Please sign in to comment.