RolebookDocs

Migrations and upgrades

Moving to Rolebook 4, adopting sharing one resource type at a time, and upgrading the sharing packages.

Moving to Rolebook 4

Rolebook 4 is a breaking rewrite of the core. These docs describe version 4 only; earlier APIs are not documented here. The model changes you must account for:

  • Subjects no longer carry roles. A Subject is { type, id, attributes? }. Load the subject’s scoped assignments from your own store and pass them to bind().
  • Roles live at a scope. Every role declares a scope resource type, and every assignment names a concrete scope resource and whether it covers self or descendants.
  • Resources carry their ancestry. Build each check’s target with trustedResource(), including the complete chain from immediate parent to root.
  • Input is validated as complete envelopes. An extra key on a binding, request, assignment, or resource denies instead of being ignored, and undefined values inside snapshots deny. Map rows to exactly the documented fields.
  • Policies are compiled and frozen. Define a policy once with definePolicy() and reuse the object.

Adopting sharing

Sharing is additive: existing Rolebook 4 policies and checks keep working. Adopt it one resource type at a time:

  1. Add owner and member roles, and the three management actions, at the resource’s scope.
  2. Create the sharing tables with the adapter’s migrations. Configure the eligibility fence against your authoritative, database-local organization-member table and its real columns; it must lock member rows in the mutation transaction. With a remote provider (Clerk, WorkOS), coordinate a local projection with membership removal first. If you map workspace roles to assignments, configure the workspace-role fact fence too.
  3. Backfill exactly one ownership row per resource. Reject the rollout for a resource without an unambiguous owner; do not guess from the highest existing role.
  4. Backfill ordinary memberships without mapping any legacy owner-equivalent label to a member role.
  5. Run the adapter contract against the migrated schema, including duplicate resource IDs across tenants and concurrent ownership and invitation cases.
  6. In shadow mode, compare existing decisions and listings with sharing’s. Investigate every additional allow before enforcing.
  7. Route mutations through execute(), then reads and listings. Remove legacy membership writes only after stale-access monitoring is clean.

Invitations. Do not import legacy raw invitation tokens. Reissue pending invitations in the new keyed digest format with bounded expiry and an explicit recipient. Consumed, revoked, or expired legacy invitations stay closed.

Moves and deletions. Wrap application deletes and moves in the adapter’s lifecycle transaction (see PostgreSQL lifecycle). Never call a standalone lifecycle convenience after the application change committed.

Rollback. Keep the old read path during shadowing, but do not dual-write ownership after cutover. Roll back deliberately: freeze sharing mutations, export owner, member, invitation, audit, lifecycle, and idempotency state, restore the previous store, verify authorization and listing parity, then run the adapter’s rollback migrations. Rollback cannot restore deleted or moved application rows or raw invitation secrets.

The sharing packages at 0.1.0

The sharing packages have not had a public release before 0.1.0, so there is no upgrade path to follow yet. The behaviours below are part of the 0.1.0 baseline. They are listed because each one differs from what an application that grew its own sharing layer may expect, and each names what to do when adopting the packages or when a future release changes the behaviour:

ChangeWhat to do
Secret keys are rbk1_ generated keys when given as stringsPass an existing random key as bytes with new TextEncoder().encode(key); replace a passphrase with a generated key. See secret keys.
The delegation ceilingA manager that hands out a role stronger than their own gets FORBIDDEN. Give managers covering roles, or leave those changes to owners. See delegation.
Denials hide existenceCallers without a relationship to a resource get NOT_FOUND_OR_DENIED, not FORBIDDEN. Handle both alike.
Sealed listing cursorsCursors are sealed and bound to the caller; a cursor from another key or another caller rejects with INVALID_CURSOR, and the client restarts from the first page. Configure a shared listingCursorKey across instances.
Time-limited membershipsDo not issue expiring grants until every instance runs a build with expiry.
Audit incarnationsHistory is scoped to a resource’s current incarnation; a deleted and recreated ID starts a new history.
Restricting keys (PostgreSQL) and the key guard (SQLite)Route tenant or ID changes of initialized resources through the move seam, never through a plain UPDATE.
Idempotency created_at and pruningSchedule one pruning job with a retention longer than every client’s retry window.
Better Auth subject attributesprincipal.subject is { type, id } only; email, emailVerified, and workspaceRole live in principal.attributes. Conditions read subject.email and similar from there, including deny rules and exists checks.
Teams team-set factsEvery team-wrapped principal carries a TEAM_SET_AUTHORIZATION_FACT; custom team fences must handle it and return "conflict" when the set changed. See teams.
Versioned sharingFence every writing process’s adapters before the first publish.

Re-run the adapter migrations on every deploy: they validate the existing schema and upgrade it in place, so a future release that changes a table ships as a migration step rather than a manual procedure.