Skip to content

Commit

Permalink
fix(sqllab): invalid persisted tab state (#25308) (#25398)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark authored Sep 26, 2023
1 parent c508a33 commit 721db8a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
31 changes: 16 additions & 15 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,21 +1025,8 @@ def profile(self) -> FlaskResponse:

@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] = {
Expand All @@ -1050,6 +1037,20 @@ def _get_sqllab_tabs(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
27 changes: 27 additions & 0 deletions tests/integration_tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,33 @@ def test_feature_flag_serialization(self):
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},
Expand Down

0 comments on commit 721db8a

Please sign in to comment.