|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Ship\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Rudra\Cli\ConsoleFacade as Cli; |
|
6
|
|
|
use Rudra\Container\Facades\Rudra; |
|
7
|
|
|
|
|
8
|
|
|
class CreateMigrationCommand |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Creates a file with Migration data |
|
12
|
|
|
* ----------------------------- |
|
13
|
|
|
* Создает файл с данными Migration |
|
14
|
|
|
*/ |
|
15
|
|
|
public function actionIndex() |
|
16
|
|
|
{ |
|
17
|
|
|
Cli::printer("Enter table name: ", "magneta"); |
|
18
|
|
|
$table = str_replace("\n", "", Cli::reader()); |
|
19
|
|
|
|
|
20
|
|
|
Cli::printer("Enter container (empty for Ship): ", "magneta"); |
|
21
|
|
|
$container = ucfirst(str_replace("\n", "", Cli::reader())); |
|
22
|
|
|
|
|
23
|
|
|
$date = date("_dmYHis"); |
|
24
|
|
|
$className = ucfirst($table) . $date; |
|
25
|
|
|
|
|
26
|
|
|
if (!empty($container)) { |
|
27
|
|
|
|
|
28
|
|
|
$namespace = 'App\Containers\\' . $container . '\Migrations'; |
|
29
|
|
|
|
|
30
|
|
|
if (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "mysql") { |
|
31
|
|
|
$this->writeFile([Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migrations/", "{$className}_migration.php"], |
|
32
|
|
|
$this->createMysqlMigration($className, $table, $namespace) |
|
33
|
|
|
); |
|
34
|
|
|
} elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "pgsql") { |
|
35
|
|
|
$this->writeFile([Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migrations/", "{$className}_migration.php"], |
|
36
|
|
|
$this->createPgsqlMigration($className, $table, $namespace) |
|
37
|
|
|
); |
|
38
|
|
|
} elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "sqlite") { |
|
39
|
|
|
$this->writeFile([Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migrations/", "{$className}_migration.php"], |
|
40
|
|
|
$this->createSqliteMigration($className, $table, $namespace) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
} else { |
|
45
|
|
|
|
|
46
|
|
|
$namespace = "App\Ship\Migrations"; |
|
47
|
|
|
|
|
48
|
|
|
if (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "mysql") { |
|
49
|
|
|
$this->writeFile([Rudra::config()->get('app.path') . "/app/Ship/Migrations/", "{$className}_migration.php"], |
|
50
|
|
|
$this->createMysqlMigration($className, $table, $namespace) |
|
51
|
|
|
); |
|
52
|
|
|
} elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "pgsql") { |
|
53
|
|
|
$this->writeFile([Rudra::config()->get('app.path') . "/app/Ship/Migrations/", "{$className}_migration.php"], |
|
54
|
|
|
$this->createPgsqlMigration($className, $table, $namespace) |
|
55
|
|
|
); |
|
56
|
|
|
} elseif (Rudra::get("DSN")->getAttribute(\PDO::ATTR_DRIVER_NAME) === "sqlite") { |
|
57
|
|
|
$this->writeFile([Rudra::config()->get('app.path') . "/app/Ship/Migrations/", "{$className}_migration.php"], |
|
58
|
|
|
$this->createSqliteMigration($className, $table, $namespace) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $className |
|
66
|
|
|
* @param string $container |
|
67
|
|
|
* @param string $table |
|
68
|
|
|
* @return string |
|
69
|
|
|
* |
|
70
|
|
|
* Creates class data |
|
71
|
|
|
* ------------------ |
|
72
|
|
|
* Создает данные класса |
|
73
|
|
|
*/ |
|
74
|
|
|
private function createMysqlMigration(string $className, string $table, string $namespace) |
|
75
|
|
|
{ |
|
76
|
|
|
return <<<EOT |
|
77
|
|
|
<?php |
|
78
|
|
|
|
|
79
|
|
|
namespace $namespace; |
|
80
|
|
|
|
|
81
|
|
|
use Rudra\Container\Facades\Rudra; |
|
82
|
|
|
|
|
83
|
|
|
class {$className}_migration |
|
84
|
|
|
{ |
|
85
|
|
|
public function up() |
|
86
|
|
|
{ |
|
87
|
|
|
\$table = "$table"; |
|
88
|
|
|
|
|
89
|
|
|
\$query = Rudra::get("DSN")->prepare(" |
|
90
|
|
|
CREATE TABLE {\$table} ( |
|
91
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
|
92
|
|
|
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , |
|
93
|
|
|
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
|
94
|
|
|
, PRIMARY KEY (`id`)) ENGINE = InnoDB |
|
95
|
|
|
"); |
|
96
|
|
|
|
|
97
|
|
|
\$query->execute(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
EOT; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $className |
|
105
|
|
|
* @param string $container |
|
106
|
|
|
* @param string $table |
|
107
|
|
|
* @return string |
|
108
|
|
|
* |
|
109
|
|
|
* Creates class data |
|
110
|
|
|
* ------------------ |
|
111
|
|
|
* Создает данные класса |
|
112
|
|
|
*/ |
|
113
|
|
|
private function createPgsqlMigration(string $className, string $table, string $namespace) |
|
114
|
|
|
{ |
|
115
|
|
|
return <<<EOT |
|
116
|
|
|
<?php |
|
117
|
|
|
|
|
118
|
|
|
namespace $namespace; |
|
119
|
|
|
|
|
120
|
|
|
use Rudra\Container\Facades\Rudra; |
|
121
|
|
|
|
|
122
|
|
|
class {$className}_migration |
|
123
|
|
|
{ |
|
124
|
|
|
public function up() |
|
125
|
|
|
{ |
|
126
|
|
|
\$table = "$table"; |
|
127
|
|
|
|
|
128
|
|
|
\$query = Rudra::get("DSN")->prepare(" |
|
129
|
|
|
|
|
130
|
|
|
CREATE TABLE {\$table} ( |
|
131
|
|
|
id serial PRIMARY KEY, |
|
132
|
|
|
created_at TIMESTAMP NOT NULL, |
|
133
|
|
|
updated_at TIMESTAMP NOT NULL |
|
134
|
|
|
); |
|
135
|
|
|
"); |
|
136
|
|
|
|
|
137
|
|
|
\$query->execute(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
EOT; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $className |
|
145
|
|
|
* @param string $container |
|
146
|
|
|
* @param string $table |
|
147
|
|
|
* @return string |
|
148
|
|
|
* |
|
149
|
|
|
* Creates class data |
|
150
|
|
|
* ------------------ |
|
151
|
|
|
* Создает данные класса |
|
152
|
|
|
*/ |
|
153
|
|
|
private function createSqliteMigration(string $className, string $table, string $namespace) |
|
154
|
|
|
{ |
|
155
|
|
|
return <<<EOT |
|
156
|
|
|
<?php |
|
157
|
|
|
|
|
158
|
|
|
namespace $namespace; |
|
159
|
|
|
|
|
160
|
|
|
use Rudra\Container\Facades\Rudra; |
|
161
|
|
|
|
|
162
|
|
|
class {$className}_migration |
|
163
|
|
|
{ |
|
164
|
|
|
public function up() |
|
165
|
|
|
{ |
|
166
|
|
|
\$table = "$table"; |
|
167
|
|
|
|
|
168
|
|
|
\$query = Rudra::get("DSN")->prepare(" |
|
169
|
|
|
|
|
170
|
|
|
CREATE TABLE IF NOT EXISTS {\$table} ( |
|
171
|
|
|
id INTEGER PRIMARY KEY, |
|
172
|
|
|
created_at TEXT NOT NULL, |
|
173
|
|
|
updated_at TEXT NOT NULL |
|
174
|
|
|
); |
|
175
|
|
|
"); |
|
176
|
|
|
|
|
177
|
|
|
\$query->execute(); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
EOT; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param $path |
|
185
|
|
|
* @param $callable |
|
186
|
|
|
* |
|
187
|
|
|
* Writes data to a file |
|
188
|
|
|
* --------------------- |
|
189
|
|
|
* Записывает данные в файл |
|
190
|
|
|
*/ |
|
191
|
|
|
private function writeFile($path, $callable) |
|
192
|
|
|
{ |
|
193
|
|
|
if (!is_dir($path[0])) { |
|
194
|
|
|
mkdir($path[0], 0755, true); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$fullPath = $path[0] . $path[1]; |
|
198
|
|
|
|
|
199
|
|
|
if (!file_exists($fullPath)) { |
|
200
|
|
|
Cli::printer("The file $fullPath was created\n", "light_green"); |
|
201
|
|
|
file_put_contents($fullPath, $callable); |
|
202
|
|
|
} else { |
|
203
|
|
|
Cli::printer("The file $fullPath is already exists\n", "light_yellow"); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|