|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Suricate\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Suricate\Console; |
|
8
|
|
|
use Suricate\Suricate; |
|
9
|
|
|
|
|
10
|
|
|
class Migration |
|
11
|
|
|
{ |
|
12
|
|
|
protected $app; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Suricate $app) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->app = $app; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function execute(array $arguments): int |
|
20
|
|
|
{ |
|
21
|
|
|
$command = $arguments[0] ?? ''; |
|
22
|
|
|
switch ($command) { |
|
23
|
|
|
case 'migrate': |
|
24
|
|
|
return $this->commandMigrate(); |
|
25
|
|
|
case 'init': |
|
26
|
|
|
return $this->commandInit(); |
|
27
|
|
|
case 'list': |
|
28
|
|
|
return $this->commandList(); |
|
29
|
|
|
case 'create': |
|
30
|
|
|
return $this->commandCreate(); |
|
31
|
|
|
default: |
|
32
|
|
|
return $this->commandHelp(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function commandMigrate() |
|
37
|
|
|
{ |
|
38
|
|
|
Suricate::Migration()->doMigrations(); |
|
39
|
|
|
return 0; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function commandInit(): int |
|
43
|
|
|
{ |
|
44
|
|
|
$result = Suricate::Migration()->initMigrationTable(); |
|
45
|
|
|
if ($result) { |
|
46
|
|
|
echo 'Migration table created successfully' . "\n"; |
|
47
|
|
|
return 0; |
|
48
|
|
|
} |
|
49
|
|
|
echo 'Failed to create migration table' ."\n"; |
|
50
|
|
|
|
|
51
|
|
|
return 1; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function commandList(): int |
|
55
|
|
|
{ |
|
56
|
|
|
$migrations = Suricate::Migration()->listMigrations(); |
|
57
|
|
|
if (count($migrations) === 0) { |
|
58
|
|
|
echo "No migration\n"; |
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
echo str_repeat("-", 76) . "\n"; |
|
63
|
|
|
foreach ($migrations as $migrationKey=>$migrationDate) { |
|
64
|
|
|
echo "| " . str_pad(trim($migrationKey), 50, ' ', STR_PAD_RIGHT) . " | " . ($migrationDate !== false ? $migrationDate : str_pad('-', 19, ' ', STR_PAD_BOTH)). " |\n"; |
|
65
|
|
|
} |
|
66
|
|
|
echo str_repeat("-", 76) . "\n"; |
|
67
|
|
|
|
|
68
|
|
|
return 0; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function commandCreate(): int |
|
72
|
|
|
{ |
|
73
|
|
|
$migrationName = Suricate::Migration()->createMigration(); |
|
74
|
|
|
if ($migrationName === false) { |
|
75
|
|
|
echo "Failed to create migration file\n"; |
|
76
|
|
|
return 1; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
echo "Migration $migrationName created successfully\n"; |
|
80
|
|
|
|
|
81
|
|
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
private function commandHelp(): int |
|
84
|
|
|
{ |
|
85
|
|
|
$str = "Help:" . "\n"; |
|
86
|
|
|
$str .= Console::coloredString('init:', 'green') . "\n"; |
|
87
|
|
|
$str .= "\tInitialize the migrations table\n"; |
|
88
|
|
|
$str .= Console::coloredString('list:', 'green') . "\n"; |
|
89
|
|
|
$str .= "\tList migrations\n"; |
|
90
|
|
|
$str .= Console::coloredString('migrate:', 'green') . "\n"; |
|
91
|
|
|
$str .= "\tExecute pending migrations\n"; |
|
92
|
|
|
|
|
93
|
|
|
echo $str; |
|
94
|
|
|
return 0; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|