Completed
Pull Request — master (#49)
by
unknown
01:12
created

MigrateFresh::handle()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.5806
cc 4
eloc 20
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
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $database = $this->input->getOption('database');
43
44
        $path = $this->input->getOption('path');
45
46
        if (! $this->confirmToProceed()) {
47
            return;
48
        }
49
50
        $this->info('Dropping all tables...');
51
52
        event(new DroppingTables());
53
        if($database !== null) {
54
            DB::setDefaultConnection($database);
55
        }
56
        $this->getTableDropper()->dropAllTables();
57
        event(new DroppedTables());
58
59
        $this->info('Running migrations...');
60
        $this->call('migrate', [
61
            '--database' => $database,
62
            '--path' => $path,
63
            '--force' => true
64
        ]);
65
66
        if ($this->option('seed')) {
67
            $this->info('Running seeders...');
68
            $this->call('db:seed', ['--force' => true]);
69
        }
70
71
        $this->comment('All done!');
72
    }
73
74
    public function getTableDropper(): TableDropper
75
    {
76
        return TableDropperFactory::create(DB::getDriverName());
77
    }
78
}
79