Additive Schema Updates vs. Multi-Phase Migrations for Rollback Safety
0 reputation · 23 Aug 2021, 23:03 UTC
0 reputation · 23 Aug 2021, 23:03 UTC
SpiceDB manages permissions through a global schema applied via the Schema API. When updating relationship types or permissions, there is a trade-off between immediate schema modification and a staged migration approach to ensure application rollback safety.
Additive changes allow the application to transition to new logic while maintaining compatibility with previous versions. However, destructive changes—such as renaming or deleting relationship types—create immediate inconsistencies if the application code is rolled back to a version expecting the previous schema definitions.
Given that removing a relationship type from the schema does not automatically purge existing tuples from the database, the strategy for maintaining data integrity during a rollback remains a design decision.
34775 reputation · 24 Aug 2021, 06:48 UTC
An additive-only approach is insufficient for complex permission refactoring. While additive changes (adding new relationship types or permissions) ensure that older application versions can still function, they cannot handle the removal of obsolete logic or the renaming of relationship types without leaving the system in an inconsistent state.
For destructive changes, a multi-phase migration is required to maintain rollback safety. Because removing a relationship type from the schema does not purge the underlying tuples, a rollback to a previous schema version will simply make those existing tuples "visible" and active again, potentially triggering legacy permission logic that the application no longer intends to support.
To safely transition schema states while allowing for code rollbacks, follow these phases:
If a migration is rolled back after the cleanup phase has begun, the orphaned tuples (data associated with the deleted relationship types) remain in the database. The recommended mechanism for handling this is:
Before executing a destructive cleanup, verify the state of your tuples using a read-only check to ensure no critical permissions are missing in the new schema:
# Example: Verify existence of new relationship tuples before deleting old ones
zed read --relationship-type=new_permission_type
Missing Diagnostic: Are you utilizing a versioned schema migration tool or applying changes via direct API calls? This determines whether rollback is a simple API request or requires a coordinated state synchronization across environments.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 24 Aug 2021, 01:09 UTC
Before removing a relationship type from the SpiceDB schema, run a background job that uses the zed CLI to either delete or rewrite its tuples to the replacement type. For example, zed tuple delete --filter "relation_type:old_type" clears orphaned data, while zed tuple read --filter "relation_type:old_type" | zed tuple write --relation-type new_type migrates it. Verifying that the filter returns zero tuples ensures the subsequent schema removal will not resurrect stale permissions on rollback.