This is an automated email from the ASF dual-hosted git repository.

justinpark pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 7aaae06c9d fix(sqllab): invalid persisted tab state (#25308)
7aaae06c9d is described below

commit 7aaae06c9d071e49100c33a3b609a7d0e2e1368f
Author: JUST.in DO IT <[email protected]>
AuthorDate: Tue Sep 19 15:58:34 2023 -0700

    fix(sqllab): invalid persisted tab state (#25308)
---
 superset/sqllab/utils.py                     | 29 +++++++++++++++-------------
 tests/integration_tests/sql_lab/api_tests.py | 29 ++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/superset/sqllab/utils.py b/superset/sqllab/utils.py
index d308bd4639..989bd19cc3 100644
--- a/superset/sqllab/utils.py
+++ b/superset/sqllab/utils.py
@@ -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] = {
@@ -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)
diff --git a/tests/integration_tests/sql_lab/api_tests.py 
b/tests/integration_tests/sql_lab/api_tests.py
index ebe00add61..6441033b6c 100644
--- a/tests/integration_tests/sql_lab/api_tests.py
+++ b/tests/integration_tests/sql_lab/api_tests.py
@@ -61,6 +61,35 @@ class TestSqlLabApi(SupersetTestCase):
         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},

Reply via email to