Skip to content

Commit

Permalink
Migrate with SQL statements. (#4105)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinsuke-nara authored and arikfr committed Aug 30, 2019
1 parent 31c888e commit cab011d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions migrations/versions/969126bd800f_.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ def upgrade():
# Update widgets position data:
column_size = 3
print("Updating dashboards position data:")
for dashboard in Dashboard.query:
print(" Updating dashboard: {}".format(dashboard.id))
layout = simplejson.loads(dashboard.layout)
dashboard_result = db.session.execute("SELECT id, layout FROM dashboards")
for dashboard in dashboard_result:
print(" Updating dashboard: {}".format(dashboard['id']))
layout = simplejson.loads(dashboard['layout'])

print(" Building widgets map:")
widgets = {}
for w in dashboard.widgets:
print(" Widget: {}".format(w.id))
widgets[w.id] = w
widget_result = db.session.execute(
"SELECT id, options, width FROM widgets WHERE dashboard_id=:dashboard_id",
{"dashboard_id" : dashboard['id']})
for w in widget_result:
print(" Widget: {}".format(w['id']))
widgets[w['id']] = w
widget_result.close()

print(" Iterating over layout:")
for row_index, row in enumerate(layout):
Expand All @@ -47,17 +52,18 @@ def upgrade():
if widget is None:
continue

options = simplejson.loads(widget.options) or {}
options = simplejson.loads(widget['options']) or {}
options['position'] = {
"row": row_index,
"col": column_index * column_size,
"sizeX": column_size * widget.width
}

widget.options = simplejson.dumps(options)

db.session.add(widget)
db.session.execute(
"UPDATE widgets SET options=:options WHERE id=:id",
{"options" : simplejson.dumps(options), "id" : widget_id})

dashboard_result.close()
db.session.commit()

# Remove legacy columns no longer in use.
Expand Down

0 comments on commit cab011d

Please sign in to comment.