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

refactor: sql_json view endpoint: separate save query concern #16646

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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