setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Create migrations table if it doesn't exist $pdo->exec("CREATE TABLE IF NOT EXISTS migrations ( id INT AUTO_INCREMENT PRIMARY KEY, migration VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); // Get all executed migrations $executedMigrations = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN); $migrationFiles = glob(__DIR__ . '/migrations/*.sql'); sort($migrationFiles); foreach ($migrationFiles as $file) { $migrationName = basename($file); if (!in_array($migrationName, $executedMigrations)) { $sql = file_get_contents($file); $pdo->exec($sql); // Record the migration $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); $stmt->execute([$migrationName]); echo "Migration successful: {$migrationName}" . PHP_EOL; } } echo "All migrations are up to date." . PHP_EOL; } catch (PDOException $e) { die("Migration failed: " . $e->getMessage()); }