Skip to content

Commit bca3f37

Browse files
Правки в миграции
1 parent ff7c8d6 commit bca3f37

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

Migration/MigrateCommand.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,52 @@ private function up()
4343
$this->q(
4444
"CREATE TABLE `migrate` (
4545
`id` INT NOT NULL AUTO_INCREMENT ,
46-
`name` VARCHAR(500) NULL DEFAULT NULL ,
47-
`hash` VARCHAR(500) NULL DEFAULT NULL ,
46+
`name` VARCHAR(500) NULL DEFAULT NULL ,
47+
`hash` VARCHAR(500) NULL DEFAULT NULL ,
4848
`cdate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
4949
`sql_str` TEXT NULL DEFAULT NULL,
50-
PRIMARY KEY (`id`)
50+
PRIMARY KEY (`id`)
5151
) ENGINE = InnoDB;"
5252
);
53-
}
53+
}
5454

5555
$cq = 0;
56-
dirEach($this->DIR, callFile: function ($name) use (&$cq){
56+
$files = scandir($this->DIR);
57+
$files = array_values(array_filter($files, fn($f) => $f !== '.' && $f !== '..'));
58+
sort($files);
59+
60+
foreach ($files as $name) {
5761
$query = file_get_contents($this->DIR . "/$name");
5862
if (empty($query)) {
5963
Console::text("Пустой файл миграции", 'red');
64+
continue;
6065
}
6166
$hash = md5($query);
62-
if (empty($this->find(['hash' => "$hash"]))) {
63-
$this->insert(['name' => "$name", 'hash' => "$hash"]);
64-
foreach (explode(";", $query) as $q) {
65-
try {
66-
if (empty(trim($q))) {
67-
continue;
68-
}
69-
$Query = $this->q($q);
70-
if ($Query) {
71-
$Query->fetch();
72-
$this->set(['sql_str'=> $q]);
73-
Console::text(($cq + 1) . ") Выполнен: " . str_replace("\n", "", iconv_substr(trim($q), 0, 50, 'UTF-8')), Console::GREEN);
74-
}
75-
} catch (Error | Exception $e) {
76-
Console::text("Error: " . $e->getMessage(), Console::RED);
67+
if (!empty($this->find(['hash' => "$hash"]))) {
68+
continue;
69+
}
70+
71+
$migrateId = $this->create(['name' => "$name", 'hash' => "$hash"]);
72+
if ($migrateId === false) {
73+
Console::text("Ошибка вставки записи миграции", Console::RED);
74+
continue;
75+
}
76+
77+
foreach (explode(";", $query) as $q) {
78+
try {
79+
if (empty(trim($q))) {
80+
continue;
7781
}
78-
$cq++;
82+
$this->q($q);
83+
$this->set(['sql_str' => $q]);
84+
Console::text(($cq + 1) . ") Выполнен: " . str_replace("\n", "", iconv_substr(trim($q), 0, 50, 'UTF-8')), Console::GREEN);
85+
} catch (Error | Exception $e) {
86+
Console::text("Error: " . $e->getMessage(), Console::RED);
7987
}
88+
$cq++;
8089
}
81-
});
90+
}
91+
8292
Console::text("=====================", Console::YELLOW);
8393
if ($cq == 0) {
8494
Console::text("Новых миграций нет", Console::YELLOW);

0 commit comments

Comments
 (0)