exec($migrationTableSql); echo "Migrations table ensured.\n"; // 2. Get executed migrations $stmt = $pdo->query("SELECT migration FROM migrations"); $executedMigrations = $stmt->fetchAll(PDO::FETCH_COLUMN); // 3. Find and execute new migrations $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); $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); $stmt->execute([$migrationName]); echo "Executed migration: $migrationName\n"; } } echo "All migrations have been run.\n"; } catch (PDOException $e) { die("Database migration failed: " . $e->getMessage() . "\n"); }