diff --git a/superset/migrations/versions/2023-05-11_12-41_4ea966691069_cross_filter_global_scoping.py b/superset/migrations/versions/2023-05-11_12-41_4ea966691069_cross_filter_global_scoping.py index 02b4b23bcdb7d..638e54430fb3a 100644 --- a/superset/migrations/versions/2023-05-11_12-41_4ea966691069_cross_filter_global_scoping.py +++ b/superset/migrations/versions/2023-05-11_12-41_4ea966691069_cross_filter_global_scoping.py @@ -52,12 +52,24 @@ def upgrade(): bind = op.get_bind() session = db.Session(bind=bind) for dashboard in paginated_update(session.query(Dashboard)): + # + # This is needed in order to work-around a potential issue + # that some folks may have run into where their json_metadata's + # were left partially upgraded. + # + needs_upgrade = True try: json_metadata = json.loads(dashboard.json_metadata) new_chart_configuration = {} for config in json_metadata.get("chart_configuration", {}).values(): chart_id = int(config.get("id", 0)) scope = config.get("crossFilters", {}).get("scope", {}) + + # Skip any JSON's that have the "new" structure + if not isinstance(scope, dict): + needs_upgrade = False + continue + excluded = [ int(excluded_id) for excluded_id in scope.get("excluded", []) ] @@ -72,7 +84,9 @@ def upgrade(): ] = "global" json_metadata["chart_configuration"] = new_chart_configuration - dashboard.json_metadata = json.dumps(json_metadata) + + if needs_upgrade: + dashboard.json_metadata = json.dumps(json_metadata) except Exception as e: logger.exception("Failed to run up migration") @@ -94,9 +108,9 @@ def downgrade(): chart_id = config.get("id") if chart_id is None: continue - scope = config.get("crossFilters", {}).get("scope", "") + scope = config.get("crossFilters", {}).get("scope", {}) new_chart_configuration[chart_id] = copy.deepcopy(config) - if scope.lower() == "global": + if scope in ("global", "Global"): new_chart_configuration[chart_id]["crossFilters"]["scope"] = { "rootPath": ["ROOT_ID"], "excluded": [chart_id],