1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jumilla\Versionia\Laravel\Commands; |
4
|
|
|
|
5
|
|
|
use UnexpectedValueException; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Console\ConfirmableTrait; |
8
|
|
|
use Jumilla\Versionia\Laravel\Migrator; |
9
|
|
|
|
10
|
|
|
class DatabaseRollbackCommand extends Command |
11
|
|
|
{ |
12
|
|
|
use DatabaseCommandTrait; |
13
|
|
|
use ConfirmableTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'database:rollback |
21
|
|
|
{group : The name of the migration group.} |
22
|
|
|
{--all : Includes all version.} |
23
|
|
|
{--force : Force the operation to run when in production.} |
24
|
|
|
'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Database migrate to back to one or specifed version'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* @param \Jumilla\Versionia\Laravel\Migrator $migrator |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
7 |
|
public function handle(Migrator $migrator) |
41
|
|
|
{ |
42
|
7 |
|
if (!$this->confirmToProceed()) { |
43
|
1 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
$group = $this->argument('group'); |
47
|
|
|
|
48
|
|
|
// check valid group |
49
|
6 |
|
if (!in_array($group, $migrator->migrationGroups())) { |
50
|
2 |
|
throw new UnexpectedValueException("Migation group '$group' is not defined."); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
$version = $this->option('all') ? Migrator::VERSION_NULL : $migrator->migrationLatestVersion($group); |
|
|
|
|
54
|
|
|
|
55
|
4 |
|
$migrator->makeLogTable(); |
56
|
|
|
|
57
|
4 |
|
$this->doRollback($migrator, $group, $version); |
|
|
|
|
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Execute rollback. |
62
|
|
|
* |
63
|
|
|
* @param \Jumilla\Versionia\Laravel\Migrator $migrator |
64
|
|
|
* @param string $target_group |
65
|
|
|
* @param string $target_version |
66
|
|
|
*/ |
67
|
4 |
|
protected function doRollback(Migrator $migrator, $target_group, $target_version) |
68
|
|
|
{ |
69
|
4 |
|
$installed_migrations = $migrator->installedMigrationsByDesc(); |
70
|
|
|
|
71
|
4 |
|
if (!isset($installed_migrations[$target_group])) { |
72
|
1 |
|
$this->info("Nothing migrations for group '$target_group'."); |
73
|
|
|
|
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
foreach ($installed_migrations[$target_group] as $data) { |
78
|
|
|
// check version |
79
|
3 |
|
if ($migrator->compareMigrationVersion($data->version, $target_version) < 0) { |
80
|
1 |
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
$this->infoDowngrade($target_group, $data->version, $data->class); |
84
|
|
|
|
85
|
3 |
|
$migrator->doDowngrade($target_group, $data->version); |
86
|
|
|
} |
87
|
3 |
|
} |
88
|
|
|
} |
89
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.