Commit ae64a019 authored by Administrator's avatar Administrator

Update 2 files via Son of Anton

parent 5157eafb
...@@ -33,32 +33,37 @@ final class MigrationRunner ...@@ -33,32 +33,37 @@ final class MigrationRunner
echo " Migrating: {$name}...\n"; echo " Migrating: {$name}...\n";
$migration = require $file; try {
$migration = require $file;
if (is_array($migration) && isset($migration['up'])) {
// Array format: ['up' => 'SQL...', 'down' => 'SQL...'] if (is_array($migration) && isset($migration['up'])) {
$upSql = $migration['up']; $upSql = $migration['up'];
// Handle multiple statements separated by semicolons $statements = $this->splitStatements($upSql);
$statements = $this->splitStatements($upSql); foreach ($statements as $stmt) {
foreach ($statements as $stmt) { $stmt = trim($stmt);
$stmt = trim($stmt); if ($stmt !== '') {
if ($stmt !== '') { $this->db->raw($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; return $results;
...@@ -84,22 +89,22 @@ final class MigrationRunner ...@@ -84,22 +89,22 @@ final class MigrationRunner
if (file_exists($file)) { if (file_exists($file)) {
echo " Rolling back: {$name}...\n"; echo " Rolling back: {$name}...\n";
$migration = require $file; try {
$migration = require $file;
if (is_array($migration) && isset($migration['down'])) {
$statements = $this->splitStatements($migration['down']); if (is_array($migration) && isset($migration['down'])) {
foreach ($statements as $stmt) { $statements = $this->splitStatements($migration['down']);
$stmt = trim($stmt); foreach ($statements as $stmt) {
if ($stmt !== '') { $stmt = trim($stmt);
try { if ($stmt !== '') {
$this->db->raw($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')) { } catch (\Throwable $e) {
$migration->down($this->db); echo " ⚠️ Rollback warning: " . $e->getMessage() . "\n";
} }
} }
...@@ -133,7 +138,6 @@ final class MigrationRunner ...@@ -133,7 +138,6 @@ final class MigrationRunner
try { try {
$this->db->selectOne("SELECT 1 FROM migrations LIMIT 1"); $this->db->selectOne("SELECT 1 FROM migrations LIMIT 1");
} catch (\Throwable $e) { } catch (\Throwable $e) {
// Table doesn't exist — create it
$this->db->raw(" $this->db->raw("
CREATE TABLE IF NOT EXISTS `migrations` ( CREATE TABLE IF NOT EXISTS `migrations` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
...@@ -167,7 +171,7 @@ final class MigrationRunner ...@@ -167,7 +171,7 @@ final class MigrationRunner
if ($files === false || empty($files)) { if ($files === false || empty($files)) {
return []; return [];
} }
sort($files); // Lexicographic sort: Phase_01_001 < Phase_02_001 < Phase_03_001 sort($files);
return $files; return $files;
} }
...@@ -186,10 +190,6 @@ final class MigrationRunner ...@@ -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 private function splitStatements(string $sql): array
{ {
$sql = trim($sql); $sql = trim($sql);
...@@ -197,14 +197,11 @@ final class MigrationRunner ...@@ -197,14 +197,11 @@ final class MigrationRunner
return []; return [];
} }
// Simple split on semicolons followed by whitespace/newline
// This handles 99% of cases for DDL statements
$statements = preg_split('/;\s*\n/', $sql); $statements = preg_split('/;\s*\n/', $sql);
if ($statements === false) { if ($statements === false) {
return [$sql]; return [$sql];
} }
// Clean up: remove trailing semicolons from last statement
$result = []; $result = [];
foreach ($statements as $stmt) { foreach ($statements as $stmt) {
$stmt = trim($stmt); $stmt = trim($stmt);
......
...@@ -31,13 +31,7 @@ return [ ...@@ -31,13 +31,7 @@ return [
CONSTRAINT `fk_receipts_member` FOREIGN KEY (`member_id`) REFERENCES `members`(`id`), 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 `fk_receipts_payment` FOREIGN KEY (`payment_id`) REFERENCES `payments`(`id`) ON DELETE SET NULL,
CONSTRAINT `chk_receipts_amount` CHECK (`amount` >= 0) CONSTRAINT `chk_receipts_amount` CHECK (`amount` >= 0)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) 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`;
", ",
'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