Skip to content

Commit 9264674

Browse files
committed
Exclude models when FKs have already been excluded
1 parent 4b2e643 commit 9264674

2 files changed

Lines changed: 29 additions & 18 deletions

File tree

morango/sync/operations.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ def _deserialize_from_store(profile, skip_erroring=False, filter=None):
512512
if app_model:
513513
app_models.append(app_model)
514514
for fk_model, fk_refs in model_deferred_fks.items():
515+
# validate that the FK references aren't to anything already in the
516+
# excluded list, which should only contain models which failed to
517+
# deserialize for reasons other than broken FKs at this point
518+
for fk_ref in fk_refs:
519+
if fk_ref.to_pk in excluded_list:
520+
raise exceptions.ValidationError("{} with id {} failed to deserialize".format(fk_model, fk_ref.to_pk))
515521
deferred_fks[fk_model].extend(fk_refs)
516522
except (
517523
exceptions.ValidationError,
@@ -533,17 +539,23 @@ def _deserialize_from_store(profile, skip_erroring=False, filter=None):
533539
# array for holding db values from the fields of each model for this class
534540
db_values = []
535541
for app_model in app_models:
536-
print("CHECK EXCLUDE", app_model.pk, excluded_list)
537542
if (
538543
app_model.pk not in excluded_list
539544
and app_model.pk not in deleted_list
540545
):
541-
new_db_values = []
542-
for f in fields:
543-
value = getattr(app_model, f.attname)
544-
db_value = f.get_db_prep_value(value, connection)
545-
new_db_values.append(db_value)
546-
db_values += new_db_values
546+
# handle any errors that might come from `get_db_prep_value`
547+
try:
548+
new_db_values = []
549+
for f in fields:
550+
value = getattr(app_model, f.attname)
551+
db_value = f.get_db_prep_value(value, connection)
552+
new_db_values.append(db_value)
553+
db_values += new_db_values
554+
except ValueError as e:
555+
excluded_list.append(app_model.pk)
556+
store_model = store_models.get(pk=app_model.pk)
557+
store_model.deserialization_error = str(e)
558+
store_model.save(update_fields=["deserialization_error"])
547559

548560
if db_values:
549561
with connection.cursor() as cursor:

tests/testapp/tests/sync/test_operations.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
import uuid
33

44
import factory
5+
import mock
56
from django.db import connection
6-
from django.test import TestCase
77
from django.test import override_settings
8+
from django.test import TestCase
89
from django.utils import timezone
910
from facility_profile.models import Facility
1011
from facility_profile.models import MyUser
1112
from facility_profile.models import SummaryLog
12-
import mock
13-
import pytest
1413

1514
from ..helpers import create_buffer_and_store_dummy_data
1615
from ..helpers import create_dummy_store_data
@@ -19,12 +18,12 @@
1918
from morango.errors import MorangoLimitExceeded
2019
from morango.models.core import Buffer
2120
from morango.models.core import DatabaseIDModel
21+
from morango.models.core import DatabaseMaxCounter
2222
from morango.models.core import InstanceIDModel
2323
from morango.models.core import RecordMaxCounter
2424
from morango.models.core import RecordMaxCounterBuffer
2525
from morango.models.core import Store
2626
from morango.models.core import SyncSession
27-
from morango.models.core import DatabaseMaxCounter
2827
from morango.models.core import TransferSession
2928
from morango.sync.backends.utils import load_backend
3029
from morango.sync.context import LocalSessionContext
@@ -35,11 +34,11 @@
3534
from morango.sync.operations import _queue_into_buffer_v1
3635
from morango.sync.operations import _queue_into_buffer_v2
3736
from morango.sync.operations import CleanupOperation
38-
from morango.sync.operations import ReceiverDequeueOperation
39-
from morango.sync.operations import ProducerDequeueOperation
40-
from morango.sync.operations import ReceiverDeserializeOperation
4137
from morango.sync.operations import InitializeOperation
38+
from morango.sync.operations import ProducerDequeueOperation
4239
from morango.sync.operations import ProducerQueueOperation
40+
from morango.sync.operations import ReceiverDequeueOperation
41+
from morango.sync.operations import ReceiverDeserializeOperation
4342
from morango.sync.operations import ReceiverQueueOperation
4443
from morango.sync.syncsession import TransferClient
4544

@@ -1101,25 +1100,25 @@ def test_successful_deserialization(self):
11011100
self.assert_deserialization()
11021101

11031102
def test_deserialization_with_missing_username(self):
1104-
1103+
11051104
self.serialized_user["username"] = ""
11061105

11071106
self.serialize_all_to_store()
11081107

11091108
_deserialize_from_store(self.profile)
11101109

11111110
self.assert_deserialization(user_deserialized=False, log1_deserialized=False, log2_deserialized=False)
1112-
1111+
11131112
def test_deserialization_with_excessively_long_username(self):
1114-
1113+
11151114
self.serialized_user["username"] = "a" * 256
11161115

11171116
self.serialize_all_to_store()
11181117

11191118
_deserialize_from_store(self.profile)
11201119

11211120
self.assert_deserialization(user_deserialized=False, log1_deserialized=False, log2_deserialized=False)
1122-
1121+
11231122
def test_deserialization_with_invalid_content_id(self):
11241123

11251124
self.serialized_log1["content_id"] = "invalid"

0 commit comments

Comments
 (0)