Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/reference/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ example_operation.validate()
| UpdateDropdownOption | Updates the name of an existing option in a dropdown. | 6 |
| ArchiveDropdownOption | Archives an existing option in a dropdown. | 7 |
| ReorderDropdownOptions | Reorders the options in an existing dropdown. | 8 |
| ArchiveDropdown | Archives an existing dropdown. | 19 |
| ArchiveDropdown | Archives an existing dropdown. | 18 |

### Entity Schema Operations

| Operation Class | Description | Order |
|--------------------------------|-----------------------------------------------------------------------------|-------|
| CreateEntitySchema | Creates a new entity schema with specified fields. | 9 |
| UnarchiveEntitySchema | Unarchives an archived entity schema. | 10 |
| UnarchiveEntitySchema | Unarchives an archived entity schema. | 10 |
| CreateEntitySchemaField | Creates a new field in an existing entity schema. | 11 |
| UpdateEntitySchemaNameTemplate | Updates the name template of an existing entity schema. | 12 |
| UpdateEntitySchema | Updates the properties of an existing entity schema. | 13 |
| UnarchiveEntitySchemaField | Unarchives an archived field in an entity schema. | 14 |
| UpdateEntitySchemaField | Updates the properties of an existing field in an entity schema. | 15 |
| UpdateEntitySchema | Updates the properties of an existing entity schema. | 12 |
| UnarchiveEntitySchemaField | Unarchives an archived field in an entity schema. | 13 |
| UpdateEntitySchemaField | Updates the properties of an existing field in an entity schema. | 14 |
| UpdateEntitySchemaNameTemplate | Updates the name template of an existing entity schema. | 15 |
| ArchiveEntitySchemaField | Archives an existing field in an entity schema. | 16 |
| ReorderEntitySchemaFields | Reorders the fields in an existing entity schema. | 17 |
| ArchiveEntitySchema | Archives an existing entity schema. | 18 |
8 changes: 4 additions & 4 deletions liminal/base/base_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
9. CreateEntitySchema
10. UnarchiveEntitySchema
11. CreateEntitySchemaField
12. UpdateEntitySchemaNameTemplate
13. UpdateEntitySchema
14. UnarchiveEntitySchemaField
15. UpdateEntitySchemaField
12. UpdateEntitySchema
13. UnarchiveEntitySchemaField
14. UpdateEntitySchemaField
15. UpdateEntitySchemaNameTemplate
16. ArchiveEntitySchemaField
17. ReorderEntitySchemaFields
18. ArchiveEntitySchema
Expand Down
8 changes: 7 additions & 1 deletion liminal/base/compare_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
class CompareOperation(BaseModel):
op: BaseOperation
reverse_op: BaseOperation
# Overrides op.order when this operation must run at a different point than its type's default.
order_override: int | None = None

model_config = ConfigDict(arbitrary_types_allowed=True)

@property
def sort_order(self) -> int:
return self.op.order if self.order_override is None else self.order_override

def __lt__(self, other: "CompareOperation") -> bool:
return self.op.order < other.op.order
return self.sort_order < other.sort_order
5 changes: 5 additions & 0 deletions liminal/base/properties/base_field_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def unset_unit_name(self) -> BaseFieldProperties:
self.__pydantic_fields_set__.remove("unit_name")
return self

def unset_parent_link(self) -> BaseFieldProperties:
if "parent_link" in self.__pydantic_fields_set__:
self.__pydantic_fields_set__.remove("parent_link")
return self

