Skip to content

Commit

Permalink
Making cross filter migration up idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
craig-rueda committed Jun 2, 2023
1 parent 72d193c commit 0a736b5
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
]
Expand All @@ -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")
Expand All @@ -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],
Expand Down

0 comments on commit 0a736b5

Please sign in to comment.