| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Console\Migrations;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\Command;
|
|
|
6 |
use Illuminate\Console\ConfirmableTrait;
|
|
|
7 |
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
8 |
use Illuminate\Database\Events\DatabaseRefreshed;
|
|
|
9 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
10 |
|
|
|
11 |
class FreshCommand extends Command
|
|
|
12 |
{
|
|
|
13 |
use ConfirmableTrait;
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* The console command name.
|
|
|
17 |
*
|
|
|
18 |
* @var string
|
|
|
19 |
*/
|
|
|
20 |
protected $name = 'migrate:fresh';
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* The console command description.
|
|
|
24 |
*
|
|
|
25 |
* @var string
|
|
|
26 |
*/
|
|
|
27 |
protected $description = 'Drop all tables and re-run all migrations';
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Execute the console command.
|
|
|
31 |
*
|
|
|
32 |
* @return int
|
|
|
33 |
*/
|
|
|
34 |
public function handle()
|
|
|
35 |
{
|
|
|
36 |
if (! $this->confirmToProceed()) {
|
|
|
37 |
return 1;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$database = $this->input->getOption('database');
|
|
|
41 |
|
|
|
42 |
$this->newLine();
|
|
|
43 |
|
|
|
44 |
$this->components->task('Dropping all tables', fn () => $this->callSilent('db:wipe', array_filter([
|
|
|
45 |
'--database' => $database,
|
|
|
46 |
'--drop-views' => $this->option('drop-views'),
|
|
|
47 |
'--drop-types' => $this->option('drop-types'),
|
|
|
48 |
'--force' => true,
|
|
|
49 |
])) == 0);
|
|
|
50 |
|
|
|
51 |
$this->newLine();
|
|
|
52 |
|
|
|
53 |
$this->call('migrate', array_filter([
|
|
|
54 |
'--database' => $database,
|
|
|
55 |
'--path' => $this->input->getOption('path'),
|
|
|
56 |
'--realpath' => $this->input->getOption('realpath'),
|
|
|
57 |
'--schema-path' => $this->input->getOption('schema-path'),
|
|
|
58 |
'--force' => true,
|
|
|
59 |
'--step' => $this->option('step'),
|
|
|
60 |
]));
|
|
|
61 |
|
|
|
62 |
if ($this->laravel->bound(Dispatcher::class)) {
|
|
|
63 |
$this->laravel[Dispatcher::class]->dispatch(
|
|
|
64 |
new DatabaseRefreshed
|
|
|
65 |
);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if ($this->needsSeeding()) {
|
|
|
69 |
$this->runSeeder($database);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return 0;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Determine if the developer has requested database seeding.
|
|
|
77 |
*
|
|
|
78 |
* @return bool
|
|
|
79 |
*/
|
|
|
80 |
protected function needsSeeding()
|
|
|
81 |
{
|
|
|
82 |
return $this->option('seed') || $this->option('seeder');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Run the database seeder command.
|
|
|
87 |
*
|
|
|
88 |
* @param string $database
|
|
|
89 |
* @return void
|
|
|
90 |
*/
|
|
|
91 |
protected function runSeeder($database)
|
|
|
92 |
{
|
|
|
93 |
$this->call('db:seed', array_filter([
|
|
|
94 |
'--database' => $database,
|
|
|
95 |
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
|
|
|
96 |
'--force' => true,
|
|
|
97 |
]));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Get the console command options.
|
|
|
102 |
*
|
|
|
103 |
* @return array
|
|
|
104 |
*/
|
|
|
105 |
protected function getOptions()
|
|
|
106 |
{
|
|
|
107 |
return [
|
|
|
108 |
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'],
|
|
|
109 |
['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],
|
|
|
110 |
['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],
|
|
|
111 |
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
|
|
|
112 |
['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'],
|
|
|
113 |
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
|
|
|
114 |
['schema-path', null, InputOption::VALUE_OPTIONAL, 'The path to a schema dump file'],
|
|
|
115 |
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run'],
|
|
|
116 |
['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'],
|
|
|
117 |
['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually'],
|
|
|
118 |
];
|
|
|
119 |
}
|
|
|
120 |
}
|