Completed
Push — master ( 357fac...f3abe9 )
by Freek
01:53
created

MigrateFresh::dropAllTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Spatie\MigrateFresh\Commands;
4
5
use DB;
6
use Schema;
7
use stdClass;
8
use Illuminate\Console\Command;
9
use Illuminate\Console\ConfirmableTrait;
10
11
class MigrateFresh extends Command
12
{
13
    use ConfirmableTrait;
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'migrate:fresh {--seed} {--force}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Nuke the entire db and rebuild it using migrations.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return mixed
33
     */
34
    public function handle()
35
    {
36
        if (! $this->confirmToProceed()) {
37
            return;
38
        }
39
40
        $this->info('Dropping all tables...');
41
        $this->dropAllTables();
42
43
        $this->info('Running migrations...');
44
        $this->call('migrate', ['--force' => true]);
45
46
        if ($this->hasOption('seed')) {
47
            $this->info('Running seeders...');
48
            $this->call('db:seed', ['--force' => true]);
49
        }
50
51
        $this->comment('All done!');
52
    }
53
54
    public function dropAllTables()
55
    {
56
        Schema::disableForeignKeyConstraints();
57
58
        collect(DB::select('SHOW TABLES'))
59
            ->map(function (stdClass $tableProperties) {
60
                return get_object_vars($tableProperties)[key($tableProperties)];
61
            })
62
            ->each(function (string $tableName) {
63
                Schema::drop($tableName);
64
            });
65
66
        Schema::enableForeignKeyConstraints();
67
    }
68
}
69