diff --git a/superset/views/core.py b/superset/views/core.py index 0d7ea130154f..03ae820baf47 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -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) @@ -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) diff --git a/superset/views/sql_lab.py b/superset/views/sql_lab.py index ef3d901d4dec..e3a09e66a5c1 100644 --- a/superset/views/sql_lab.py +++ b/superset/views/sql_lab.py @@ -228,10 +228,12 @@ def migrate_query( # pylint: disable=no-self-use @has_access_api @expose("/query/", 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"))