Skip to content

Commit

Permalink
Merge pull request #2594 from SFDO-Tooling/feature/more-robust-recent…
Browse files Browse the repository at this point in the history
…ly-viewed

Make set_recently_viewed more robust.
  • Loading branch information
prescod authored Apr 30, 2021
2 parents 0867912 + 4f5ff9b commit 45bb088
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
History
=======

3.34.1 (2021-04-30)
-------------------

Issues Closed

- Fixed a regression in the ``load_dataset`` task where some sObjects could not be loaded without explicitly turning off the new ``set_recently_viewed`` option.

3.34.0 (2021-04-29)
-------------------

Expand Down
38 changes: 27 additions & 11 deletions cumulusci/tasks/bulkdata/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Union
import tempfile
from contextlib import contextmanager
from cumulusci.salesforce_api.org_schema import get_org_schema

from sqlalchemy import Column, MetaData, Table, Unicode, create_engine, text, func
from sqlalchemy.orm import aliased, Session
Expand Down Expand Up @@ -139,7 +140,10 @@ def _run_task(self):
f"Step {after_name} did not complete successfully: {','.join(result.job_errors)}"
)
if self.options["set_recently_viewed"]:
self._set_viewed()
try:
self._set_viewed()
except Exception as e:
self.logger.warning(f"Could not set recently viewed because {e}")

def _execute_step(
self, mapping: MappingStep
Expand Down Expand Up @@ -654,14 +658,26 @@ def _set_viewed(self):
object_names.add(object_name)
# collect SobjectName that have custom tabs
if custom_objects:
for record in self.sf.query_all(
"SELECT SObjectName FROM TabDefinition WHERE IsCustom = true AND SObjectName IN ('{}')".format(
"','".join(sorted(custom_objects))
try:
custom_tab_objects = self.sf.query_all(
"SELECT SObjectName FROM TabDefinition WHERE IsCustom = true AND SObjectName IN ('{}')".format(
"','".join(sorted(custom_objects))
)
)
)["records"]:
object_names.add(record["SobjectName"])

for mapped_item in sorted(object_names):
self.sf.query_all(
f"SELECT Id FROM {mapped_item} ORDER BY CreatedDate DESC LIMIT 1000 FOR VIEW"
)
for record in custom_tab_objects["records"]:
object_names.add(record["SobjectName"])
except Exception as e:
self.logger.warning(
f"Cannot get the list of custom tabs to set recently viewed status on them. Error: {e}"
)
with get_org_schema(self.sf, self.org_config) as org_schema:
for mapped_item in sorted(object_names):
if org_schema[mapped_item].mruEnabled:
try:
self.sf.query_all(
f"SELECT Id FROM {mapped_item} ORDER BY CreatedDate DESC LIMIT 1000 FOR VIEW"
)
except Exception as e:
self.logger.warning(
f"Cannot set recently viewed status for {mapped_item}. Error: {e}"
)
54 changes: 54 additions & 0 deletions cumulusci/tasks/bulkdata/tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,7 @@ def _job_state_from_batches(self, job_id):
):
task()

@mock.patch("cumulusci.tasks.bulkdata.load.get_org_schema", mock.MagicMock())
def test_set_viewed(self):
base_path = os.path.dirname(__file__)
task = _make_task(
Expand Down Expand Up @@ -2313,3 +2314,56 @@ def _query_all(query):
"SELECT Id FROM Account ORDER BY CreatedDate DESC LIMIT 1000 FOR VIEW",
"SELECT Id FROM Custom__c ORDER BY CreatedDate DESC LIMIT 1000 FOR VIEW",
], queries

@mock.patch("cumulusci.tasks.bulkdata.load.get_org_schema", mock.MagicMock())
def test_set_viewed__SOQL_error_1(self):
base_path = os.path.dirname(__file__)
task = _make_task(
LoadData,
{
"options": {
"sql_path": "test.sql",
"mapping": os.path.join(base_path, self.mapping_file),
}
},
)

def _query_all(query):
assert 0

task.sf = mock.Mock()
task.sf.query_all = _query_all
task.mapping = {}
task.mapping["Insert Households"] = MappingStep(sf_object="Account", fields={})
task.mapping["Insert Custom__c"] = MappingStep(sf_object="Custom__c", fields={})

with mock.patch.object(task.logger, "warning") as warning:
task._set_viewed()

assert "custom tabs" in str(warning.mock_calls[0])
assert "Account" in str(warning.mock_calls[1])

def test_set_viewed__exception(self):
task = _make_task(
LoadData,
{
"options": {
"database_url": "sqlite://",
"mapping": "mapping.yml",
"set_recently_viewed": True,
}
},
)
task._init_db = mock.Mock(return_value=nullcontext())
task._init_mapping = mock.Mock()
task.mapping = {}
task.after_steps = {}

def raise_exception():
assert 0, "xyzzy"

task._set_viewed = raise_exception

with mock.patch.object(task.logger, "warning") as warning:
task()
assert "xyzzy" in str(warning.mock_calls[0])
2 changes: 1 addition & 1 deletion cumulusci/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.34.0
3.34.1

0 comments on commit 45bb088

Please sign in to comment.