Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ dmypy.json

# Editor
.vscode/
.idea/

### macOS ###
# General
Expand Down Expand Up @@ -308,3 +309,4 @@ docs/CHANGELOG.md
public
/compose.yaml
/dump.sql

1 change: 1 addition & 0 deletions changes/379.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Moved `log_sync_status` calls until after object creation.
7 changes: 6 additions & 1 deletion diffsync/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ def sync_diff_element(self, element: DiffElement, parent_model: Optional["DiffSy
if parent_model:
parent_model.add_child(dst_model)
self.dst_diffsync.add(dst_model)
# Log after insertion so processors can resolve the newly created object.
status, message = dst_model.get_status()
self.log_sync_status(self.action, status, message)
elif self.action == DiffSyncActions.DELETE:
if parent_model:
parent_model.remove_child(dst_model)
Expand Down Expand Up @@ -453,7 +456,9 @@ def sync_model( # pylint: disable=too-many-branches, unused-argument
return (True, None)
raise

self.log_sync_status(self.action, status, message)
# A returned create model is logged by sync_diff_element() after store insertion.
if self.action != DiffSyncActions.CREATE or not dst_model:
self.log_sync_status(self.action, status, message)

return (True, dst_model)

Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_create_log_ordering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Regression test for resolving a created object while its result is logged."""

import structlog

from diffsync import Adapter, DiffSyncModel


class Record(DiffSyncModel):
_modelname = "record"
_identifiers = ("name",)

name: str


class RecordAdapter(Adapter):
record = Record
top_level = ["record"]


def test_create_log_can_resolve_model():
source, target = RecordAdapter(), RecordAdapter()
source.add(Record(name="new"))
logged_objects = []

def processor(_logger, _method, event):
if event.get("action") == "create" and event.get("status") == "success":
logged_objects.append(event["dst"].get(event["model"], event["unique_id"]))
return event

processors = structlog.get_config()["processors"]
processors.insert(0, processor)
try:
source.sync_to(target)
finally:
processors.remove(processor)

assert logged_objects == [target.get("record", "new")]
Loading