Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exception in case of missing ds table #34595

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions corehq/apps/userreports/sql/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,13 @@ def bulk_delete(self, docs, use_shard_col=True):
table = self.get_table()
doc_ids = [doc['_id'] for doc in docs]
delete = table.delete(table.c.doc_id.in_(doc_ids))
with self.session_context() as session:
session.execute(delete)
try:
with self.session_context() as session:
session.execute(delete)
except ProgrammingError as e:
if not self.table_exists:
return
raise e
Comment on lines +214 to +220
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really worth the hassle of changing, but maybe just interesting to know about: You can use raise to raise the last exception, and so you don't need a variable for it in this situation.

Suggested change
try:
with self.session_context() as session:
session.execute(delete)
except ProgrammingError as e:
if not self.table_exists:
return
raise e
try:
with self.session_context() as session:
session.execute(delete)
except ProgrammingError:
if not self.table_exists:
return
raise

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I forgot about that 👍


register_data_source_row_change(
domain=self.config.domain,
Expand Down
52 changes: 52 additions & 0 deletions corehq/apps/userreports/tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.test import TestCase
from unittest.mock import patch

from corehq.apps.userreports.sql.adapter import IndicatorSqlAdapter
from corehq.apps.userreports.models import DataSourceConfiguration
from corehq.apps.userreports.app_manager.helpers import clean_table_name


class TestIndicatorSqlAdapter(TestCase):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkangia Oh my goodness! Thank you!


@staticmethod
def _create_data_source_config(domain):
indicator = {
"type": "expression",
"expression": {
"type": "property_name",
"property_name": 'name'
},
"column_id": 'name',
"display_name": 'name',
"datatype": "string"
}
return DataSourceConfiguration(
domain=domain,
display_name='foo',
referenced_doc_type='CommCareCase',
table_id=clean_table_name('domain', 'test-table'),
configured_indicators=[indicator],
)

@patch("corehq.apps.userreports.sql.adapter.register_data_source_row_change")
def test_bulk_delete_table_dont_exist(self, register_data_source_row_change_mock):
config = self._create_data_source_config("test-domain")
adapter = IndicatorSqlAdapter(config)

docs = [{'_id': '1'}]
self.assertFalse(adapter.table_exists)

adapter.bulk_delete(docs)
register_data_source_row_change_mock.assert_not_called()

@patch("corehq.apps.userreports.sql.adapter.register_data_source_row_change")
def test_bulk_delete_table_exists(self, register_data_source_row_change_mock):
config = self._create_data_source_config("test-domain2")
adapter = IndicatorSqlAdapter(config)

docs = [{'_id': '1'}]
adapter.build_table()
self.assertTrue(adapter.table_exists)

adapter.bulk_delete(docs)
register_data_source_row_change_mock.assert_called()