Skip to content

Commit

Permalink
fix(sqllab): invalid persisted tab state (apache#25308)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark authored Sep 19, 2023
1 parent b722a95 commit 7aaae06
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
29 changes: 16 additions & 13 deletions superset/sqllab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,8 @@ def write_ipc_buffer(table: pa.Table) -> pa.Buffer:


def bootstrap_sqllab_data(user_id: int | None) -> dict[str, Any]:
# send list of tab state ids
tabs_state = (
db.session.query(TabState.id, TabState.label).filter_by(user_id=user_id).all()
)
tab_state_ids = [str(tab_state[0]) for tab_state in tabs_state]
# return first active tab, or fallback to another one if no tab is active
active_tab = (
db.session.query(TabState)
.filter_by(user_id=user_id)
.order_by(TabState.active.desc())
.first()
)

tabs_state: list[Any] = []
active_tab: Any = None
databases: dict[int, Any] = {}
for database in DatabaseDAO.find_all():
databases[database.id] = {
Expand All @@ -102,6 +91,20 @@ def bootstrap_sqllab_data(user_id: int | None) -> dict[str, Any]:

# These are unnecessary if sqllab backend persistence is disabled
if is_feature_enabled("SQLLAB_BACKEND_PERSISTENCE"):
# send list of tab state ids
tabs_state = (
db.session.query(TabState.id, TabState.label)
.filter_by(user_id=user_id)
.all()
)
tab_state_ids = [str(tab_state[0]) for tab_state in tabs_state]
# return first active tab, or fallback to another one if no tab is active
active_tab = (
db.session.query(TabState)
.filter_by(user_id=user_id)
.order_by(TabState.active.desc())
.first()
)
# return all user queries associated with existing SQL editors
user_queries = (
db.session.query(Query)
Expand Down
29 changes: 29 additions & 0 deletions tests/integration_tests/sql_lab/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ def test_get_from_empty_bootsrap_data(self):
assert result["tab_state_ids"] == []
self.assertEqual(len(result["databases"]), 0)

@mock.patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
{"SQLLAB_BACKEND_PERSISTENCE": False},
clear=True,
)
def test_get_from_bootstrap_data_for_non_persisted_tab_state(self):
self.login("admin")
# create a tab
data = {
"queryEditor": json.dumps(
{
"title": "Untitled Query 1",
"dbId": 1,
"schema": None,
"autorun": False,
"sql": "SELECT ...",
"queryLimit": 1000,
}
)
}
self.get_json_resp("/tabstateview/", data=data)
resp = self.client.get("/api/v1/sqllab/")
assert resp.status_code == 200
data = json.loads(resp.data.decode("utf-8"))
result = data.get("result")
assert result["active_tab"] == None
assert result["queries"] == {}
assert result["tab_state_ids"] == []

@mock.patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
{"SQLLAB_BACKEND_PERSISTENCE": True},
Expand Down

0 comments on commit 7aaae06

Please sign in to comment.