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(sqllab): invalid persisted tab state #25308

Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading