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

fix: avoid 500 errors with SQLLAB_BACKEND_PERSISTENCE #25553

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions superset/views/sql_lab/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ def put(self, tab_state_id: int) -> FlaskResponse:
return Response(status=403)

fields = {k: json.loads(v) for k, v in request.form.to_dict().items()}
if client_id := fields.get("latest_query_id"):
query = db.session.query(Query).filter_by(client_id=client_id).one_or_none()
if not query:
return self.json_response(
{"error": f"Unknown client_id: {client_id}"}, status=400
Khrol marked this conversation as resolved.
Show resolved Hide resolved
)
db.session.query(TabState).filter_by(id=tab_state_id).update(fields)
db.session.commit()
return json_success(json.dumps(tab_state_id))
Expand Down
35 changes: 35 additions & 0 deletions tests/integration_tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,41 @@ def test_tabstate_with_name(self):

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

def test_tabstate_update(self):
username = "admin"
self.login(username)
# create a tab
data = {
"queryEditor": json.dumps(
{
"name": "Untitled Query foo",
"dbId": 1,
"schema": None,
"autorun": False,
"sql": "SELECT ...",
"queryLimit": 1000,
}
)
}
resp = self.get_json_resp("/tabstateview/", data=data)
tab_state_id = resp["id"]
# update tab state with non-existing client_id
client_id = "asdfasdf"
data = {"sql": json.dumps("select 1"), "latest_query_id": json.dumps(client_id)}
response = self.client.put(f"/tabstateview/{tab_state_id}", data=data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json["error"], f"Unknown client_id: {client_id}")
# generate query
db.session.add(Query(client_id=client_id, database_id=1))
db.session.commit()
# update tab state with a valid client_id
response = self.client.put(f"/tabstateview/{tab_state_id}", data=data)
self.assertEqual(response.status_code, 200)
# nulls should be ok too
data["latest_query_id"] = "null"
response = self.client.put(f"/tabstateview/{tab_state_id}", data=data)
self.assertEqual(response.status_code, 200)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

def test_virtual_table_explore_visibility(self):
# test that default visibility it set to True
database = superset.utils.database.get_example_database()
Expand Down
Loading