Skip to content

Commit ec3bed7

Browse files
Khrolmichael-s-molina
authored andcommitted
fix: avoid 500 errors with SQLLAB_BACKEND_PERSISTENCE (#25553)
(cherry picked from commit 99f79f5)
1 parent 701ee30 commit ec3bed7

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

superset/views/sql_lab/views.py

+4
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ def put(self, tab_state_id: int) -> FlaskResponse: # pylint: disable=no-self-us
219219
return Response(status=403)
220220

221221
fields = {k: json.loads(v) for k, v in request.form.to_dict().items()}
222+
if client_id := fields.get("latest_query_id"):
223+
query = db.session.query(Query).filter_by(client_id=client_id).one_or_none()
224+
if not query:
225+
return self.json_response({"error": "Bad request"}, status=400)
222226
db.session.query(TabState).filter_by(id=tab_state_id).update(fields)
223227
db.session.commit()
224228
return json_success(json.dumps(tab_state_id))

tests/integration_tests/core_tests.py

+35
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,41 @@ def test_tabstate_with_name(self):
11971197

11981198
self.assertEqual(payload["label"], "Untitled Query foo")
11991199

1200+
def test_tabstate_update(self):
1201+
username = "admin"
1202+
self.login(username)
1203+
# create a tab
1204+
data = {
1205+
"queryEditor": json.dumps(
1206+
{
1207+
"name": "Untitled Query foo",
1208+
"dbId": 1,
1209+
"schema": None,
1210+
"autorun": False,
1211+
"sql": "SELECT ...",
1212+
"queryLimit": 1000,
1213+
}
1214+
)
1215+
}
1216+
resp = self.get_json_resp("/tabstateview/", data=data)
1217+
tab_state_id = resp["id"]
1218+
# update tab state with non-existing client_id
1219+
client_id = "asdfasdf"
1220+
data = {"sql": json.dumps("select 1"), "latest_query_id": json.dumps(client_id)}
1221+
response = self.client.put(f"/tabstateview/{tab_state_id}", data=data)
1222+
self.assertEqual(response.status_code, 400)
1223+
self.assertEqual(response.json["error"], "Bad request")
1224+
# generate query
1225+
db.session.add(Query(client_id=client_id, database_id=1))
1226+
db.session.commit()
1227+
# update tab state with a valid client_id
1228+
response = self.client.put(f"/tabstateview/{tab_state_id}", data=data)
1229+
self.assertEqual(response.status_code, 200)
1230+
# nulls should be ok too
1231+
data["latest_query_id"] = "null"
1232+
response = self.client.put(f"/tabstateview/{tab_state_id}", data=data)
1233+
self.assertEqual(response.status_code, 200)
1234+
12001235
def test_virtual_table_explore_visibility(self):
12011236
# test that default visibility it set to True
12021237
database = superset.utils.database.get_example_database()

0 commit comments

Comments
 (0)