Skip to content

Commit

Permalink
add read only fields to memory, clean record (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyn4 authored Sep 17, 2024
1 parent dbf5e5e commit 9a56fcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion target_salesforce_v3/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,10 @@ def preprocess_record(self, record, context):
for key in record:
if isinstance(record.get(key), datetime):
record[key] = record[key].isoformat()

# clean any read only fields
for field in self._target.read_only_fields.get(self.stream_name, []):
record.pop(field, None)
return record

def upsert_record(self, record, context):
Expand Down Expand Up @@ -1008,8 +1012,17 @@ def upsert_record(self, record, context):
return id, True, state_updates
except Exception as e:
if "INVALID_FIELD_FOR_INSERT_UPDATE" in str(e):
fields = json.loads(str(e))[0]['fields']
try:
fields = json.loads(str(e))[0]['fields']
except:
raise Exception(f"Attempted to write read-only fields. Unable to extract read-only fields to retry request: {str(e)}")

self.logger.warning(f"Attempted to write read-only fields: {fields}. Removing them and retrying.")
# append read-only field to a list
if not self._target.read_only_fields.get(self.stream_name):
self._target.read_only_fields[self.stream_name] = []
self._target.read_only_fields[self.stream_name].extend(fields)
# remove read-only fields from record
for f in fields:
record.pop(f, None)
# retry
Expand Down
2 changes: 2 additions & 0 deletions target_salesforce_v3/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TargetSalesforceV3(TargetHotglue):
name = "target-salesforce-v3"
MAX_PARALLELISM = 10
SINK_TYPES = SINK_TYPES
read_only_fields = {}

def get_sink_class(self, stream_name: str):
"""Get sink for a stream."""
for sink_class in SINK_TYPES:
Expand Down

0 comments on commit 9a56fcb

Please sign in to comment.