Skip to content

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

Merged
merged 4 commits into from
Apr 22, 2022

Conversation

pauldambra
Copy link
Member

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

Copy link
Collaborator

@mariusandra mariusandra left a 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 case layouts 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.

@guidoiaquinti
Copy link
Contributor

+1 to what @mariusandra wrote, overall LGTM

@pauldambra
Copy link
Member Author

Could the original migration also be fixed?

OMG you can edit already applied migrations?! 🤯

@pauldambra pauldambra requested a review from mariusandra April 22, 2022 12:20
Copy link
Collaborator

@mariusandra mariusandra left a 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])
Copy link
Collaborator

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... 🤔

@pauldambra pauldambra merged commit 2d49394 into master Apr 22, 2022
@pauldambra pauldambra deleted the layouts_fix branch April 22, 2022 14:28
pauldambra added a commit that referenced this pull request Apr 22, 2022
pauldambra added a commit that referenced this pull request Apr 22, 2022
pauldambra added a commit that referenced this pull request Apr 25, 2022
* Revert "Revert "fix(dashboard-tiles): migrate doubly stringified layouts correctly (#9497)""

This reverts commit 4e5aa60.

* fix: fix migration without using both sql and orm
pauldambra added a commit that referenced this pull request May 13, 2022
* Revert "Revert "fix(dashboard-tiles): migrate doubly stringified layouts correctly (#9497)""

This reverts commit 4e5aa60.

* fix: fix migration without using both sql and orm

(cherry picked from commit d14a4a5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants