Skip to content

Commit 138241e

Browse files
Functionality for Select+Insert
1 parent 730ba6c commit 138241e

12 files changed

Lines changed: 960 additions & 166 deletions

cumulusci/tasks/bulkdata/load.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,14 @@ def _execute_step(
312312

313313
def process_lookup_fields(self, mapping, fields, polymorphic_fields):
314314
"""Modify fields and priority fields based on lookup and polymorphic checks."""
315+
# Store the lookups and their original order for re-insertion at the end
316+
original_lookups = [name for name in fields if name in mapping.lookups]
317+
max_insert_index = -1
315318
for name, lookup in mapping.lookups.items():
316319
if name in fields:
317320
# Get the index of the lookup field before removing it
318321
insert_index = fields.index(name)
322+
max_insert_index = max(max_insert_index, insert_index)
319323
# Remove the lookup field from fields
320324
fields.remove(name)
321325

@@ -351,14 +355,15 @@ def process_lookup_fields(self, mapping, fields, polymorphic_fields):
351355
None,
352356
)
353357
if lookup_mapping_step:
354-
lookup_fields = lookup_mapping_step.get_load_field_list()
358+
lookup_fields = lookup_mapping_step.fields.keys()
355359
# Insert fields in the format {relationship_name}.{ref_type}.{lookup_field}
356360
for field in lookup_fields:
357361
fields.insert(
358362
insert_index,
359363
f"{relationship_name}.{lookup_mapping_step.sf_object}.{field}",
360364
)
361365
insert_index += 1
366+
max_insert_index = max(max_insert_index, insert_index)
362367
if lookup_in_priority_fields:
363368
mapping.select_options.priority_fields[
364369
f"{relationship_name}.{lookup_mapping_step.sf_object}.{field}"
@@ -383,17 +388,24 @@ def process_lookup_fields(self, mapping, fields, polymorphic_fields):
383388

384389
if lookup_mapping_step:
385390
relationship_name = polymorphic_fields[name]["relationshipName"]
386-
lookup_fields = lookup_mapping_step.get_load_field_list()
391+
lookup_fields = lookup_mapping_step.fields.keys()
387392

388393
# Insert the new fields at the same position as the removed lookup field
389394
for field in lookup_fields:
390395
fields.insert(insert_index, f"{relationship_name}.{field}")
391396
insert_index += 1
397+
max_insert_index = max(max_insert_index, insert_index)
392398
if lookup_in_priority_fields:
393399
mapping.select_options.priority_fields[
394400
f"{relationship_name}.{field}"
395401
] = f"{relationship_name}.{field}"
396402

403+
# Append the original lookups at the end in the same order
404+
for name in original_lookups:
405+
if name not in fields:
406+
fields.insert(max_insert_index, name)
407+
max_insert_index += 1
408+
397409
def configure_step(self, mapping):
398410
"""Create a step appropriate to the action"""
399411
bulk_mode = mapping.bulk_mode or self.bulk_mode or "Parallel"
@@ -479,6 +491,7 @@ def configure_step(self, mapping):
479491
selection_filter=mapping.select_options.filter,
480492
selection_priority_fields=mapping.select_options.priority_fields,
481493
content_type=content_type,
494+
threshold=mapping.select_options.threshold,
482495
)
483496
return step, query
484497

@@ -588,10 +601,9 @@ def _query_db(self, mapping):
588601
mapping, self.mapping, self.metadata, model, self._old_format
589602
)
590603
)
591-
else:
592-
transformers.append(
593-
AddLookupsToQuery(mapping, self.metadata, model, self._old_format)
594-
)
604+
transformers.append(
605+
AddLookupsToQuery(mapping, self.metadata, model, self._old_format)
606+
)
595607

596608
transformers.extend([cls(mapping, self.metadata, model) for cls in classes])
597609

cumulusci/tasks/bulkdata/mapping_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class MappingLookup(CCIDictModel):
3131
join_field: Optional[str] = None
3232
after: Optional[str] = None
3333
aliased_table: Optional[Any] = None
34+
parent_tables: Optional[Any] = None
3435
name: Optional[str] = None # populated by parent
3536

3637
def get_lookup_key_field(self, model=None):

cumulusci/tasks/bulkdata/query_transformers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ def columns_to_add(self):
106106
columns = []
107107
for lookup in self.lookups:
108108
tables = lookup.table if isinstance(lookup.table, list) else [lookup.table]
109-
lookup.aliased_table = [
109+
lookup.parent_tables = [
110110
aliased(
111111
self.metadata.tables[table], name=f"{lookup.name}_{table}_alias"
112112
)
113113
for table in tables
114114
]
115115

116-
for aliased_table, table_name in zip(lookup.aliased_table, tables):
116+
for parent_table, table_name in zip(lookup.parent_tables, tables):
117117
# Find the mapping step for this polymorphic type
118118
lookup_mapping_step = next(
119119
(
@@ -124,24 +124,24 @@ def columns_to_add(self):
124124
None,
125125
)
126126
if lookup_mapping_step:
127-
load_fields = lookup_mapping_step.get_load_field_list()
127+
load_fields = lookup_mapping_step.fields.keys()
128128
for field in load_fields:
129129
if field in lookup_mapping_step.fields:
130130
matching_column = next(
131131
(
132132
col
133-
for col in aliased_table.columns
133+
for col in parent_table.columns
134134
if col.name == lookup_mapping_step.fields[field]
135135
)
136136
)
137137
columns.append(
138-
matching_column.label(f"{aliased_table.name}_{field}")
138+
matching_column.label(f"{parent_table.name}_{field}")
139139
)
140140
else:
141141
# Append an empty string if the field is not present
142142
columns.append(
143143
literal_column("''").label(
144-
f"{aliased_table.name}_{field}"
144+
f"{parent_table.name}_{field}"
145145
)
146146
)
147147
return columns
@@ -150,15 +150,15 @@ def columns_to_add(self):
150150
def outerjoins_to_add(self):
151151
"""Add outer joins for each lookup table directly, including handling for polymorphic lookups."""
152152

153-
def join_for_lookup(lookup, aliased_table):
153+
def join_for_lookup(lookup, parent_table):
154154
key_field = lookup.get_lookup_key_field(self.model)
155155
value_column = getattr(self.model, key_field)
156-
return (aliased_table, aliased_table.columns.id == value_column)
156+
return (parent_table, parent_table.columns.id == value_column)
157157

158158
joins = []
159159
for lookup in self.lookups:
160-
for aliased_table in lookup.aliased_table:
161-
joins.append(join_for_lookup(lookup, aliased_table))
160+
for parent_table in lookup.parent_tables:
161+
joins.append(join_for_lookup(lookup, parent_table))
162162
return joins
163163

164164

0 commit comments

Comments
 (0)