From 02f84488e335dc013ae6e1e4f89400ec1dc2a77a Mon Sep 17 00:00:00 2001 From: Paul Prescod Date: Fri, 30 Apr 2021 11:34:18 -0700 Subject: [PATCH 1/3] Make set_recently_viewed more robust. --- HISTORY.rst | 8 +++ cumulusci/tasks/bulkdata/load.py | 38 ++++++++++----- cumulusci/tasks/bulkdata/tests/test_load.py | 54 +++++++++++++++++++++ cumulusci/version.txt | 2 +- 4 files changed, 90 insertions(+), 12 deletions(-) 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 From dbb19d19149a8440a6337ceb36b4b6e4a92d72ea Mon Sep 17 00:00:00 2001 From: prescod Date: Fri, 30 Apr 2021 12:29:58 -0700 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: David Glick --- HISTORY.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c6a8deb721..a70783ec3a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,13 +2,12 @@ History ======= -3.34.1 (2021-04-29) +3.34.1 (2021-04-30) ------------------- Issues Closed -Some SObjects could not be loaded without explicitly turning off the new -``set_recently_viewed`` behavior. +- 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) ------------------- From 4f5ff9bd3ea5f34e0cbad1cdc4f34c77368a2b21 Mon Sep 17 00:00:00 2001 From: Paul Prescod Date: Fri, 30 Apr 2021 12:33:28 -0700 Subject: [PATCH 3/3] Clearer errors. --- cumulusci/tasks/bulkdata/load.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cumulusci/tasks/bulkdata/load.py b/cumulusci/tasks/bulkdata/load.py index 871d3785f6..ff1d2524fc 100644 --- a/cumulusci/tasks/bulkdata/load.py +++ b/cumulusci/tasks/bulkdata/load.py @@ -666,9 +666,9 @@ def _set_viewed(self): ) for record in custom_tab_objects["records"]: object_names.add(record["SobjectName"]) - except Exception: + except Exception as e: self.logger.warning( - "Cannot get the list of custom tabs to set recently viewed status on them." + 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): @@ -677,7 +677,7 @@ def _set_viewed(self): self.sf.query_all( f"SELECT Id FROM {mapped_item} ORDER BY CreatedDate DESC LIMIT 1000 FOR VIEW" ) - except Exception: + except Exception as e: self.logger.warning( - f"Cannot set recently viewed status for {mapped_item}" + f"Cannot set recently viewed status for {mapped_item}. Error: {e}" )