1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vados\MigrationRunner\command; |
4
|
|
|
|
5
|
|
|
use Vados\MigrationRunner\Helpers; |
6
|
|
|
use Vados\MigrationRunner\migration\Migration; |
7
|
|
|
use Vados\MigrationRunner\models\TblMigration; |
8
|
|
|
use Vados\MigrationRunner\providers\PathProvider; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Up |
12
|
|
|
* @package Vados\MigrationRunner\command |
13
|
|
|
*/ |
14
|
|
|
class Up extends MigrationRun implements ICommand |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $params; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $runCount = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Up constructor. |
28
|
|
|
* @param array $params |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $params) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
$this->params = $params; |
34
|
|
|
if (isset($params[0])) { |
35
|
|
|
$this->runCount = (int)$params[0]; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function run() |
40
|
|
|
{ |
41
|
|
|
$migrations = $this->getMigrationsList(); |
42
|
|
|
if (!empty($migrations)) { |
43
|
|
|
echo implode(PHP_EOL, $migrations); |
44
|
|
|
if ($this->actionConfirmation('Apply the above migrations?')) { |
45
|
|
|
foreach ($migrations as $migration) { |
46
|
|
|
echo "Migration $migration: "; |
47
|
|
|
$result = $this->up($migration); |
48
|
|
|
echo Helpers::boolToString($result) . PHP_EOL; |
49
|
|
|
if (!$result) { |
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
protected function getMigrationsList(): array |
61
|
|
|
{ |
62
|
|
|
$migrations = $this->getNewMigrations(); |
63
|
|
|
if (0 !== $this->runCount) { |
64
|
|
|
$migrations = array_slice($migrations, 0, $this->runCount); |
65
|
|
|
} |
66
|
|
|
return $migrations; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $migration |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
private function up(string $migration): bool |
74
|
|
|
{ |
75
|
|
|
if (file_exists(PathProvider::getMigrationDir() . DIRECTORY_SEPARATOR . $migration)) { |
76
|
|
|
require_once PathProvider::getMigrationDir() . DIRECTORY_SEPARATOR . $migration; |
77
|
|
|
$migrationClass = substr($migration, 0, -4); |
78
|
|
|
/** @var Migration $m */ |
79
|
|
|
$m = new $migrationClass(); |
80
|
|
|
if ($m->safeRun('up')) { |
81
|
|
|
$model = new TblMigration(); |
82
|
|
|
$model->setMigration($migration); |
83
|
|
|
$model->save(); |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |