Skip to content

Commit

Permalink
fix(migrations): Fixing cross filter migration (#24279)
Browse files Browse the repository at this point in the history
  • Loading branch information
craig-rueda authored Jun 3, 2023
1 parent 1d9a761 commit da05f22
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import copy
import json
import logging

import sqlalchemy as sa
from alembic import op
Expand All @@ -37,6 +38,7 @@
from superset.migrations.shared.utils import paginated_update

Base = declarative_base()
logger = logging.getLogger(__name__)


class Dashboard(Base):
Expand All @@ -50,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", [])
]
Expand All @@ -70,10 +84,13 @@ def upgrade():
] = "global"

json_metadata["chart_configuration"] = new_chart_configuration
dashboard.json_metadata = json.dumps(json_metadata)

except Exception:
pass
if needs_upgrade:
dashboard.json_metadata = json.dumps(json_metadata)

except Exception as e:
logger.exception("Failed to run up migration")
raise e

session.commit()
session.close()
Expand All @@ -93,18 +110,22 @@ def downgrade():
continue
scope = config.get("crossFilters", {}).get("scope", {})
new_chart_configuration[chart_id] = copy.deepcopy(config)
if scope == "global":
if scope in ("global", "Global"):
new_chart_configuration[chart_id]["crossFilters"]["scope"] = {
"rootPath": ["ROOT_ID"],
"excluded": [chart_id],
}

json_metadata["chart_configuration"] = new_chart_configuration
del json_metadata["global_chart_configuration"]

if "global_chart_configuration" in json_metadata:
del json_metadata["global_chart_configuration"]

dashboard.json_metadata = json.dumps(json_metadata)

except Exception:
pass
except Exception as e:
logger.exception("Failed to run down migration")
raise e

session.commit()
session.close()

0 comments on commit da05f22

Please sign in to comment.