Commit ae64a019 authored by Administrator's avatar Administrator

Update 2 files via Son of Anton

parent 5157eafb
......@@ -33,32 +33,37 @@ final class MigrationRunner
echo " Migrating: {$name}...\n";
$migration = require $file;
if (is_array($migration) && isset($migration['up'])) {
// Array format: ['up' => 'SQL...', 'down' => 'SQL...']
$upSql = $migration['up'];
// Handle multiple statements separated by semicolons
$statements = $this->splitStatements($upSql);
foreach ($statements as $stmt) {
$stmt = trim($stmt);
if ($stmt !== '') {
$this->db->raw($stmt);
try {
$migration = require $file;
if (is_array($migration) && isset($migration['up'])) {
$upSql = $migration['up'];
$statements = $this->splitStatements($upSql);
foreach ($statements as $stmt) {
$stmt = trim($stmt);
if ($stmt !== '') {
$this->db->raw($stmt);
}
}
} elseif (is_object($migration) && method_exists($migration, 'up')) {
$migration->up($this->db);
} elseif (is_callable($migration)) {
$migration($this->db);
}
} elseif (is_object($migration) && method_exists($migration, 'up')) {
$migration->up($this->db);
} elseif (is_callable($migration)) {
$migration($this->db);
}
$this->db->insert('migrations', [
'migration' => $name,
'batch' => $batch,
'executed_at' => date('Y-m-d H:i:s'),
]);
$results[] = $name;
$this->db->insert('migrations', [
'migration' => $name,
'batch' => $batch,
'executed_at' => date('Y-m-d H:i:s'),
]);
$results[] = $name;
echo " ✔ {$name}\n";
} catch (\Throwable $e) {
echo " ❌ {$name} FAILED: " . $e->getMessage() . "\n";
// Continue to next migration — don't let one failure kill the whole batch
continue;
}
}
return $results;
......@@ -84,22 +89,22 @@ final class MigrationRunner
if (file_exists($file)) {
echo " Rolling back: {$name}...\n";
$migration = require $file;
if (is_array($migration) && isset($migration['down'])) {
$statements = $this->splitStatements($migration['down']);
foreach ($statements as $stmt) {
$stmt = trim($stmt);
if ($stmt !== '') {
try {
try {
$migration = require $file;
if (is_array($migration) && isset($migration['down'])) {
$statements = $this->splitStatements($migration['down']);
foreach ($statements as $stmt) {
$stmt = trim($stmt);
if ($stmt !== '') {
$this->db->raw($stmt);
} catch (\Throwable $e) {
echo " ⚠️ Rollback warning for {$name}: " . $e->getMessage() . "\n";
}
}
} elseif (is_object($migration) && method_exists($migration, 'down')) {
$migration->down($this->db);
}
} elseif (is_object($migration) && method_exists($migration, 'down')) {
$migration->down($this->db);
} catch (\Throwable $e) {
echo " ⚠️ Rollback warning: " . $e->getMessage() . "\n";
}
}
......@@ -133,7 +138,6 @@ final class MigrationRunner
try {
$this->db->selectOne("SELECT 1 FROM migrations LIMIT 1");
} catch (\Throwable $e) {
// Table doesn't exist — create it
$this->db->raw("
CREATE TABLE IF NOT EXISTS `migrations` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
......@@ -167,7 +171,7 @@ final class MigrationRunner
if ($files === false || empty($files)) {
return [];
}
sort($files); // Lexicographic sort: Phase_01_001 < Phase_02_001 < Phase_03_001
sort($files);
return $files;
}
......@@ -186,10 +190,6 @@ final class MigrationRunner
}
}
/**
* Split SQL string into individual statements.
* Handles semicolons inside the SQL properly.
*/
private function splitStatements(string $sql): array
{
$sql = trim($sql);
......@@ -197,14 +197,11 @@ final class MigrationRunner
return [];
}
// Simple split on semicolons followed by whitespace/newline
// This handles 99% of cases for DDL statements
$statements = preg_split('/;\s*\n/', $sql);
if ($statements === false) {
return [$sql];
}
// Clean up: remove trailing semicolons from last statement
$result = [];
foreach ($statements as $stmt) {
$stmt = trim($stmt);
......
......@@ -31,13 +31,7 @@ return [
CONSTRAINT `fk_receipts_member` FOREIGN KEY (`member_id`) REFERENCES `members`(`id`),
CONSTRAINT `fk_receipts_payment` FOREIGN KEY (`payment_id`) REFERENCES `payments`(`id`) ON DELETE SET NULL,
CONSTRAINT `chk_receipts_amount` CHECK (`amount` >= 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `payments`
ADD CONSTRAINT `fk_payments_receipt` FOREIGN KEY (`receipt_id`) REFERENCES `receipts`(`id`) ON DELETE SET NULL;
",
'down' => "
ALTER TABLE `payments` DROP FOREIGN KEY IF EXISTS `fk_payments_receipt`;
DROP TABLE IF EXISTS `receipts`;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
",
'down' => "DROP TABLE IF EXISTS `receipts`",
];
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment