mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(scripts): judge a dropped Prisma relation by its effective delete rule, and run the invitation test on every database setup
This commit is contained in:
parent
e9e68f0559
commit
7957eb2f03
3 changed files with 56 additions and 21 deletions
|
|
@ -13,7 +13,8 @@ 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*:")
|
||||
ON_DELETE: Final = re.compile(r"\bonDelete\s*:\s*(\w+)")
|
||||
ROW_PRESERVING_ON_DELETE: Final = frozenset({"Restrict", "NoAction"})
|
||||
|
||||
|
||||
def parse_model_fields(schema: str) -> Schema:
|
||||
|
|
@ -37,9 +38,17 @@ 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 _on_delete(field_type: str) -> str:
|
||||
written: Final = ON_DELETE.search(field_type)
|
||||
if written is not None:
|
||||
return written[1]
|
||||
return "SetNull" if field_type.split()[0].endswith("?") else "Restrict"
|
||||
|
||||
|
||||
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
|
||||
holds_key: Final = "fields:" in field_type
|
||||
return is_relation and (not holds_key or _on_delete(field_type) in ROW_PRESERVING_ON_DELETE)
|
||||
|
||||
|
||||
def _model_changes(model: str, old_schema: Schema, new_schema: Schema) -> Iterator[str]:
|
||||
|
|
|
|||
|
|
@ -2,18 +2,31 @@ import uuid
|
|||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
import pytest
|
||||
|
||||
from integration._support.client import Gateway, object_value
|
||||
from integration._support.database import scratch_database
|
||||
from integration._support.process import owned_proxy
|
||||
from integration._support.process import DB_PUSH, LEGACY_MIGRATE_DEPLOY, MIGRATE_DEPLOY, owned_proxy
|
||||
|
||||
MASTER_KEY_USER: Final = "default_user_id"
|
||||
|
||||
|
||||
def test_master_key_invites_on_a_proxy_where_nobody_signed_in(gateway: Gateway, tmp_path: Path) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
"database_setup",
|
||||
(DB_PUSH, MIGRATE_DEPLOY, LEGACY_MIGRATE_DEPLOY),
|
||||
ids=("db-push", "v2-resolver", "legacy-resolver"),
|
||||
)
|
||||
def test_master_key_invites_on_a_proxy_where_nobody_signed_in(
|
||||
gateway: Gateway, tmp_path: Path, database_setup: tuple[str, ...]
|
||||
) -> None:
|
||||
with (
|
||||
scratch_database() as database_url,
|
||||
owned_proxy(
|
||||
gateway, tmp_path, {"DATABASE_URL": database_url}, remove_environment=("DATABASE_URL_READ_REPLICA",)
|
||||
gateway,
|
||||
tmp_path,
|
||||
{"DATABASE_URL": database_url},
|
||||
remove_environment=("DATABASE_URL_READ_REPLICA",),
|
||||
database_setup=database_setup,
|
||||
) as proxy,
|
||||
proxy.scenario() as scenario,
|
||||
):
|
||||
|
|
|
|||
|
|
@ -50,18 +50,39 @@ RELATED: Final = (
|
|||
)
|
||||
|
||||
|
||||
def test_schema_compatibility_allows_dropping_relations_but_not_their_columns() -> None:
|
||||
relations_dropped: Final = (
|
||||
"model Owner {\n id String @id\n}\nmodel Item {\n id String @id\n owner_id String?\n created_by String\n}\n"
|
||||
)
|
||||
columns_dropped: Final = "model Owner {\n id String @id\n}\nmodel Item {\n id String @id\n}\n"
|
||||
assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(relations_dropped)) == ()
|
||||
assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(columns_dropped)) == (
|
||||
"Breaking: Field Item.owner_id was removed",
|
||||
def _without(schema: str, *prefixes: str) -> str:
|
||||
return "\n".join(line for line in schema.splitlines() if not line.startswith(prefixes))
|
||||
|
||||
|
||||
def test_schema_compatibility_allows_dropping_a_restrict_relation_but_not_its_column() -> None:
|
||||
relation_dropped: Final = _without(RELATED, " created ", " creator ")
|
||||
column_dropped: Final = _without(RELATED, " created ", " creator ", " created_by ")
|
||||
assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(relation_dropped)) == ()
|
||||
assert check_breaking_changes(parse_model_fields(RELATED), parse_model_fields(column_dropped)) == (
|
||||
"Breaking: Field Item.created_by was removed",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("schema", "removed", "expected"),
|
||||
(
|
||||
pytest.param(RELATED, (" items ", " owner "), "Item.owner", id="optional-relation-defaults-to-set-null"),
|
||||
pytest.param(
|
||||
RELATED.replace("references: [id])\n}", "references: [id], onDelete: Cascade)\n}"),
|
||||
(" created ", " creator "),
|
||||
"Item.creator",
|
||||
id="written-cascade",
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_schema_compatibility_rejects_dropping_a_relation_whose_delete_rule_changes_rows(
|
||||
schema: str, removed: tuple[str, ...], expected: str
|
||||
) -> None:
|
||||
assert check_breaking_changes(parse_model_fields(schema), parse_model_fields(_without(schema, *removed))) == (
|
||||
f"Breaking: Field {expected} was removed",
|
||||
)
|
||||
|
||||
|
||||
def test_schema_compatibility_still_rejects_changes_to_a_kept_relation() -> None:
|
||||
old_creator: Final = 'Owner @relation("Created", fields: [created_by], references: [id])'
|
||||
new_creator: Final = 'Owner @relation("Created", fields: [created_by], references: [id], onDelete: Restrict)'
|
||||
|
|
@ -70,11 +91,3 @@ def test_schema_compatibility_still_rejects_changes_to_a_kept_relation() -> None
|
|||
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",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue