Skip to content

Commit ec912bc

Browse files
committed
Code review updates
1 parent 9309247 commit ec912bc

5 files changed

Lines changed: 43 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ List of the most important changes for each release.
55
## 0.6.11
66
- Added deferred processing of foreign keys to allow bulk processing and to improve performance.
77
- Eliminated extraneous SQL queries for the transfer session when querying for buffers.
8+
- Added database index to Store's partition field to improve querying performance.
89

910
## 0.6.10
1011
- Fixes Django migration issue introduced in 0.6.7 allowing nullable fields with PostgreSQL backends

morango/models/core.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import json
55
import logging
66
import uuid
7-
87
from collections import defaultdict
98
from collections import namedtuple
109
from functools import reduce
@@ -778,7 +777,7 @@ class RecordMaxCounterBuffer(AbstractCounter):
778777
model_uuid = UUIDField(db_index=True)
779778

780779

781-
ForeignKeyReference = namedtuple("ForeignKeyReference", ["from_pk", "to_pk"])
780+
ForeignKeyReference = namedtuple("ForeignKeyReference", ["from_field", "from_pk", "to_pk"])
782781

783782

784783
class SyncableModel(UUIDModelMixin):
@@ -905,8 +904,12 @@ def deferred_clean_fields(self):
905904
if getattr(self, field.attname) is None:
906905
continue
907906
excluded_fields.append(field.name)
908-
deferred_fks[field.related_model.__name__].append(
909-
ForeignKeyReference(from_pk=self.pk, to_pk=getattr(self, field.attname))
907+
deferred_fks[field.related_model._meta.verbose_name].append(
908+
ForeignKeyReference(
909+
from_field=field.attname,
910+
from_pk=self.pk,
911+
to_pk=getattr(self, field.attname)
912+
)
910913
)
911914

912915
self.clean_fields(exclude=excluded_fields)

morango/sync/backends/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def bulk_insert(self, values):
144144

145145
class Meta:
146146
"""
147-
HACK: some Django code bits require a model, only to access meta information, which we use
148-
this to mimic that on this non-model class
147+
HACK: some Django code bits require a model, only to access meta information, so we use
148+
this to mimic a model class
149149
"""
150150

151151
db_tablespace = None

morango/sync/operations.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.db import connections
1212
from django.db import router
1313
from django.db import transaction
14+
from django.db.models import CharField
1415
from django.db.models import Q
1516
from django.db.models import signals
1617
from django.utils import timezone
@@ -278,7 +279,7 @@ def _validate_missing_store_foreign_keys(from_model_name, to_model_name, temp_ta
278279
"""
279280
invalid_pks = []
280281
select_sql = """
281-
SELECT t.from_pk, t.to_pk
282+
SELECT t.from_field, t.from_pk, t.to_pk
282283
FROM {temp_table} t
283284
WHERE NOT EXISTS (
284285
SELECT 1
@@ -297,18 +298,27 @@ def _validate_missing_store_foreign_keys(from_model_name, to_model_name, temp_ta
297298
store_update_fields = [Store._meta.pk, store_deserialization_error]
298299

299300
from_pk_field = temp_table.get_field("from_pk")
301+
to_pk_field = temp_table.get_field("to_pk")
300302
update_values = []
301303
with connection.cursor() as c:
302304
c.execute(select_sql)
303-
for from_pk, to_pk in c.fetchall():
304-
err = "Error deserializing instance of {from_model} with id {from_pk}: missing {to_model} with id {to_pk}".format(
305-
from_model=from_model_name,
306-
from_pk=from_pk,
307-
to_model=to_model_name,
308-
to_pk=to_pk,
305+
for from_field, from_pk, to_pk in c.fetchall():
306+
err = dict(
307+
{
308+
from_field: "{to_model_name} instance with id '{to_pk}' does not exist".format(
309+
to_model_name=to_model_name,
310+
to_pk=to_pk_field.to_python(to_pk),
311+
)
312+
}
313+
)
314+
logger.warning(
315+
"Error deserializing instance of {from_model} with id {from_pk}: {err}".format(
316+
from_model=from_model_name,
317+
from_pk=from_pk,
318+
err=str(err),
319+
)
309320
)
310-
logger.warning(err)
311-
update_values.extend([from_pk, err])
321+
update_values.extend([from_pk, str(err)])
312322
invalid_pks.append(from_pk_field.to_python(from_pk))
313323
if update_values:
314324
# update Store with errors
@@ -384,7 +394,11 @@ def _validate_store_foreign_keys(from_model_name, fk_references):
384394

385395
for to_model_name, to_fk_references in fk_references.items():
386396
with TemporaryTable(
387-
connection, "fks", from_pk=UUIDField(), to_pk=UUIDField()
397+
connection,
398+
"fks",
399+
from_field=CharField(max_length=255),
400+
from_pk=UUIDField(),
401+
to_pk=UUIDField(),
388402
) as temp_table:
389403
# insert all the FK references into a temp table in the database
390404
temp_table.bulk_insert([fks._asdict() for fks in to_fk_references])
@@ -517,7 +531,11 @@ def _deserialize_from_store(profile, skip_erroring=False, filter=None):
517531
# deserialize for reasons other than broken FKs at this point
518532
for fk_ref in fk_refs:
519533
if fk_ref.to_pk in excluded_list:
520-
raise exceptions.ValidationError("{} with id {} failed to deserialize".format(fk_model, fk_ref.to_pk))
534+
raise exceptions.ValidationError(
535+
"{} with id {} failed to deserialize".format(
536+
fk_model, fk_ref.to_pk
537+
)
538+
)
521539
deferred_fks[fk_model].extend(fk_refs)
522540
except (
523541
exceptions.ValidationError,

tests/testapp/tests/sync/test_controller.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
import contextlib
12
import json
23
import uuid
3-
import contextlib
4+
from test.support import EnvironmentVarGuard
45

56
import factory
67
import mock
78
from django.test import SimpleTestCase
89
from django.test import TestCase
910
from facility_profile.models import Facility
10-
from facility_profile.models import MyUser
1111
from facility_profile.models import InteractionLog
12+
from facility_profile.models import MyUser
1213
from facility_profile.models import SummaryLog
13-
from test.support import EnvironmentVarGuard
1414

1515
from ..helpers import serialized_facility_factory
1616
from ..helpers import TestSessionContext
@@ -641,7 +641,7 @@ def test_deserialization_of_model_with_missing_foreignkey_referent(self):
641641

642642
new_log.refresh_from_db()
643643
self.assertTrue(new_log.dirty_bit)
644-
self.assertIn("missing MyUser", new_log.deserialization_error)
644+
self.assertIn("my user instance with id '{}'".format(data["user_id"]), new_log.deserialization_error)
645645

646646
def test_deserialization_of_model_with_disallowed_null_foreignkey(self):
647647

0 commit comments

Comments
 (0)