Skip to content

Commit

Permalink
feat(prisma): upgrade to v4.13.0 (#740)
Browse files Browse the repository at this point in the history
## Change Summary

Please summarise the changes this pull request is making here.

## Checklist

- [ ] Unit tests for the changes exist
- [ ] Tests pass without significant drop in coverage
- [ ] Documentation reflects changes where applicable
- [ ] Test snapshots have been
[updated](https://prisma-client-py.readthedocs.io/en/latest/contributing/contributing/#snapshot-tests)
if applicable

## Agreement

By submitting this pull request, I confirm that you can use, modify,
copy and redistribute this contribution, under the terms of your choice.
  • Loading branch information
RobertCraigie committed Apr 30, 2023
1 parent a415c63 commit 70951b2
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img src="https://img.shields.io/discord/933860922039099444?color=blue&label=chat&logo=discord" alt="Chat on Discord">
</a>
<a href="https://prisma.io">
<img src="https://img.shields.io/static/v1?label=prisma&message=4.11.0&color=blue&logo=prisma" alt="Supported Prisma version is 4.11.0">
<img src="https://img.shields.io/static/v1?label=prisma&message=4.13.0&color=blue&logo=prisma" alt="Supported Prisma version is 4.13.0">
</a>
<a href="https://github.com/grantjenks/blue">
<img src="https://camo.githubusercontent.com/dbdbcf26db37abfa1f2ab7e6c28c8b3a199f2dad98e4ef53a50e9c45c7e4ace8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532307374796c652d626c75652d626c75652e737667" alt="Code style: blue">
Expand Down
20 changes: 18 additions & 2 deletions databases/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import pytest

import prisma
from prisma import Prisma
from prisma.errors import FieldNotFoundError, ForeignKeyViolationError

Expand All @@ -13,7 +14,7 @@ async def test_field_not_found_error(client: Prisma) -> None:
with pytest.raises(FieldNotFoundError, match='bad_field'):
await client.post.find_first(where={'bad_field': 'foo'}) # type: ignore

with pytest.raises(FieldNotFoundError, match='foo'):
with pytest.raises(FieldNotFoundError, match='data.foo'):
await client.post.create(
data={
'title': 'foo',
Expand All @@ -22,7 +23,7 @@ async def test_field_not_found_error(client: Prisma) -> None:
}
)

with pytest.raises(FieldNotFoundError, match='foo'):
with pytest.raises(FieldNotFoundError, match='where.author.is.foo'):
await client.post.find_first(
where={
'author': {
Expand All @@ -34,6 +35,21 @@ async def test_field_not_found_error(client: Prisma) -> None:
)


@pytest.mark.asyncio
@pytest.mark.prisma
async def test_field_not_found_error_selection() -> None:
"""The FieldNotFoundError is raised when an unknown field is passed to selections."""

class CustomPost(prisma.bases.BasePost):
foo_field: str

with pytest.raises(
FieldNotFoundError,
match=r'Field \'foo_field\' not found in enclosing type \'Post\'',
):
await CustomPost.prisma().find_first()


@pytest.mark.asyncio
async def test_foreign_key_violation_error(client: Prisma) -> None:
"""The ForeignKeyViolationError is raised when a foreign key is invalid."""
Expand Down
13 changes: 10 additions & 3 deletions databases/tests/test_find_many.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ async def test_ordering(client: Prisma) -> None:
assert found[1].published is False
assert found[2].published is False


@pytest.mark.asyncio
@pytest.mark.skip(
reason='incorrect error is raised here - requires an overhaul of the error system'
)
async def test_too_many_fields_error(client: Prisma) -> None:
"""Passing in multiple fields in order is not supported"""
with pytest.raises(prisma.errors.DataError) as exc:
await client.post.find_many(
where={
Expand All @@ -302,11 +309,11 @@ async def test_ordering(client: Prisma) -> None:
@pytest.mark.asyncio
async def test_order_field_not_nullable(client: Prisma) -> None:
"""Order by fields, if present, cannot be None"""
with pytest.raises(prisma.errors.FieldNotFoundError) as exc:
with pytest.raises(
prisma.errors.FieldNotFoundError, match=r'orderBy.desc'
):
await client.post.find_many(order={'desc': None}) # type: ignore

assert exc.match(r'desc')


@pytest.mark.asyncio
async def test_distinct(client: Prisma) -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img src="https://img.shields.io/discord/933860922039099444?color=blue&label=chat&logo=discord" alt="Chat on Discord">
</a>
<a href="https://prisma.io">
<img src="https://img.shields.io/static/v1?label=prisma&message=4.11.0&color=blue&logo=prisma" alt="Supported Prisma version is 4.11.0">
<img src="https://img.shields.io/static/v1?label=prisma&message=4.13.0&color=blue&logo=prisma" alt="Supported Prisma version is 4.13.0">
</a>
<a href="https://github.com/grantjenks/blue">
<img src="https://camo.githubusercontent.com/dbdbcf26db37abfa1f2ab7e6c28c8b3a199f2dad98e4ef53a50e9c45c7e4ace8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f64652532307374796c652d626c75652d626c75652e737667" alt="Code style: blue">
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/binaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Prisma Client Python _should_ automatically download the correct binaries for yo
- Clone the prisma-engines repository at the current version that the python client supports:

```
git clone https://github.com/prisma/prisma-engines --branch=4.11.0
git clone https://github.com/prisma/prisma-engines --branch=4.13.0
```

- Build the binaries following the steps found [here](https://github.com/prisma/prisma-engines#building-prisma-engines)
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ This option controls the version of Prisma to use. It should be noted that this

| Option | Environment Variable | Default |
| ---------------- | --------------------- | -------- |
| `prisma_version` | `PRISMA_VERSION` | `4.11.0` |
| `prisma_version` | `PRISMA_VERSION` | `4.13.0` |

### Expected Engine Version

This is an internal option that is here as a safeguard for the `prisma_version` option. If you modify the `prisma_version` option then you must also update this option to use the corresponding engine version. You can find a list of engine versions [here](https://github.com/prisma/prisma-engines).

| Option | Environment Variable | Default |
| ------------------------- | -------------------------------- | ------------------------------------------ |
| `expected_engine_version` | `PRISMA_EXPECTED_ENGINE_VERSION` | `8fde8fef4033376662cad983758335009d522acb` |
| `expected_engine_version` | `PRISMA_EXPECTED_ENGINE_VERSION` | `1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a` |


### Binary Platform
Expand Down
1 change: 1 addition & 0 deletions src/prisma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
models as models,
partials as partials,
types as types,
bases as bases,
)
except ModuleNotFoundError:
# code has not been generated yet
Expand Down
4 changes: 2 additions & 2 deletions src/prisma/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class DefaultConfig(BaseSettings):
# doesn't change then the CLI is incorrectly cached
prisma_version: str = Field(
env='PRISMA_VERSION',
default='4.11.0',
default='4.13.0',
)

# Engine binary versions can be found under https://github.com/prisma/prisma-engine/commits/main
expected_engine_version: str = Field(
env='PRISMA_EXPECTED_ENGINE_VERSION',
default='8fde8fef4033376662cad983758335009d522acb',
default='1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a',
)

# Home directory, used to build the `binary_cache_dir` option by default, useful in multi-user
Expand Down
21 changes: 19 additions & 2 deletions src/prisma/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
'P2025': prisma_errors.RecordNotFoundError,
}

META_ERROR_MAPPING: dict[str, type[Exception]] = {
'UnknownArgument': prisma_errors.FieldNotFoundError,
'UnknownInputField': prisma_errors.FieldNotFoundError,
'UnknownSelectionField': prisma_errors.FieldNotFoundError,
}


def query_engine_name() -> str:
return f'prisma-query-engine-{platform.check_for_extension(platform.binary_platform())}'
Expand Down Expand Up @@ -164,10 +170,21 @@ def handle_response_errors(resp: AbstractResponse[Any], data: Any) -> NoReturn:
if 'A value is required but not set' in message:
raise prisma_errors.MissingRequiredValueError(error)

exc = ERROR_MAPPING.get(code)
exc: type[Exception] | None = None

kind = user_facing.get('meta', {}).get('kind')
if kind is not None:
exc = META_ERROR_MAPPING.get(kind)

if exc is None:
exc = ERROR_MAPPING.get(code)

if exc is not None:
raise exc(error)
except (KeyError, TypeError):
except (KeyError, TypeError) as err:
log.debug(
'Ignoring error while constructing specialized error %s', err
)
continue

try:
Expand Down
29 changes: 28 additions & 1 deletion src/prisma/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any, Optional


Expand Down Expand Up @@ -104,7 +106,21 @@ class FieldNotFoundError(DataError):
# currently we cannot easily resolve the erroneous field as Prisma
# returns different results for unknown fields in different situations
# e.g. root query, nested query and mutation queries
...
def __init__(self, data: Any, *, message: str | None = None) -> None:
if message is None:
meta = data.get('user_facing_error', {}).get('meta', {})
if meta.get('kind') == 'Union':
error = _pick_union_error(meta.get('errors', []))
else:
error = meta

argument_path = error.get('argumentPath')
selection_path = error.get('selectionPath')

if argument_path:
message = f'Could not find field at `{".".join(selection_path)}.{".".join(argument_path)}`'

super().__init__(data, message=message)


class RecordNotFoundError(DataError):
Expand Down Expand Up @@ -160,3 +176,14 @@ class PrismaWarning(Warning):
# Note: this is currently unused but not worth removing
class UnsupportedSubclassWarning(PrismaWarning):
pass


# TODO: proper types
def _pick_union_error(errors: list[Any]) -> Any:
# Note: uses the same heuristic as the TS client
return max(
errors,
key=lambda e: (
len(e.get('argumentPath', [])) + len(e.get('selectionPath'))
),
)

0 comments on commit 70951b2

Please sign in to comment.