1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Framework |
5
|
|
|
* |
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
7
|
|
|
* PHP Web framework |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Framework |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file MigrationExecuteCommand.php |
34
|
|
|
* |
35
|
|
|
* The migration execute command class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Framework\Migration\Command |
38
|
|
|
* @author Platine Developers team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Framework\Migration\Command; |
49
|
|
|
|
50
|
|
|
use Platine\Config\Config; |
51
|
|
|
use Platine\Filesystem\Filesystem; |
52
|
|
|
use Platine\Framework\App\Application; |
53
|
|
|
use Platine\Framework\Migration\MigrationRepository; |
54
|
|
|
use RuntimeException; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @class MigrationExecuteCommand |
58
|
|
|
* @package Platine\Framework\Migration\Command |
59
|
|
|
* @template T |
60
|
|
|
* @extends AbstractCommand<T> |
61
|
|
|
*/ |
62
|
|
|
class MigrationExecuteCommand extends AbstractCommand |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* Create new instance |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
Application $app, |
70
|
|
|
MigrationRepository $repository, |
71
|
|
|
Config $config, |
72
|
|
|
Filesystem $filesystem |
73
|
|
|
) { |
74
|
|
|
parent::__construct($app, $repository, $config, $filesystem); |
75
|
|
|
$this->setName('migration:exec') |
76
|
|
|
->setDescription('Execute the migration up/down for one version'); |
77
|
|
|
|
78
|
|
|
$this->addArgument('type', 'type of migration [up|down]', 'up', true, false, function ($val) { |
79
|
|
|
if (!in_array($val, ['up', 'down'])) { |
80
|
|
|
throw new RuntimeException(sprintf( |
81
|
|
|
'Invalid argument type [%s], must be one of [up, down]', |
82
|
|
|
$val |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $val; |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
$this->addOption('-i|--id', 'the migration version', null, false); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function execute(): mixed |
96
|
|
|
{ |
97
|
|
|
$type = $this->getArgumentValue('type'); |
98
|
|
|
|
99
|
|
|
$io = $this->io(); |
100
|
|
|
$writer = $io->writer(); |
101
|
|
|
$writer->boldYellow('MIGRATION EXECUTION', true)->eol(); |
102
|
|
|
|
103
|
|
|
$migrations = $this->getMigrations(); |
104
|
|
|
$executed = $this->getExecuted('DESC'); |
105
|
|
|
$version = $this->getOptionValue('id'); |
106
|
|
|
|
107
|
|
|
if ($type === 'up') { |
108
|
|
|
$diff = array_diff_key($migrations, $executed); |
109
|
|
|
if (count($diff) === 0) { |
110
|
|
|
$writer->boldGreen('Migration already up to date'); |
111
|
|
|
} else { |
112
|
|
|
if (empty($version)) { |
113
|
|
|
$version = $io->choice('Choose which version to migrate up', $diff); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!isset($diff[$version])) { |
117
|
|
|
$writer->boldRed(sprintf( |
118
|
|
|
'Invalid migration version [%s] or already executed', |
119
|
|
|
$version |
120
|
|
|
)); |
121
|
|
|
} else { |
122
|
|
|
$description = str_replace('_', ' ', $migrations[$version]); |
123
|
|
|
$this->executeMigrationUp($version, $description); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
if (count($executed) === 0) { |
128
|
|
|
$writer->boldGreen('No migration to rollback'); |
129
|
|
|
} else { |
130
|
|
|
$data = []; |
131
|
|
|
foreach ($executed as $ver => $entity) { |
132
|
|
|
$data[$ver] = $entity->description; |
133
|
|
|
} |
134
|
|
|
if (empty($version)) { |
135
|
|
|
$version = $io->choice('Choose which version to rollback', $data); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!isset($data[$version])) { |
139
|
|
|
$writer->boldRed(sprintf( |
140
|
|
|
'Invalid migration version [%s] or not yet executed', |
141
|
|
|
$version |
142
|
|
|
)); |
143
|
|
|
} else { |
144
|
|
|
$description = str_replace('_', ' ', $data[$version]); |
145
|
|
|
$this->executeMigrationDown($version, $description); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Execute migration up |
155
|
|
|
* @param string $version |
156
|
|
|
* @param string $description |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function executeMigrationUp(string $version, string $description): void |
160
|
|
|
{ |
161
|
|
|
$writer = $this->io()->writer(); |
162
|
|
|
$writer->boldGreen(sprintf( |
163
|
|
|
'* Execute migration up for %s: %s', |
164
|
|
|
$version, |
165
|
|
|
$description, |
166
|
|
|
))->eol(); |
167
|
|
|
|
168
|
|
|
$migration = $this->createMigrationClass($description, $version); |
169
|
|
|
$migration->up(); |
170
|
|
|
|
171
|
|
|
$entity = $this->repository->create([ |
172
|
|
|
'version' => $version, |
173
|
|
|
'description' => $description, |
174
|
|
|
'created_at' => date('Y-m-d H:i:s'), |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
$this->repository->save($entity); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Execute migration down |
182
|
|
|
* @param string $version |
183
|
|
|
* @param string $description |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function executeMigrationDown(string $version, string $description): void |
187
|
|
|
{ |
188
|
|
|
$writer = $this->io()->writer(); |
189
|
|
|
$writer->boldGreen(sprintf( |
190
|
|
|
'* Execute migration down for %s: %s', |
191
|
|
|
$version, |
192
|
|
|
$description, |
193
|
|
|
))->eol(); |
194
|
|
|
|
195
|
|
|
$migration = $this->createMigrationClass($description, $version); |
196
|
|
|
$migration->down(); |
197
|
|
|
|
198
|
|
|
$entity = $this->repository->findBy([ |
199
|
|
|
'version' => $version |
200
|
|
|
]); |
201
|
|
|
|
202
|
|
|
if ($entity) { |
203
|
|
|
$this->repository->delete($entity); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|