Upgrading Casbin (Go) from v2 to v3: A Complete Migration Guide
If you're running Casbin v2 in production and wondering about the upgrade path to v3, this guide is for you. We'll cover everything you need to know about the transition, from understanding what changed to executing a safe production migration.
What Changed in v3?
The good news: v3 is primarily a module path change with full backward compatibility. The core authorization logic, APIs, and functionality remain unchanged. This was an intentional decision to minimize disruption while following Go's semantic versioning conventions.
The Main Change: Module Path
The only breaking change in v3 is the Go module path:
// v2
import "github.com/casbin/casbin/v2"
// v3
import "github.com/casbin/casbin/v3"
That's it. Your existing policies, models, and code logic don't need to change. The migration is mechanical: update import statements and your go.mod file.
Why Release v3?
You might wonder why release a new major version for just an import path change. The answer is semantic versioning. Go's module system uses the major version number in the import path (/v2, /v3, etc.). This allows multiple major versions to coexist in the same dependency tree, which is crucial for large applications with many dependencies.
By releasing v3, we've:
- Aligned with Go's semantic import versioning
- Enabled gradual migration in complex dependency graphs
- Set the stage for future enhancements without breaking existing v2 users
What Stayed the Same?
Everything else. Seriously.
- All APIs remain identical -
Enforce(),AddPolicy(),GetAllRoles(), and every other method works exactly as before - Model and policy syntax unchanged - Your existing
.confmodel files and.csvpolicy files work without modification - Adapter interface unchanged - The
persist.Adaptermethods are the same; adapters only need a release built against the v3 module path - Performance characteristics - No performance regressions or improvements; it's the same engine
- Middleware integrations - Middleware works the same way once it imports the same major version as your enforcer
New Features in v3
Since the v3.0.0 release in December 2025, several new features have been added:
v3.7.0+: Enhanced Logger Integration
You can now integrate custom loggers into Casbin's core enforcement and policy management operations, providing better observability:
enforcer.SetLogger(yourCustomLogger)
v3.8.0+: GetAllUsers() API
A new API to distinguish users from roles:
users, err := enforcer.GetAllUsers()
This is particularly useful in RBAC systems where you need to list actual users separately from role definitions.
v3.9.0+: Built-in Cycle Detection
The enforcer now includes automatic detection of cycles in role hierarchies, preventing infinite loops:
// Automatically detects and prevents circular role assignments
enforcer.AddGroupingPolicy("alice", "admin")
enforcer.AddGroupingPolicy("admin", "alice") // Will be detected as a cycle
