Skip to content

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

reversible

Whether this operation can be rolled back.

Returns:

Type Description
bool

True if rollback TypeQL is available

to_typeql abstractmethod

to_typeql()

Generate TypeQL for forward migration.

Returns:

Type Description
str

TypeQL string to execute

Source code in type_bridge/migration/operations.py
@abstractmethod
def to_typeql(self) -> str:
    """Generate TypeQL for forward migration.

    Returns:
        TypeQL string to execute
    """
    pass

to_rollback_typeql abstractmethod

to_rollback_typeql()

Generate TypeQL for rollback.

Returns:

Type Description
str | None

TypeQL string to execute, or None if operation is irreversible

Source code in type_bridge/migration/operations.py
@abstractmethod
def to_rollback_typeql(self) -> str | None:
    """Generate TypeQL for rollback.

    Returns:
        TypeQL string to execute, or None if operation is irreversible
    """
    pass

AddAttribute dataclass

AddAttribute(attribute)

Bases: Operation

Add a new attribute type.

Example

ops.AddAttribute(Phone) # Creates: define attribute phone, value string;

RemoveAttribute dataclass

RemoveAttribute(attribute)

Bases: Operation

Remove an attribute type.

WARNING: This is a BREAKING change. Ensure all attribute instances and ownerships are removed first.

AddEntity dataclass

AddEntity(entity)

Bases: Operation

Add a new entity type.

Example

ops.AddEntity(Person)

RemoveEntity dataclass

RemoveEntity(entity)

Bases: Operation

Remove an entity type.

WARNING: This is a BREAKING change. Ensure all entity instances are deleted first.

AddOwnership dataclass

AddOwnership(owner, attribute, optional=False, key=False, unique=False, card_min=None, card_max=None)

Bases: Operation

Add attribute ownership to an entity or relation.

Example

ops.AddOwnership(Person, Phone, optional=True)

Creates: define person owns phone @card(0..1);

ops.AddOwnership(Person, Email, key=True)

Creates: define person owns email @key;

RemoveOwnership dataclass

RemoveOwnership(owner, attribute)

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

ModifyOwnership(owner, attribute, old_annotations, new_annotations)

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

AddRelation(relation)

Bases: Operation

Add a new relation type with its roles.

Example

ops.AddRelation(Employment)

RemoveRelation dataclass

RemoveRelation(relation)

Bases: Operation

Remove a relation type.

WARNING: This is a BREAKING change. Ensure all relation instances are deleted first.

AddRole dataclass

AddRole(relation, role_name, player_types=list())

Bases: Operation

Add a new role to an existing relation.

Example

ops.AddRole(Employment, "manager", ["person"])

RemoveRole dataclass

RemoveRole(relation, role_name)

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

AddRolePlayer(relation, role_name, player_type)

Bases: Operation

Add a player type to an existing role.

Example

ops.AddRolePlayer(Employment, "employee", "contractor")

Allows Contractor entities to play the employee role

RemoveRolePlayer dataclass

RemoveRolePlayer(relation, role_name, player_type)

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

RunTypeQL(forward, reverse=None)

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

RenameAttribute(old_name, new_name, value_type)

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.