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 $stmt = $pdo->query('SELECT migration FROM migrations'); $executedMigrations = $stmt ? $stmt->fetchAll(PDO::FETCH_COLUMN) : []; // Find all migration files $migrationFiles = glob(__DIR__ . '/migrations/*.sql') ?: []; sort($migrationFiles); $migrationsRun = false; // Run pending migrations foreach ($migrationFiles as $migrationFile) { $migrationName = basename($migrationFile); if (!in_array($migrationName, $executedMigrations)) { $sql = file_get_contents($migrationFile); if (!empty(trim($sql))) { $pdo->exec($sql); // Log the migration $stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)'); $stmt->execute([$migrationName]); echo "Migration from $migrationName ran successfully.\n"; $migrationsRun = true; } } } if (!$migrationsRun) { echo "All migrations are up to date.\n"; } } catch (PDOException $e) { die("DB ERROR: " . $e->getMessage()); }