-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(dashboard-tiles): migrate doubly stringified layouts correctly #9497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Could the original migration also be fixed? So that new users won't have to convert all their data twice. Once to tiles, and once to correct tiles? This second migration would just be a no-op for them.
- Might it be easier to just have one SQL query that sets
layouts = layouts::json
or something like that, in caselayouts
is a string? Totally unsure about how the query would look like... This is fine though. - I think it's fine to keep the get_items
str
fix around for a while.
+1 to what @mariusandra wrote, overall LGTM |
OMG you can edit already applied migrations?! 🤯 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably good to go, just one 🤔
@@ -34,7 +35,10 @@ def migrate_dashboard_insight_relations(apps, _) -> None: | |||
if not page: | |||
break | |||
DashboardTile.objects.bulk_create( | |||
[DashboardTile(insight_id=row[0], dashboard_id=row[1], layouts=row[2], color=row[3]) for row in page], | |||
[ | |||
DashboardTile(insight_id=row[0], dashboard_id=row[1], layouts=json.loads(row[2]), color=row[3]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a chance this might also be None
somehow... causing json.loads(None)
to throw? The default is a dict
, so probably not... 🤔
Problem
In #9416 the migration loaded layouts from the insight table and saved them to new items in the dashboard tiles table
It used raw sql to load them, and django models to save them
Both fields are JSONField
However, django models stringify the json when saving to DB and destringify when loading.
As they were loaded by raw sql they weren't destringified when loading but, since they were saved using a django model, they were stringified when saving to the new location
This meant they were doubly stringified i.e.
json.dumps(json.dumps(field_data))
Changes
Adds a follow-up migration which loads all dashboard tiles. Pages through them 500 at a time, for each it loads the tile, destringifies the layout once or twice, and adds it back to the tile. Which should mean that the tile's layout is set to a dict (not a string encoded dict)
The amended tiles from the page are then updated in a single operation
👉 Stay up-to-date with PostHog coding conventions for a smoother review.
How did you test this code?
Added unit tests and ran it locally