Skip to content

Commit

Permalink
feat: Considers GENERIC_CHART_AXES in viz migrations (#23905)
Browse files Browse the repository at this point in the history
Co-authored-by: Ville Brofeldt <[email protected]>
  • Loading branch information
michael-s-molina and villebro committed May 3, 2023
1 parent 2e1cf24 commit 10d640e
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions superset/migrations/shared/migrate_viz/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
# under the License.
from __future__ import annotations

import copy
import json
from typing import Dict, Set
from typing import Any, Dict, Set

from alembic import op
from sqlalchemy import and_, Column, Integer, String, Text
from sqlalchemy.ext.declarative import declarative_base

from superset import db
from superset import conf, db, is_feature_enabled
from superset.migrations.shared.utils import paginated_update, try_load_json

Base = declarative_base()
Expand Down Expand Up @@ -52,7 +53,7 @@ def __init__(self, form_data: str) -> None:
self.data = try_load_json(form_data)

def _pre_action(self) -> None:
"""some actions before migrate"""
"""Some actions before migrate"""

def _migrate(self) -> None:
if self.data.get("viz_type") != self.source_viz_type:
Expand All @@ -68,22 +69,50 @@ def _migrate(self) -> None:

if key in self.rename_keys:
rv_data[self.rename_keys[key]] = value
continue

if key in self.remove_keys:
continue

rv_data[key] = value

if is_feature_enabled("GENERIC_CHART_AXES"):
self._migrate_temporal_filter(rv_data)

self.data = rv_data

def _post_action(self) -> None:
"""some actions after migrate"""
"""Some actions after migrate"""

def _migrate_temporal_filter(self, rv_data: Dict[str, Any]) -> None:
"""Adds a temporal filter."""
granularity_sqla = rv_data.pop("granularity_sqla", None)
time_range = rv_data.pop("time_range", None) or conf.get("DEFAULT_TIME_FILTER")

if not granularity_sqla:
return

temporal_filter = {
"clause": "WHERE",
"subject": granularity_sqla,
"operator": "TEMPORAL_RANGE",
"comparator": time_range,
"expressionType": "SIMPLE",
}

if isinstance(granularity_sqla, dict):
temporal_filter["comparator"] = None
temporal_filter["expressionType"] = "SQL"
temporal_filter["subject"] = granularity_sqla["label"]
temporal_filter["sqlExpression"] = granularity_sqla["sqlExpression"]

rv_data["adhoc_filters"] = rv_data.get("adhoc_filters", []) + [temporal_filter]

@classmethod
def upgrade_slice(cls, slc: Slice) -> Slice:
clz = cls(slc.params)
slc.viz_type = cls.target_viz_type
form_data_bak = clz.data.copy()
form_data_bak = copy.deepcopy(clz.data)

clz._pre_action()
clz._migrate()
Expand Down

0 comments on commit 10d640e

Please sign in to comment.