PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); } return $pdo; } function run_migrations() { $pdo = db(); $migrations_dir = __DIR__ . '/migrations'; if (!is_dir($migrations_dir)) { return; } $migration_files = glob($migrations_dir . '/*.sql'); foreach ($migration_files as $file) { $p = pathinfo($file); $migration_name = $p['basename']; try { $stmt = $pdo->query("SELECT 1 FROM migrations WHERE name = '$migration_name'"); if ($stmt->fetchColumn()) { continue; } } catch (PDOException $e) { // migrations table doesn't exist, create it $pdo->exec("CREATE TABLE IF NOT EXISTS migrations (name VARCHAR(255) PRIMARY KEY, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); } $sql = file_get_contents($file); $pdo->exec($sql); $stmt = $pdo->prepare("INSERT INTO migrations (name) VALUES (?)"); $stmt->execute([$migration_name]); } } run_migrations();