MigrateFresh::handle()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 4
nc 5
nop 0
1
<?php
2
3
namespace Spatie\MigrateFresh\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Console\ConfirmableTrait;
8
use Spatie\MigrateFresh\TableDropperFactory;
9
use Spatie\MigrateFresh\Events\DroppedTables;
10
use Spatie\MigrateFresh\Events\DroppingTables;
11
use Spatie\MigrateFresh\TableDroppers\TableDropper;
12
13
class MigrateFresh extends Command
14
{
15
    use ConfirmableTrait;
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'migrate:fresh {--database= : The database connection to use.}
23
                {--force : Force the operation to run when in production.}
24
                {--path= : The path of migrations files to be executed.}
25
                {--seed : Indicates if the seed task should be re-run.}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Drop all database tables and re-run all migrations';
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39
    public function handle()
40
    {
41
        $database = $this->input->getOption('database');
42
43
        $path = $this->input->getOption('path');
44
45
        if (! $this->confirmToProceed()) {
46
            return;
47
        }
48
49
        $this->info('Dropping all tables...');
50
51
        event(new DroppingTables());
52
        if ($database !== null) {
53
            DB::setDefaultConnection($database);
54
        }
55
        $this->getTableDropper()->dropAllTables();
56
        event(new DroppedTables());
57
58
        $this->info('Running migrations...');
59
        $this->call('migrate', [
60
            '--database' => $database,
61
            '--path' => $path,
62
            '--force' => true,
63
        ]);
64
65
        if ($this->option('seed')) {
66
            $this->info('Running seeders...');
67
            $this->call('db:seed', ['--force' => true]);
68
        }
69
70
        $this->comment('All done!');
71
    }
72
73
    public function getTableDropper(): TableDropper
74
    {
75
        return TableDropperFactory::create(DB::getDriverName());
76
    }
77
}
78