type_bridge.migration.operations¶
operations
¶
Migration operations for TypeDB schema changes.
Operations define atomic schema changes that can be applied to a TypeDB database. Each operation can generate forward TypeQL and optionally rollback TypeQL.
Example
from type_bridge.migration import operations as ops
operations = [ ops.AddAttribute(Phone), ops.AddOwnership(Person, Phone, optional=True), ops.RunTypeQL( forward="match $p isa person; insert $p has phone 'unknown';", reverse="match $p isa person, has phone 'unknown'; delete $p has phone 'unknown';", ), ]
Operation
¶
Bases: ABC
Base class for migration operations.
Operations must implement: - to_typeql(): Generate forward migration TypeQL - to_rollback_typeql(): Generate rollback TypeQL (or None if irreversible)
reversible
property
¶
Whether this operation can be rolled back.
Returns:
| Type | Description |
|---|---|
bool
|
True if rollback TypeQL is available |
to_typeql
abstractmethod
¶
to_rollback_typeql
abstractmethod
¶
Generate TypeQL for rollback.
Returns:
| Type | Description |
|---|---|
str | None
|
TypeQL string to execute, or None if operation is irreversible |
AddAttribute
dataclass
¶
Bases: Operation
Add a new attribute type.
Example
ops.AddAttribute(Phone) # Creates: define attribute phone, value string;
RemoveAttribute
dataclass
¶
Bases: Operation
Remove an attribute type.
WARNING: This is a BREAKING change. Ensure all attribute instances and ownerships are removed first.
AddEntity
dataclass
¶
RemoveEntity
dataclass
¶
Bases: Operation
Remove an entity type.
WARNING: This is a BREAKING change. Ensure all entity instances are deleted first.
AddOwnership
dataclass
¶
RemoveOwnership
dataclass
¶
Bases: Operation
Remove attribute ownership from an entity or relation.
WARNING: This may orphan attribute data. Ensure attribute values are removed from instances first.
ModifyOwnership
dataclass
¶
Bases: Operation
Modify ownership annotations (cardinality, key, unique).
Example
ops.ModifyOwnership( Person, Phone, old_annotations="@card(0..1)", new_annotations="@card(1..1)" )
AddRelation
dataclass
¶
RemoveRelation
dataclass
¶
Bases: Operation
Remove a relation type.
WARNING: This is a BREAKING change. Ensure all relation instances are deleted first.
AddRole
dataclass
¶
Bases: Operation
Add a new role to an existing relation.
Example
ops.AddRole(Employment, "manager", ["person"])
RemoveRole
dataclass
¶
Bases: Operation
Remove a role from a relation.
WARNING: This is a BREAKING change. Ensure no relation instances have role players for this role.
AddRolePlayer
dataclass
¶
RemoveRolePlayer
dataclass
¶
Bases: Operation
Remove a player type from a role.
WARNING: This is a BREAKING change. Ensure no relation instances have this player type in this role.
RunTypeQL
dataclass
¶
Bases: Operation
Execute arbitrary TypeQL for complex migrations.
Use this for: - Data migrations (updating existing data) - Complex schema changes not covered by other operations - Renaming attributes (requires data migration)
Example
ops.RunTypeQL( forward=""" match $p isa person; not { $p has phone $ph; }; insert $p has phone "unknown"; """, reverse=""" match $p isa person, has phone "unknown"; delete $p has phone "unknown"; """ )
RenameAttribute
dataclass
¶
Bases: Operation
Rename an attribute type.
WARNING: This is a complex operation that requires both schema and data migration. Consider using RunTypeQL for full control.
This operation: 1. Creates new attribute type 2. Migrates data from old to new 3. Removes old attribute type
Note: Rollback is not supported for this operation.