diff --git a/scripts/check_schema_compatibility.py b/scripts/check_schema_compatibility.py index 20e65cb986e..8d6f3e395ba 100644 --- a/scripts/check_schema_compatibility.py +++ b/scripts/check_schema_compatibility.py @@ -13,6 +13,7 @@ from pydantic import TypeAdapter Schema: TypeAlias = Mapping[str, Mapping[str, str]] MODELS: Final = re.compile(r"^[ \t]*model\s+(\w+)\s*\{(.*?)^[ \t]*\}", re.MULTILINE | re.DOTALL) +REFERENTIAL_ACTION: Final = re.compile(r"\bon(?:Delete|Update)\s*:") def parse_model_fields(schema: str) -> Schema: @@ -36,8 +37,9 @@ def _field_changes(model: str, field: str, old_type: str, new_fields: Mapping[st yield f"Breaking: Field {model}.{field} changed type from {old_type} to {new_type}" -def _is_relation(field_type: str, old_schema: Schema) -> bool: - return re.split(r"[\[?\s]", field_type, maxsplit=1)[0] in old_schema +def _is_droppable_relation(field_type: str, old_schema: Schema) -> bool: + is_relation: Final = re.split(r"[\[?\s]", field_type, maxsplit=1)[0] in old_schema + return is_relation and REFERENTIAL_ACTION.search(field_type) is None def _model_changes(model: str, old_schema: Schema, new_schema: Schema) -> Iterator[str]: @@ -46,7 +48,7 @@ def _model_changes(model: str, old_schema: Schema, new_schema: Schema) -> Iterat return new_fields: Final = new_schema[model] for field, old_type in old_schema[model].items(): - if field in new_fields or not _is_relation(old_type, old_schema): + if field in new_fields or not _is_droppable_relation(old_type, old_schema): yield from _field_changes(model, field, old_type, new_fields) diff --git a/tests/unit/proxy/test_db_schema_changes.py b/tests/unit/proxy/test_db_schema_changes.py index bbd31b13123..461d814890c 100644 --- a/tests/unit/proxy/test_db_schema_changes.py +++ b/tests/unit/proxy/test_db_schema_changes.py @@ -69,3 +69,12 @@ def test_schema_compatibility_still_rejects_changes_to_a_kept_relation() -> None assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(restricted)) == ( f"Breaking: Field Item.creator changed type from {old_creator} to {new_creator}", ) + + +def test_schema_compatibility_rejects_dropping_a_relation_with_a_referential_action() -> None: + cascading: Final = RELATED.replace("references: [id])\n creator", "references: [id], onDelete: Cascade)\n creator") + kept_lines: Final = (line for line in cascading.splitlines() if not line.startswith((" items ", " owner "))) + owner_dropped: Final = "\n".join(kept_lines) + assert check_breaking_changes(parse_model_fields(cascading), parse_model_fields(owner_dropped)) == ( + "Breaking: Field Item.owner was removed", + )