Completed
Push — master ( 34b930...ab3e16 )
by Avtandil
05:13
created

DbClear::getDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\Commands;
13
14
use DB;
15
use Illuminate\Console\Command;
16
use Illuminate\Console\ConfirmableTrait;
17
18
class DbClear extends Command
19
{
20
    use ConfirmableTrait;
21
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'db:clear {--database= : The database connection to use.}
28
                {--force : Force the operation to run when in production.}
29
                {--pretend : Dump the SQL queries that would be run.}';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Clear database';
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        if (! $this->confirmToProceed('Application In Production! Will be dropped all tables!')) {
46
            return;
47
        }
48
49
        $db_conn = $this->getDatabase();
50
        $connection = DB::connection($db_conn);
51
52
        $database = $connection->getDatabaseName();
53
        $tables = $connection->select('SHOW TABLES');
54
55
        if (empty($tables)) {
56
            $this->info('Tables not found in database "' . $database . '"');
57
58
            return;
59
        }
60
61
        $pretend = $this->input->getOption('pretend');
62
        $connection->transaction(function () use ($connection, $tables, $database, $pretend) {
63
            if (! $pretend) {
64
                $connection->statement('SET FOREIGN_KEY_CHECKS=0;');
65
            }
66
67
            foreach ($tables as $table) {
68
                foreach ($table as $key => $value) {
69
                    $stm = 'DROP TABLE IF EXISTS `' . $value . '`';
70
                    if ($pretend) {
71
                        $this->line("{$stm}");
72
                    } else {
73
                        $connection->statement($stm);
74
                        $this->comment('Table `' . $value . '` dropped');
75
                    }
76
                }
77
            }
78
            if (! $pretend) {
79
                $connection->statement('SET FOREIGN_KEY_CHECKS=1;');
80
                $this->info('All tables dropped from database "' . $database . '"!');
81
            }
82
        });
83
    }
84
85
    protected function getDatabase(): string
86
    {
87
        $database = $this->input->getOption('database');
88
89
        return $database ?: $this->laravel['config']['database.default'];
90
    }
91
}
92