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 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"); // Get all run migrations $stmt = $pdo->query("SELECT `migration` FROM `migrations`"); $run_migrations = $stmt->fetchAll(PDO::FETCH_COLUMN); // Get all migration files $migration_files = glob(__DIR__ . '/migrations/*.sql'); sort($migration_files); foreach ($migration_files as $file) { $migration_name = basename($file); if (!in_array($migration_name, $run_migrations)) { echo "Running migration: {$migration_name}...\n"; $sql = file_get_contents($file); $pdo->exec($sql); $stmt = $pdo->prepare("INSERT INTO `migrations` (`migration`) VALUES (?)"); $stmt->execute([$migration_name]); echo "Success: {$migration_name} migrated.\n"; } } echo "All migrations have been run.\n"; } catch (PDOException $e) { die("Migration failed: " . $e->getMessage()); }