graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
"""`EntityRepository` over Django models.
|
|
2
|
+
|
|
3
|
+
There is no mapper: a record *is* `Entity.to_dict()`. Reads go out through
|
|
4
|
+
`.values()` and are split back into `Entity.from_dict()`'s input by the
|
|
5
|
+
bound model's `_meta`. Typed fields map to columns; the nested attributes
|
|
6
|
+
bag maps directly to its JSON column.
|
|
7
|
+
A second backend only has to reimplement this file.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections import defaultdict
|
|
12
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
13
|
+
from typing import Optional
|
|
14
|
+
import asyncio
|
|
15
|
+
import logging
|
|
16
|
+
|
|
17
|
+
from asgiref.sync import sync_to_async
|
|
18
|
+
from django.contrib.postgres.expressions import ArraySubquery
|
|
19
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
20
|
+
from django.db import IntegrityError, connections, transaction
|
|
21
|
+
from django.db.models import CharField, F, JSONField, OuterRef, TextField
|
|
22
|
+
|
|
23
|
+
from graphcore.domain.entity.Entity import Entity
|
|
24
|
+
from graphcore.errors import ConcurrentModificationError, DuplicateEntity, PreconditionFailed
|
|
25
|
+
from graphcore.persistence.django.backends import check_supported, is_unique_violation
|
|
26
|
+
from graphcore.persistence.django.compiler import QueryCompiler
|
|
27
|
+
from graphcore.persistence.django.models import BackendIdentityModel, EntityModel, EntityReferenceModel
|
|
28
|
+
from graphcore.persistence.django.threads import use_shared_threads
|
|
29
|
+
from graphcore.registry.EntityDirectory import StorageNotBound
|
|
30
|
+
from graphcore.persistence.repository import (
|
|
31
|
+
NOT_LOADED, DeleteReceipt, DeletedEntity, EntityRepository, Precondition, SaveReceipt,
|
|
32
|
+
project_reference_ids,
|
|
33
|
+
)
|
|
34
|
+
from graphcore.types.entities import EntityPage
|
|
35
|
+
from graphcore.types.entity_id import is_entity_id
|
|
36
|
+
from graphcore.types.query import (
|
|
37
|
+
AllOf, ContainsAny, ContainsValue, EntityQuery, InValues, PageRequest, Predicate,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
logger = logging.getLogger(__name__)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_COLUMNS_CACHE: dict = {}
|
|
44
|
+
_TYPE_HINT_LIMIT = 50_000
|
|
45
|
+
_CHILD_ORDER = ('sibling_position', 'created_at', 'entity_id')
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _columns(model_cls) -> dict:
|
|
49
|
+
"""`{attname: Field}` for every concrete column on `model_cls`, cached
|
|
50
|
+
per model. `attname` is what a record key and a `**kwargs` constructor
|
|
51
|
+
call both use (e.g. a `ForeignKey`'s is `user_id`, not `user`)."""
|
|
52
|
+
columns = _COLUMNS_CACHE.get(model_cls)
|
|
53
|
+
if columns is None:
|
|
54
|
+
columns = {field.attname: field for field in model_cls._meta.concrete_fields}
|
|
55
|
+
_COLUMNS_CACHE[model_cls] = columns
|
|
56
|
+
return columns
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _dependent_column(model_cls, field: str, entity_cls):
|
|
60
|
+
"""The column behind a `dependent_on` field: a string column, or a JSON
|
|
61
|
+
list column."""
|
|
62
|
+
column = _columns(model_cls).get(field)
|
|
63
|
+
if isinstance(column, (CharField, TextField)) or (
|
|
64
|
+
isinstance(column, JSONField) and column.default is list
|
|
65
|
+
):
|
|
66
|
+
return column
|
|
67
|
+
raise ImproperlyConfigured(
|
|
68
|
+
f"{entity_cls.__name__}.dependent_on names {field!r}, which is not a string or "
|
|
69
|
+
f"JSON list column of {model_cls.__name__}"
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _row_ids(entity_ids) -> list:
|
|
74
|
+
"""Ids that could name a row. Subject ids reaching the repository are not
|
|
75
|
+
all entity ids (PUBLIC, a bare user id) - those resolve to nothing rather
|
|
76
|
+
than to a scan for a value no row can hold."""
|
|
77
|
+
return [
|
|
78
|
+
entity_id for entity_id in dict.fromkeys(str(eid) for eid in entity_ids)
|
|
79
|
+
if is_entity_id(entity_id)
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _read_now(query):
|
|
84
|
+
"""Run one blocking ORM read, even from inside an event loop.
|
|
85
|
+
|
|
86
|
+
Composition is synchronous but the first call in a process can land in
|
|
87
|
+
an async entrypoint, where the ORM refuses a sync query. One throwaway
|
|
88
|
+
thread, once per process - never on a request path."""
|
|
89
|
+
try:
|
|
90
|
+
asyncio.get_running_loop()
|
|
91
|
+
except RuntimeError:
|
|
92
|
+
return query()
|
|
93
|
+
|
|
94
|
+
def isolated():
|
|
95
|
+
try:
|
|
96
|
+
return query()
|
|
97
|
+
finally:
|
|
98
|
+
connections.close_all()
|
|
99
|
+
|
|
100
|
+
with ThreadPoolExecutor(max_workers=1) as pool:
|
|
101
|
+
return pool.submit(isolated).result()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _insert(instance) -> None:
|
|
105
|
+
# Own savepoint: a multi-table insert that fails must not poison an
|
|
106
|
+
# enclosing transaction the caller goes on to read from.
|
|
107
|
+
try:
|
|
108
|
+
with transaction.atomic():
|
|
109
|
+
instance.save(force_insert=(EntityModel,))
|
|
110
|
+
except IntegrityError as e:
|
|
111
|
+
if is_unique_violation(e):
|
|
112
|
+
raise DuplicateEntity(
|
|
113
|
+
f"Entity {instance.entity_id} duplicates a unique value held by another entity",
|
|
114
|
+
entity_id=str(instance.entity_id),
|
|
115
|
+
) from e
|
|
116
|
+
raise
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class DjangoEntityRepository(EntityRepository):
|
|
120
|
+
def __init__(self, entity_directory, db_threads: int = 4) -> None:
|
|
121
|
+
check_supported(connections['default'])
|
|
122
|
+
self.entity_directory = entity_directory
|
|
123
|
+
self.db_threads = db_threads
|
|
124
|
+
self.compiler = QueryCompiler(entity_directory)
|
|
125
|
+
self._storage_validated = False
|
|
126
|
+
self._backend_id: Optional[str] = None
|
|
127
|
+
self._type_hints: dict[str, str] = {}
|
|
128
|
+
# No portable ordered array aggregate: elsewhere children are a second query.
|
|
129
|
+
self._children_in_row = connections['default'].vendor == 'postgresql'
|
|
130
|
+
|
|
131
|
+
def backend_id(self) -> str:
|
|
132
|
+
if self._backend_id is None:
|
|
133
|
+
self._backend_id = _read_now(
|
|
134
|
+
lambda: str(BackendIdentityModel.objects.values_list('backend_id', flat=True).get())
|
|
135
|
+
)
|
|
136
|
+
return self._backend_id
|
|
137
|
+
|
|
138
|
+
def use_shared_threads(self) -> None:
|
|
139
|
+
use_shared_threads(self.db_threads)
|
|
140
|
+
|
|
141
|
+
def _validate_storage(self) -> None:
|
|
142
|
+
"""Every registered entity type must resolve to a real `EntityModel`
|
|
143
|
+
subclass - checked once, lazily, so a misconfigured binding fails
|
|
144
|
+
loudly on first use rather than as a mysterious query error."""
|
|
145
|
+
if self._storage_validated:
|
|
146
|
+
return
|
|
147
|
+
for entity_cls in self.entity_directory.get_all_entities().values():
|
|
148
|
+
storage = self.entity_directory.storage(entity_cls)
|
|
149
|
+
if not (isinstance(storage, type) and issubclass(storage, EntityModel)):
|
|
150
|
+
raise StorageNotBound(
|
|
151
|
+
f"{entity_cls.__name__} is bound to {storage!r}, which is not an "
|
|
152
|
+
f"EntityModel subclass."
|
|
153
|
+
)
|
|
154
|
+
for field in entity_cls.dependent_on:
|
|
155
|
+
_dependent_column(storage, field, entity_cls)
|
|
156
|
+
self._storage_validated = True
|
|
157
|
+
|
|
158
|
+
# --- record <-> entity ------------------------------------------------
|
|
159
|
+
|
|
160
|
+
def _hydrate(self, row: dict, entity_cls, model_cls) -> Entity:
|
|
161
|
+
"""A `.values()` row as an `Entity`. Only the `attributes` bag and
|
|
162
|
+
editable concrete columns are the entity's neutral state - a
|
|
163
|
+
non-editable columns are storage details, except for entity identity."""
|
|
164
|
+
row = dict(row)
|
|
165
|
+
if row.get('entity_id') is not None:
|
|
166
|
+
row['entity_id'] = str(row['entity_id'])
|
|
167
|
+
attributes = row.pop('attributes', None)
|
|
168
|
+
columns = _columns(model_cls)
|
|
169
|
+
data = {'attributes': attributes}
|
|
170
|
+
for key, value in row.items():
|
|
171
|
+
if key == 'entity_id':
|
|
172
|
+
data[key] = value
|
|
173
|
+
continue
|
|
174
|
+
field = columns.get(key)
|
|
175
|
+
# A field's own primary key (a plain model's `entity_id`, or an
|
|
176
|
+
# MTI child's `<parent>_ptr_id`) is a storage identity detail,
|
|
177
|
+
# never part of `to_dict()`.
|
|
178
|
+
if field is not None and field.editable and not field.primary_key:
|
|
179
|
+
data[key] = value
|
|
180
|
+
entity = entity_cls.from_dict(data)
|
|
181
|
+
entity._ancestor_ids = tuple(str(x) for x in (row.get('ancestor_ids') or ()))
|
|
182
|
+
entity._reference_ids = tuple(str(x) for x in (row.get('reference_ids') or ()))
|
|
183
|
+
return entity
|
|
184
|
+
|
|
185
|
+
def _split(self, entity: Entity, model_cls) -> dict:
|
|
186
|
+
"""`entity.to_dict()` split into a write record: editable concrete
|
|
187
|
+
columns at the top level, including the nested `attributes` bag."""
|
|
188
|
+
columns = _columns(model_cls)
|
|
189
|
+
data = entity.to_dict()
|
|
190
|
+
for key in data:
|
|
191
|
+
if key not in columns:
|
|
192
|
+
raise ImproperlyConfigured(
|
|
193
|
+
f"{type(entity).__name__}.to_dict() emits {key!r} but "
|
|
194
|
+
f"{model_cls.__name__} has no column for it"
|
|
195
|
+
)
|
|
196
|
+
record = {
|
|
197
|
+
k: v for k, v in data.items()
|
|
198
|
+
if k in columns and columns[k].editable and not columns[k].primary_key
|
|
199
|
+
}
|
|
200
|
+
record['reference_ids'] = project_reference_ids(data)
|
|
201
|
+
return record
|
|
202
|
+
|
|
203
|
+
# --- reads ----------------------------------------------------------
|
|
204
|
+
|
|
205
|
+
async def load(self, entity_id: str) -> Optional[Entity]:
|
|
206
|
+
return (await self.load_many([entity_id])).get(str(entity_id))
|
|
207
|
+
|
|
208
|
+
async def load_many(self, entity_ids) -> dict:
|
|
209
|
+
ids = _row_ids(entity_ids)
|
|
210
|
+
if not ids:
|
|
211
|
+
return {}
|
|
212
|
+
self._validate_storage()
|
|
213
|
+
|
|
214
|
+
hinted = defaultdict(list)
|
|
215
|
+
unhinted = []
|
|
216
|
+
for entity_id in ids:
|
|
217
|
+
if entity_id in self._type_hints:
|
|
218
|
+
hinted[self._type_hints[entity_id]].append(entity_id)
|
|
219
|
+
else:
|
|
220
|
+
unhinted.append(entity_id)
|
|
221
|
+
entities = await self._load_typed(hinted)
|
|
222
|
+
|
|
223
|
+
# A hint is unverified until its typed row comes back: the id may
|
|
224
|
+
# since have been deleted, or recreated as another type.
|
|
225
|
+
for group_ids in hinted.values():
|
|
226
|
+
for entity_id in group_ids:
|
|
227
|
+
if entity_id not in entities:
|
|
228
|
+
self._type_hints.pop(entity_id, None)
|
|
229
|
+
unhinted.append(entity_id)
|
|
230
|
+
|
|
231
|
+
if unhinted:
|
|
232
|
+
by_type = defaultdict(list)
|
|
233
|
+
async for row in EntityModel.objects.filter(entity_id__in=unhinted).values(
|
|
234
|
+
'entity_id', 'entity_type_id'
|
|
235
|
+
):
|
|
236
|
+
by_type[row['entity_type_id']].append(str(row['entity_id']))
|
|
237
|
+
entities.update(await self._load_typed(by_type))
|
|
238
|
+
|
|
239
|
+
if not self._children_in_row:
|
|
240
|
+
await self._attach_children(entities)
|
|
241
|
+
return entities
|
|
242
|
+
|
|
243
|
+
async def _load_typed(self, by_type) -> dict:
|
|
244
|
+
"""Hydrate each `{entity_type_id: ids}` group from its storage model,
|
|
245
|
+
keeping only rows whose stored type is the group's."""
|
|
246
|
+
entities = {}
|
|
247
|
+
for entity_type_id, group_ids in by_type.items():
|
|
248
|
+
entity_cls = self.entity_directory.get_entity_class(entity_type_id)
|
|
249
|
+
if entity_cls is None:
|
|
250
|
+
logger.warning("Unknown entity_type_id during bulk load: %s", entity_type_id)
|
|
251
|
+
continue
|
|
252
|
+
model_cls = self.entity_directory.storage(entity_cls)
|
|
253
|
+
rows = model_cls.objects.filter(entity_id__in=group_ids, entity_type_id=entity_type_id)
|
|
254
|
+
if self._children_in_row:
|
|
255
|
+
rows = rows.annotate(gc_children_ids=ArraySubquery(
|
|
256
|
+
EntityModel.objects.filter(parent_id=OuterRef('entity_id'))
|
|
257
|
+
.order_by(*_CHILD_ORDER).values('entity_id')
|
|
258
|
+
))
|
|
259
|
+
async for record in rows.values():
|
|
260
|
+
children_ids = record.pop('gc_children_ids', None)
|
|
261
|
+
entity = self._hydrate(record, entity_cls, model_cls)
|
|
262
|
+
if children_ids:
|
|
263
|
+
entity._children_ids = tuple(str(child_id) for child_id in children_ids)
|
|
264
|
+
entities[entity.entity_id] = entity
|
|
265
|
+
self._remember_type(entity.entity_id, entity_type_id)
|
|
266
|
+
return entities
|
|
267
|
+
|
|
268
|
+
def _remember_type(self, entity_id: str, entity_type_id: str) -> None:
|
|
269
|
+
if entity_id not in self._type_hints and len(self._type_hints) >= _TYPE_HINT_LIMIT:
|
|
270
|
+
del self._type_hints[next(iter(self._type_hints))]
|
|
271
|
+
self._type_hints[entity_id] = entity_type_id
|
|
272
|
+
|
|
273
|
+
async def _attach_children(self, entities: dict) -> None:
|
|
274
|
+
"""One query regardless of how many entities are being hydrated."""
|
|
275
|
+
if not entities:
|
|
276
|
+
return
|
|
277
|
+
async for row in EntityModel.objects.filter(parent_id__in=list(entities)).order_by(
|
|
278
|
+
*_CHILD_ORDER
|
|
279
|
+
).values('parent_id', 'entity_id'):
|
|
280
|
+
parent = entities.get(str(row['parent_id']))
|
|
281
|
+
if parent is not None:
|
|
282
|
+
parent._children_ids = parent._children_ids + (str(row['entity_id']),)
|
|
283
|
+
|
|
284
|
+
async def versions(self, entity_ids) -> dict:
|
|
285
|
+
ids = _row_ids(entity_ids)
|
|
286
|
+
if not ids:
|
|
287
|
+
return {}
|
|
288
|
+
return {
|
|
289
|
+
str(row['entity_id']): row['version']
|
|
290
|
+
async for row in EntityModel.objects.filter(entity_id__in=ids).values(
|
|
291
|
+
'entity_id', 'version'
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async def list_entities(self, query: EntityQuery) -> EntityPage:
|
|
296
|
+
return await self._page(
|
|
297
|
+
EntityModel.objects.filter(self.compiler.compile(query.predicate)), query.page,
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
async def referrers(self, target_id: str, predicate: Predicate, page: PageRequest) -> EntityPage:
|
|
301
|
+
# The cursor is repeated on the edge column, in the same `filter()`
|
|
302
|
+
# so it binds the same join: Postgres carries equalities across a
|
|
303
|
+
# join but not ranges, so without it every page rescans the target's
|
|
304
|
+
# edges from the start.
|
|
305
|
+
edges = {'references__target_id': str(target_id)}
|
|
306
|
+
if page.cursor:
|
|
307
|
+
edges['references__source_id__gt'] = page.cursor
|
|
308
|
+
return await self._page(
|
|
309
|
+
EntityModel.objects.filter(**edges).filter(self.compiler.compile(predicate)), page,
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
async def _page(self, queryset, page: PageRequest) -> EntityPage:
|
|
313
|
+
if page.cursor:
|
|
314
|
+
queryset = queryset.filter(entity_id__gt=page.cursor)
|
|
315
|
+
|
|
316
|
+
ids = [
|
|
317
|
+
str(entity_id) async for entity_id in
|
|
318
|
+
queryset.order_by('entity_id').values_list('entity_id', flat=True)[:page.limit + 1]
|
|
319
|
+
]
|
|
320
|
+
has_more = len(ids) > page.limit
|
|
321
|
+
ids = ids[:page.limit]
|
|
322
|
+
|
|
323
|
+
hydrated = await self.load_many(ids)
|
|
324
|
+
return EntityPage(
|
|
325
|
+
entities=[hydrated[eid] for eid in ids if eid in hydrated],
|
|
326
|
+
next_cursor=ids[-1] if has_more and ids else None,
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
async def list_ids(self, predicate: Predicate) -> set:
|
|
330
|
+
return {
|
|
331
|
+
str(entity_id) async for entity_id in
|
|
332
|
+
EntityModel.objects.filter(self.compiler.compile(predicate)).values_list(
|
|
333
|
+
'entity_id', flat=True
|
|
334
|
+
)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async def descendant_ids(self, root_id: str) -> set:
|
|
338
|
+
return await self.list_ids(ContainsValue('ancestor_ids', str(root_id)))
|
|
339
|
+
|
|
340
|
+
async def dependent_ids(self, type_ids: frozenset, field: str, target_ids: set) -> set:
|
|
341
|
+
self._validate_storage()
|
|
342
|
+
targets = {str(t) for t in target_ids}
|
|
343
|
+
if not targets or not type_ids:
|
|
344
|
+
return set()
|
|
345
|
+
entity_cls = self.entity_directory.get_entity_class(next(iter(type_ids)))
|
|
346
|
+
column = _dependent_column(self.entity_directory.storage(entity_cls), field, entity_cls)
|
|
347
|
+
match = ContainsAny(field, targets) if isinstance(column, JSONField) else InValues(field, targets)
|
|
348
|
+
return await self.list_ids(AllOf((InValues('entity_type_id', type_ids), match)))
|
|
349
|
+
|
|
350
|
+
# --- writes ---------------------------------------------------------
|
|
351
|
+
|
|
352
|
+
async def _ancestors_for(self, parent_id: Optional[str]) -> list:
|
|
353
|
+
if not parent_id:
|
|
354
|
+
return []
|
|
355
|
+
parent = await EntityModel.objects.filter(entity_id=parent_id).values(
|
|
356
|
+
'ancestor_ids'
|
|
357
|
+
).afirst()
|
|
358
|
+
return (list(parent['ancestor_ids'] or []) if parent else []) + [str(parent_id)]
|
|
359
|
+
|
|
360
|
+
async def save(
|
|
361
|
+
self, entity: Entity, precondition: Optional[Precondition] = None, previous=NOT_LOADED,
|
|
362
|
+
) -> SaveReceipt:
|
|
363
|
+
self._validate_storage()
|
|
364
|
+
entity_id = str(entity.entity_id)
|
|
365
|
+
model_cls = self.entity_directory.storage(type(entity))
|
|
366
|
+
|
|
367
|
+
if previous is NOT_LOADED or (previous is not None and previous.version != entity.version):
|
|
368
|
+
row = await model_cls.objects.filter(entity_id=entity_id).values().afirst()
|
|
369
|
+
previous = self._hydrate(row, type(entity), model_cls) if row else None
|
|
370
|
+
|
|
371
|
+
old_parent_id = previous.parent_id if previous is not None else None
|
|
372
|
+
reparented = previous is not None and old_parent_id != entity.parent_id
|
|
373
|
+
if previous is None or reparented:
|
|
374
|
+
ancestor_ids = await self._ancestors_for(entity.parent_id)
|
|
375
|
+
else:
|
|
376
|
+
ancestor_ids = list(previous.ancestor_ids)
|
|
377
|
+
|
|
378
|
+
record = self._split(entity, model_cls)
|
|
379
|
+
moved_descendant_ids = frozenset()
|
|
380
|
+
|
|
381
|
+
if previous is None:
|
|
382
|
+
record['entity_id'] = entity_id
|
|
383
|
+
record['version'] = 1
|
|
384
|
+
record['ancestor_ids'] = ancestor_ids
|
|
385
|
+
instance = model_cls(**record)
|
|
386
|
+
await self._write(precondition, lambda: _insert(instance))
|
|
387
|
+
else:
|
|
388
|
+
if reparented:
|
|
389
|
+
record['ancestor_ids'] = ancestor_ids
|
|
390
|
+
base_columns = _columns(EntityModel)
|
|
391
|
+
base = {k: v for k, v in record.items() if k in base_columns and k != 'version'}
|
|
392
|
+
own = {k: v for k, v in record.items() if k not in base_columns}
|
|
393
|
+
|
|
394
|
+
def update():
|
|
395
|
+
# Base columns ride on the version swap, which is the fence;
|
|
396
|
+
# the typed table is then one UPDATE on its own link column,
|
|
397
|
+
# never Django's select-then-update-each-table.
|
|
398
|
+
if not EntityModel.objects.filter(
|
|
399
|
+
entity_id=entity_id, version=entity.version,
|
|
400
|
+
).update(version=F('version') + 1, **base):
|
|
401
|
+
return 0
|
|
402
|
+
if own:
|
|
403
|
+
concrete = model_cls._meta.concrete_model
|
|
404
|
+
link = concrete._meta.get_ancestor_link(EntityModel)
|
|
405
|
+
if concrete.objects.filter(**{link.attname: entity_id}).update(**own) != 1:
|
|
406
|
+
raise IntegrityError(f"{concrete.__name__} has no row for entity {entity_id}")
|
|
407
|
+
return 1
|
|
408
|
+
|
|
409
|
+
rows_updated = await self._write(precondition, update)
|
|
410
|
+
if rows_updated == 0:
|
|
411
|
+
current = await model_cls.objects.filter(entity_id=entity_id).values(
|
|
412
|
+
'version'
|
|
413
|
+
).afirst()
|
|
414
|
+
raise ConcurrentModificationError(
|
|
415
|
+
f"Entity {entity_id} was modified by another process. "
|
|
416
|
+
f"Version mismatch during save: expected {entity.version}, "
|
|
417
|
+
f"found {current['version'] if current else 'unknown'}",
|
|
418
|
+
entity_id=entity_id,
|
|
419
|
+
expected_version=entity.version,
|
|
420
|
+
found_version=current['version'] if current else 'unknown',
|
|
421
|
+
)
|
|
422
|
+
if reparented:
|
|
423
|
+
moved_descendant_ids = await self._rewrite_subtree(
|
|
424
|
+
entity_id, list(previous.ancestor_ids), ancestor_ids
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
if record['reference_ids'] != (list(previous.reference_ids) if previous is not None else []):
|
|
428
|
+
await self._replace_edges(entity_id)
|
|
429
|
+
|
|
430
|
+
entity.version = 1 if previous is None else entity.version + 1
|
|
431
|
+
self._remember_type(entity_id, entity.entity_type_id)
|
|
432
|
+
entity._ancestor_ids = tuple(ancestor_ids)
|
|
433
|
+
entity._reference_ids = tuple(record['reference_ids'])
|
|
434
|
+
|
|
435
|
+
return SaveReceipt(
|
|
436
|
+
entity=entity,
|
|
437
|
+
previous=previous,
|
|
438
|
+
created=previous is None,
|
|
439
|
+
touched_parent_ids=frozenset(self._touched_parent_ids(previous, entity)),
|
|
440
|
+
moved_descendant_ids=moved_descendant_ids,
|
|
441
|
+
)
|
|
442
|
+
|
|
443
|
+
@sync_to_async
|
|
444
|
+
def _write(self, precondition: Optional[Precondition], write):
|
|
445
|
+
"""`write()` in one transaction with `precondition`'s row locked, so
|
|
446
|
+
the row cannot change between the check and the write."""
|
|
447
|
+
with transaction.atomic():
|
|
448
|
+
if precondition is not None:
|
|
449
|
+
held = EntityModel.objects.select_for_update(no_key=True, of=('self',)).filter(
|
|
450
|
+
entity_id=precondition.entity_id,
|
|
451
|
+
).filter(self.compiler.compile(precondition.where)).values_list('entity_id', flat=True)
|
|
452
|
+
if not list(held[:1]):
|
|
453
|
+
raise PreconditionFailed(
|
|
454
|
+
f"Precondition on {precondition.entity_id} does not hold",
|
|
455
|
+
entity_id=precondition.entity_id,
|
|
456
|
+
)
|
|
457
|
+
return write()
|
|
458
|
+
|
|
459
|
+
@staticmethod
|
|
460
|
+
@sync_to_async
|
|
461
|
+
def _replace_edges(source_id: str) -> None:
|
|
462
|
+
"""Rebuild a source's edges from its committed row, under the row's
|
|
463
|
+
lock: two saves of one entity can interleave here, and the index must
|
|
464
|
+
end at the latest row, not at whichever save wrote last."""
|
|
465
|
+
with transaction.atomic():
|
|
466
|
+
row = EntityModel.objects.select_for_update().filter(entity_id=source_id).values('reference_ids').first()
|
|
467
|
+
EntityReferenceModel.objects.filter(source_id=source_id).delete()
|
|
468
|
+
if row is not None:
|
|
469
|
+
EntityReferenceModel.objects.bulk_create([
|
|
470
|
+
EntityReferenceModel(target_id=target_id, source_id=source_id)
|
|
471
|
+
for target_id in row['reference_ids'] or []
|
|
472
|
+
])
|
|
473
|
+
|
|
474
|
+
async def _rewrite_subtree(self, root_id: str, old_ancestors: list, new_ancestors: list) -> frozenset:
|
|
475
|
+
"""Descendants keep their shape; only the prefix above `root_id`
|
|
476
|
+
changes, and their `version` is deliberately left alone - a move is
|
|
477
|
+
not a write to the things being carried. Returns what it rewrote, so
|
|
478
|
+
the caller can invalidate payloads a version check will not."""
|
|
479
|
+
descendant_ids = await self.descendant_ids(root_id)
|
|
480
|
+
if not descendant_ids:
|
|
481
|
+
return frozenset()
|
|
482
|
+
old_prefix_length = len(old_ancestors) + 1
|
|
483
|
+
new_prefix = new_ancestors + [root_id]
|
|
484
|
+
updates = [
|
|
485
|
+
EntityModel(
|
|
486
|
+
entity_id=row['entity_id'],
|
|
487
|
+
ancestor_ids=new_prefix + list(row['ancestor_ids'] or [])[old_prefix_length:],
|
|
488
|
+
)
|
|
489
|
+
async for row in EntityModel.objects.filter(entity_id__in=descendant_ids).values(
|
|
490
|
+
'entity_id', 'ancestor_ids'
|
|
491
|
+
)
|
|
492
|
+
]
|
|
493
|
+
await EntityModel.objects.abulk_update(updates, ['ancestor_ids'])
|
|
494
|
+
return frozenset(descendant_ids)
|
|
495
|
+
|
|
496
|
+
@staticmethod
|
|
497
|
+
def _touched_parent_ids(previous: Optional[Entity], entity: Entity) -> set:
|
|
498
|
+
"""Which parents' `children_ids` projection this write moved."""
|
|
499
|
+
new_parent_id = entity.parent_id
|
|
500
|
+
if previous is None:
|
|
501
|
+
return {new_parent_id} if new_parent_id else set()
|
|
502
|
+
if previous.parent_id != new_parent_id:
|
|
503
|
+
return {pid for pid in (previous.parent_id, new_parent_id) if pid}
|
|
504
|
+
if new_parent_id and previous.sibling_position != entity.sibling_position:
|
|
505
|
+
return {new_parent_id}
|
|
506
|
+
return set()
|
|
507
|
+
|
|
508
|
+
async def delete_many(self, entity_ids, precondition: Optional[Precondition] = None) -> DeleteReceipt:
|
|
509
|
+
ids = _row_ids(entity_ids)
|
|
510
|
+
deleted = tuple([
|
|
511
|
+
DeletedEntity(
|
|
512
|
+
entity_id=str(row['entity_id']),
|
|
513
|
+
parent_id=str(row['parent_id']) if row.get('parent_id') else None,
|
|
514
|
+
entity_type_id=row.get('entity_type_id'),
|
|
515
|
+
owner=row.get('owner'),
|
|
516
|
+
ancestor_ids=tuple(row.get('ancestor_ids') or ()),
|
|
517
|
+
)
|
|
518
|
+
async for row in EntityModel.objects.filter(entity_id__in=ids).values(
|
|
519
|
+
'entity_id', 'parent_id', 'entity_type_id', 'owner', 'ancestor_ids'
|
|
520
|
+
)
|
|
521
|
+
]) if ids else ()
|
|
522
|
+
await self._write(precondition, lambda: EntityModel.objects.filter(
|
|
523
|
+
entity_id__in=[entity.entity_id for entity in deleted]
|
|
524
|
+
).delete())
|
|
525
|
+
return DeleteReceipt(entities=deleted)
|
|
File without changes
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""The Django repository runs only on databases it is tested against, and
|
|
2
|
+
chooses database-specific SQL by Django's feature flags."""
|
|
3
|
+
from types import SimpleNamespace
|
|
4
|
+
from unittest import mock
|
|
5
|
+
|
|
6
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
7
|
+
from django.test import SimpleTestCase
|
|
8
|
+
|
|
9
|
+
from graphcore.persistence.django import backends
|
|
10
|
+
from graphcore.persistence.django.compiler import QueryCompiler
|
|
11
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
12
|
+
from graphcore.types.query import ContainsAny
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def fake_connection(vendor='postgresql', supports_json_field_contains=True):
|
|
16
|
+
return SimpleNamespace(
|
|
17
|
+
vendor=vendor,
|
|
18
|
+
features=SimpleNamespace(supports_json_field_contains=supports_json_field_contains),
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CheckSupportedTests(SimpleTestCase):
|
|
23
|
+
def test_supported_databases_pass(self):
|
|
24
|
+
for vendor in ('postgresql', 'sqlite'):
|
|
25
|
+
backends.check_supported(fake_connection(vendor))
|
|
26
|
+
|
|
27
|
+
def test_an_unsupported_database_is_refused_by_name(self):
|
|
28
|
+
with self.assertRaisesMessage(ImproperlyConfigured, "'mysql'") as ctx:
|
|
29
|
+
backends.check_supported(fake_connection('mysql'))
|
|
30
|
+
self.assertIn('postgresql, sqlite', str(ctx.exception))
|
|
31
|
+
|
|
32
|
+
def test_the_reference_database_is_supported(self):
|
|
33
|
+
self.assertIn(backends.REFERENCE_VENDOR, backends.SUPPORTED_VENDORS)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SystemCheckTests(SimpleTestCase):
|
|
37
|
+
def _check_with(self, vendor):
|
|
38
|
+
with mock.patch.object(backends, 'connections', {'default': fake_connection(vendor)}):
|
|
39
|
+
return backends.check_database_vendor()
|
|
40
|
+
|
|
41
|
+
def test_supported_database_reports_nothing(self):
|
|
42
|
+
self.assertEqual(self._check_with('sqlite'), [])
|
|
43
|
+
|
|
44
|
+
def test_unsupported_database_is_an_error(self):
|
|
45
|
+
[error] = self._check_with('oracle')
|
|
46
|
+
self.assertEqual(error.id, 'graphcore.E001')
|
|
47
|
+
self.assertIn("'oracle'", error.msg)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class JsonContainmentByFeatureTests(SimpleTestCase):
|
|
51
|
+
"""Both paths are checked on whichever database the suite runs on, by
|
|
52
|
+
inspecting the `Q` the compiler builds rather than executing it."""
|
|
53
|
+
|
|
54
|
+
def _compile(self, supports):
|
|
55
|
+
compiler = QueryCompiler(DEFAULT_REGISTRY.entities)
|
|
56
|
+
target = 'graphcore.persistence.django.compiler.connection'
|
|
57
|
+
with mock.patch(target, fake_connection(supports_json_field_contains=supports)):
|
|
58
|
+
return str(compiler.compile(ContainsAny('ancestor_ids', ['s1', 's2'])))
|
|
59
|
+
|
|
60
|
+
def test_uses_django_contains_where_the_database_supports_it(self):
|
|
61
|
+
q = self._compile(supports=True)
|
|
62
|
+
self.assertIn('ancestor_ids__contains', q)
|
|
63
|
+
self.assertNotIn('json_each', q)
|
|
64
|
+
|
|
65
|
+
def test_falls_back_to_json_each_where_it_does_not(self):
|
|
66
|
+
q = self._compile(supports=False)
|
|
67
|
+
self.assertIn('json_each', q)
|
|
68
|
+
self.assertNotIn('__contains', q)
|