diff --git a/HISTORY.rst b/HISTORY.rst index fb636e317f..c6a8deb721 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,14 @@ History ======= +3.34.1 (2021-04-29) +------------------- + +Issues Closed + +Some SObjects could not be loaded without explicitly turning off the new +``set_recently_viewed`` behavior. + 3.34.0 (2021-04-29) ------------------- diff --git a/cumulusci/tasks/bulkdata/load.py b/cumulusci/tasks/bulkdata/load.py index 38658fb890..871d3785f6 100644 --- a/cumulusci/tasks/bulkdata/load.py +++ b/cumulusci/tasks/bulkdata/load.py @@ -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 @@ -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 @@ -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: + self.logger.warning( + "Cannot get the list of custom tabs to set recently viewed status on them." + ) + 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: + self.logger.warning( + f"Cannot set recently viewed status for {mapped_item}" + ) diff --git a/cumulusci/tasks/bulkdata/tests/test_load.py b/cumulusci/tasks/bulkdata/tests/test_load.py index 1fc4b4349b..f789dc50de 100644 --- a/cumulusci/tasks/bulkdata/tests/test_load.py +++ b/cumulusci/tasks/bulkdata/tests/test_load.py @@ -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( @@ -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]) diff --git a/cumulusci/version.txt b/cumulusci/version.txt index 5c0e053417..62d2864662 100644 --- a/cumulusci/version.txt +++ b/cumulusci/version.txt @@ -1 +1 @@ -3.34.0 +3.34.1