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

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


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

commit 721db8a1a9e08fe097a83960c67ec1edbe9888ac
Author: JUST.in DO IT <[email protected]>
AuthorDate: Tue Sep 26 07:19:05 2023 -0700

    fix(sqllab): invalid persisted tab state (#25308) (#25398)
---
 superset/views/core.py                | 31 ++++++++++++++++---------------
 tests/integration_tests/core_tests.py | 27 +++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/superset/views/core.py b/superset/views/core.py
index 1b50a59e36..e39edb99af 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1025,21 +1025,8 @@ class Superset(BaseSupersetView):  # pylint: 
disable=too-many-public-methods
 
     @staticmethod
     def _get_sqllab_tabs(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] = {
@@ -1050,6 +1037,20 @@ class Superset(BaseSupersetView):  # pylint: 
disable=too-many-public-methods
 
         # 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/core_tests.py 
b/tests/integration_tests/core_tests.py
index 63805b6c63..d9f998a066 100644
--- a/tests/integration_tests/core_tests.py
+++ b/tests/integration_tests/core_tests.py
@@ -1091,6 +1091,33 @@ class TestCore(SupersetTestCase, InsertChartMixin):
             data = self.get_resp(url)
             self.assertTrue(html_string in data)
 
+    @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):
+        username = "admin"
+        self.login(username)
+        user_id = security_manager.find_user(username).id
+        # create a tab
+        data = {
+            "queryEditor": json.dumps(
+                {
+                    "title": "Untitled Query 1",
+                    "dbId": 1,
+                    "schema": None,
+                    "autorun": False,
+                    "sql": "SELECT ...",
+                    "queryLimit": 1000,
+                }
+            )
+        }
+
+        payload = views.Superset._get_sqllab_tabs(user_id=user_id)
+        self.assertEqual(len(payload["queries"]), 0)
+        self.assertEqual(len(payload["tab_state_ids"]), 0)
+
     @mock.patch.dict(
         "superset.extensions.feature_flag_manager._feature_flags",
         {"SQLLAB_BACKEND_PERSISTENCE": True},

Reply via email to