Skip to content

Commit

Permalink
check if query exists before saving a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed Jun 17, 2021
1 parent 70afa08 commit 65aa038
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
30 changes: 25 additions & 5 deletions superset/views/core.py
Expand Up @@ -2477,14 +2477,34 @@ def sql_json_exec( # pylint: disable=too-many-statements,too-many-locals
CtasMethod, query_params.get("ctas_method", CtasMethod.TABLE)
)
tmp_table_name: str = cast(str, query_params.get("tmp_table_name"))
client_id: str = cast(
str, query_params.get("client_id") or utils.shortid()[:10]
)
client_id: str = cast(str, query_params.get("client_id"))
client_id_or_short_id: str = cast(str, client_id or utils.shortid()[:10])
sql_editor_id: str = cast(str, query_params.get("sql_editor_id"))
tab_name: str = cast(str, query_params.get("tab"))
status: str = QueryStatus.PENDING if async_flag else QueryStatus.RUNNING
user_id: int = g.user.get_id() if g.user else None

session = db.session()

# check to see if this query is already running
query = (
session.query(Query)
.filter_by(
client_id=client_id, user_id=user_id, sql_editor_id=sql_editor_id
)
.one_or_none()
)
if query is not None and query.status in [
QueryStatus.RUNNING,
QueryStatus.PENDING,
QueryStatus.TIMED_OUT,
]:
# return the existing query
payload = json.dumps(
{"query": query.to_dict()}, default=utils.json_int_dttm_ser
)
return json_success(payload)

mydb = session.query(Database).get(database_id)
if not mydb:
return json_error_response("Database with id %i is missing.", database_id)
Expand Down Expand Up @@ -2512,8 +2532,8 @@ def sql_json_exec( # pylint: disable=too-many-statements,too-many-locals
sql_editor_id=sql_editor_id,
tmp_table_name=tmp_table_name,
tmp_schema_name=tmp_schema_name,
user_id=g.user.get_id() if g.user else None,
client_id=client_id,
user_id=user_id,
client_id=client_id_or_short_id,
)
try:
session.add(query)
Expand Down
6 changes: 4 additions & 2 deletions superset/views/sql_lab.py
Expand Up @@ -228,10 +228,12 @@ def migrate_query( # pylint: disable=no-self-use
@has_access_api
@expose("<int:tab_state_id>/query/<client_id>", methods=["DELETE"])
def delete_query( # pylint: disable=no-self-use
self, tab_state_id: str, client_id: str
self, tab_state_id: int, client_id: str
) -> FlaskResponse:
db.session.query(Query).filter_by(
client_id=client_id, user_id=g.user.get_id(), sql_editor_id=tab_state_id
client_id=client_id,
user_id=g.user.get_id(),
sql_editor_id=str(tab_state_id),
).delete(synchronize_session=False)
db.session.commit()
return json_success(json.dumps("OK"))
Expand Down

0 comments on commit 65aa038

Please sign in to comment.