Skip to content

Commit de13a78

Browse files
authored
fix(sqllab): invalid persisted tab state (apache#25308)
1 parent 1928b12 commit de13a78

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

superset/sqllab/utils.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,8 @@ def write_ipc_buffer(table: pa.Table) -> pa.Buffer:
7979

8080

8181
def bootstrap_sqllab_data(user_id: int | None) -> dict[str, Any]:
82-
# send list of tab state ids
83-
tabs_state = (
84-
db.session.query(TabState.id, TabState.label).filter_by(user_id=user_id).all()
85-
)
86-
tab_state_ids = [str(tab_state[0]) for tab_state in tabs_state]
87-
# return first active tab, or fallback to another one if no tab is active
88-
active_tab = (
89-
db.session.query(TabState)
90-
.filter_by(user_id=user_id)
91-
.order_by(TabState.active.desc())
92-
.first()
93-
)
94-
82+
tabs_state: list[Any] = []
83+
active_tab: Any = None
9584
databases: dict[int, Any] = {}
9685
for database in DatabaseDAO.find_all():
9786
databases[database.id] = {
@@ -102,6 +91,20 @@ def bootstrap_sqllab_data(user_id: int | None) -> dict[str, Any]:
10291

10392
# These are unnecessary if sqllab backend persistence is disabled
10493
if is_feature_enabled("SQLLAB_BACKEND_PERSISTENCE"):
94+
# send list of tab state ids
95+
tabs_state = (
96+
db.session.query(TabState.id, TabState.label)
97+
.filter_by(user_id=user_id)
98+
.all()
99+
)
100+
tab_state_ids = [str(tab_state[0]) for tab_state in tabs_state]
101+
# return first active tab, or fallback to another one if no tab is active
102+
active_tab = (
103+
db.session.query(TabState)
104+
.filter_by(user_id=user_id)
105+
.order_by(TabState.active.desc())
106+
.first()
107+
)
105108
# return all user queries associated with existing SQL editors
106109
user_queries = (
107110
db.session.query(Query)

tests/integration_tests/sql_lab/api_tests.py

+29
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ def test_get_from_empty_bootsrap_data(self):
6161
assert result["tab_state_ids"] == []
6262
self.assertEqual(len(result["databases"]), 0)
6363

64+
@mock.patch.dict(
65+
"superset.extensions.feature_flag_manager._feature_flags",
66+
{"SQLLAB_BACKEND_PERSISTENCE": False},
67+
clear=True,
68+
)
69+
def test_get_from_bootstrap_data_for_non_persisted_tab_state(self):
70+
self.login("admin")
71+
# create a tab
72+
data = {
73+
"queryEditor": json.dumps(
74+
{
75+
"title": "Untitled Query 1",
76+
"dbId": 1,
77+
"schema": None,
78+
"autorun": False,
79+
"sql": "SELECT ...",
80+
"queryLimit": 1000,
81+
}
82+
)
83+
}
84+
self.get_json_resp("/tabstateview/", data=data)
85+
resp = self.client.get("/api/v1/sqllab/")
86+
assert resp.status_code == 200
87+
data = json.loads(resp.data.decode("utf-8"))
88+
result = data.get("result")
89+
assert result["active_tab"] == None
90+
assert result["queries"] == {}
91+
assert result["tab_state_ids"] == []
92+
6493
@mock.patch.dict(
6594
"superset.extensions.feature_flag_manager._feature_flags",
6695
{"SQLLAB_BACKEND_PERSISTENCE": True},

0 commit comments

Comments
 (0)