Skip to content

Commit

Permalink
refactor sql_json view endpoint: separate save query concern into ad …
Browse files Browse the repository at this point in the history
…hod method
  • Loading branch information
ofekisr committed Sep 9, 2021
1 parent 6465ee7 commit c569a3f
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2595,24 +2595,9 @@ def _run_sql_json_exec_from_scratch(
self._get_the_query_db(execution_context, session)
)
query = execution_context.create_query()
try:
session.add(query)
session.flush()
query_id = query.id
session.commit() # shouldn't be necessary
except SQLAlchemyError as ex:
logger.error("Errors saving query details %s", str(ex), exc_info=True)
session.rollback()
query_id = None
if not query_id:
raise SupersetGenericErrorException(
__(
"The query record was not created as expected. Please "
"contact an administrator for further assistance or try again."
)
)
self._save_new_query(query, session)

logger.info("Triggering query_id: %i", query_id)
logger.info("Triggering query_id: %i", query.id)

try:
query.raise_for_access()
Expand Down Expand Up @@ -2692,6 +2677,24 @@ def _run_sql_json_exec_from_scratch(
session, rendered_query, query, expand_data, log_params
)

def _save_new_query( # pylint: disable=no-self-use
self, query: Query, session: Session
) -> None:
try:
session.add(query)
session.flush()
session.commit() # shouldn't be necessary
except SQLAlchemyError as ex:
logger.error("Errors saving query details %s", str(ex), exc_info=True)
session.rollback()
if not query.id:
raise SupersetGenericErrorException(
__(
"The query record was not created as expected. Please "
"contact an administrator for further assistance or try again."
)
)

@staticmethod
def _convert_query_to_payload(query: Query) -> str:
return json.dumps(
Expand Down

0 comments on commit c569a3f

Please sign in to comment.