fix(scripts): still flag a dropped Prisma relation that has a delete or update rule in the schema compatibility check

This commit is contained in:
mateo-berri 2026-10-29 10:09:37 -07:00
parent 2d7305df24
commit e9e68f0559
2 changed files with 14 additions and 3 deletions

View file

@ -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)

View file

@ -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",
)