exec("CREATE TABLE IF NOT EXISTS migrations ( id INT AUTO_INCREMENT PRIMARY KEY, migration VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"); $stmt = $db->query("SELECT migration FROM migrations"); $executed = $stmt->fetchAll(PDO::FETCH_COLUMN); $migrationsDir = dirname(__DIR__) . '/database/migrations'; $files = glob($migrationsDir . '/*.sql'); sort($files); // Ensure order $count = 0; foreach ($files as $file) { $name = basename($file); if (!in_array($name, $executed)) { echo "🚀 Running: $name... "; $sql = file_get_contents($file); // Execute the SQL. Since it might contain multiple statements, // and PDO::exec doesn't always handle them well in one go // depending on the driver, we'll try to run it. $db->exec($sql); $stmt = $db->prepare("INSERT INTO migrations (migration) VALUES (?)"); $stmt->execute([$name]); echo "✅ Done\n"; $count++; } } if ($count === 0) { echo "✨ Nothing to migrate. Database is up to date.\n"; } else { echo "🎉 Migrations completed successfully ($count ran).\n"; } } catch (Exception $e) { echo "❌ Error: " . $e->getMessage() . "\n"; exit(1); } echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";