def set_type(self, new_type: BenchlingFieldType) -> BaseFieldProperties:
self.type = new_type
return self
Expand Down
5 changes: 5 additions & 0 deletions liminal/base/properties/base_schema_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def set_naming_strategies(
self.naming_strategies = value
return self

def unset_naming_strategies(self) -> BaseSchemaProperties:
if "naming_strategies" in self.__pydantic_fields_set__:
self.__pydantic_fields_set__.remove("naming_strategies")
return self

def merge(self, new_props: BaseSchemaProperties) -> dict[str, Any]:
"""Creates a diff between the current schema properties and the new schema properties.
Sets value to None if the values are equal, otherwise sets the value to the new value.
Expand Down
131 changes: 94 additions & 37 deletions liminal/entity_schemas/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

LOGGER = logging.getLogger(__name__)

# Template based naming strategies need the name template, so the order is overridden to be set right after it.
NAME_TEMPLATE_NAMING_STRATEGY_ORDER = UpdateEntitySchemaNameTemplate.order + 1


def compare_entity_schemas(
benchling_service: BenchlingService, schema_names: set[str] | None = None
Expand Down Expand Up @@ -237,26 +240,50 @@ def compare_entity_schemas(
)
)
if benchling_schema_props != model.__schema_properties__:
ops.append(
CompareOperation(
op=UpdateEntitySchema(
model.__schema_properties__.warehouse_name,
BaseSchemaProperties(
**benchling_schema_props.merge(
model.__schema_properties__
)
new_schema_props = BaseSchemaProperties(
**benchling_schema_props.merge(model.__schema_properties__)
)
rollback_schema_props = BaseSchemaProperties(
**model.__schema_properties__.merge(benchling_schema_props)
)
model_strategies = (
model.__schema_properties__.naming_strategies or set()
)
benchling_strategies = benchling_schema_props.naming_strategies or set()
# A template based naming strategy needs the name template, which runs after UpdateEntitySchema.
# So when one is being added, set all the naming strategies in their own operation after the template.
if any(
BenchlingNamingStrategy.is_template_based(s)
for s in model_strategies - benchling_strategies
):
new_schema_props.unset_naming_strategies()
rollback_schema_props.unset_naming_strategies()
ops.append(
CompareOperation(
op=UpdateEntitySchema(
model_wh_name,
BaseSchemaProperties(
naming_strategies=model_strategies
),
),
),
reverse_op=UpdateEntitySchema(
model.__schema_properties__.warehouse_name,
BaseSchemaProperties(
**model.__schema_properties__.merge(
benchling_schema_props
)
reverse_op=UpdateEntitySchema(
model_wh_name,
BaseSchemaProperties(
naming_strategies=benchling_strategies
),
),
order_override=NAME_TEMPLATE_NAMING_STRATEGY_ORDER,
)
)
if new_schema_props.model_dump(exclude_unset=True) != {}:
ops.append(
CompareOperation(
op=UpdateEntitySchema(model_wh_name, new_schema_props),
reverse_op=UpdateEntitySchema(
model_wh_name, rollback_schema_props
),
),
),
)
)
if benchling_name_template != model.__name_template__:
ops.append(
CompareOperation(
Expand Down Expand Up @@ -300,8 +327,24 @@ def compare_entity_schemas(
for p in field_props
if (p.unit_name and p.warehouse_name)
}
# A link field is created without a target, and Benchling only accepts
# parent_link on a field that has one, so it is set with the target.
parent_links_to_update = {
p.warehouse_name
for p in field_props
if (p.parent_link and p.entity_link and p.warehouse_name)
}
field_props = [
f.unset_tooltip().unset_entity_link().unset_unit_name()
f.unset_tooltip()
.unset_entity_link()
.unset_unit_name()
.unset_parent_link()
for f in field_props
]
field_props = [
f.unset_parent_link()
if f.warehouse_name in parent_links_to_update
else f
for f in field_props
]
field_props = [
Expand Down Expand Up @@ -337,32 +380,20 @@ def compare_entity_schemas(
)
)
# Benchling api does not allow for setting a custom warehouse_name.
# Benchling api also does not allow for setting the name template.
# Because of this, we need to run a separate UpdateEntitySchema to set the warehouse_name if it is different from the snakecase version of the model name.
# We also need to set the template based naming_strategies separately, since the name template must first be set.
new_schema_props = BaseSchemaProperties()
rollback_schema_props = BaseSchemaProperties()
# Later operations on this schema use the model's warehouse_name.
if model_wh_name != benchling_given_wh_name:
new_schema_props.warehouse_name = model_wh_name
rollback_schema_props.warehouse_name = benchling_given_wh_name
if (
model.__schema_properties__.naming_strategies
- standard_naming_strategies
):
new_schema_props.naming_strategies = (
model.__schema_properties__.naming_strategies
)
rollback_schema_props.naming_strategies = standard_naming_strategies
if new_schema_props.model_dump(exclude_unset=True) != {}:
ops.append(
CompareOperation(
op=UpdateEntitySchema(
benchling_given_wh_name,
new_schema_props,
BaseSchemaProperties(warehouse_name=model_wh_name),
),
reverse_op=UpdateEntitySchema(
model_wh_name,
rollback_schema_props,
BaseSchemaProperties(
warehouse_name=benchling_given_wh_name
),
),
)
)
Expand All @@ -382,6 +413,9 @@ def compare_entity_schemas(
)
rollback_field_props.entity_link = None
rollback_field_props.type = BenchlingFieldType.ENTITY_LINK
if wh_field_name in parent_links_to_update:
new_field_props.parent_link = True
rollback_field_props.parent_link = False
if tooltip_value := tooltips_to_update.get(wh_field_name):
new_field_props.tooltip = tooltip_value
rollback_field_props.tooltip = None
Expand All @@ -403,19 +437,20 @@ def compare_entity_schemas(
)
# Benchling api also does not allow for setting the name template when Creating an Entity Schema, since it is a different endpoint.
# Because of this, we need to run a separate UpdateEntitySchemaNameTemplate using the name template endpoint to set the name template on the newly created Entity Schema.
# The name template update runs after the field updates above, since a part can need a link target or parent link set there.
if benchling_given_name_template != model.__name_template__:
ops.append(
CompareOperation(
op=UpdateEntitySchemaNameTemplate(
benchling_given_wh_name,
model_wh_name,
BaseNameTemplate(
**benchling_given_name_template.merge(
model.__name_template__
)
),
),
reverse_op=UpdateEntitySchemaNameTemplate(
benchling_given_wh_name,
model_wh_name,
BaseNameTemplate(
**model.__name_template__.merge(
benchling_given_name_template
Expand All @@ -424,6 +459,28 @@ def compare_entity_schemas(
),
)
)
# Template based naming_strategies need the name template, so they are set right after it.
if (
model.__schema_properties__.naming_strategies
- standard_naming_strategies
):
ops.append(
CompareOperation(
op=UpdateEntitySchema(
model_wh_name,
BaseSchemaProperties(
naming_strategies=model.__schema_properties__.naming_strategies
),
),
reverse_op=UpdateEntitySchema(
model_wh_name,
BaseSchemaProperties(
naming_strategies=standard_naming_strategies
),
),
order_override=NAME_TEMPLATE_NAMING_STRATEGY_ORDER,
)
)

model_operations[model.__schema_properties__.warehouse_name] = ops
running_benchling_schema_names = [
Expand Down
14 changes: 7 additions & 7 deletions liminal/entity_schemas/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


class CreateEntitySchema(BaseOperation):
order: ClassVar[int] = 80
order: ClassVar[int] = 90

def __init__(
self,
Expand Down Expand Up @@ -165,7 +165,7 @@ def _validate_unarchive(


class ArchiveEntitySchema(BaseOperation):
order: ClassVar[int] = 170
order: ClassVar[int] = 180

def __init__(self, wh_schema_name: str) -> None:
self.wh_schema_name = wh_schema_name
Expand Down Expand Up @@ -193,7 +193,7 @@ def _validate(self, benchling_service: BenchlingService) -> EntitySchemaModel:


class UnarchiveEntitySchema(BaseOperation):
order: ClassVar[int] = 90
order: ClassVar[int] = 100

def __init__(self, wh_schema_name: str) -> None:
self.wh_schema_name = wh_schema_name
Expand Down Expand Up @@ -290,7 +290,7 @@ def _validate(self, benchling_service: BenchlingService) -> EntitySchemaModel:


class UpdateEntitySchemaNameTemplate(BaseOperation):
order: ClassVar[int] = 110
order: ClassVar[int] = 150

def __init__(
self,
Expand Down Expand Up @@ -329,7 +329,7 @@ def describe(self) -> str:


class CreateEntitySchemaField(BaseOperation):
order: ClassVar[int] = 100
order: ClassVar[int] = 110

def __init__(
self,
Expand Down Expand Up @@ -442,7 +442,7 @@ def validate(self, benchling_service: BenchlingService) -> None:


class ArchiveEntitySchemaField(BaseOperation):
order: ClassVar[int] = 150
order: ClassVar[int] = 160

def __init__(
self, wh_schema_name: str, wh_field_name: str, index: int | None = None
Expand Down Expand Up @@ -634,7 +634,7 @@ def _validate(self, benchling_service: BenchlingService) -> EntitySchemaModel:


class ReorderEntitySchemaFields(BaseOperation):
order: ClassVar[int] = 160
order: ClassVar[int] = 170

def __init__(self, wh_schema_name: str, new_order: list[str]) -> None:
self.wh_schema_name = wh_schema_name
Expand Down
Loading