From cab011def96158adb89c4a00290e1a20ef3f4bd0 Mon Sep 17 00:00:00 2001 From: shinsuke-nara Date: Fri, 30 Aug 2019 20:08:22 +0900 Subject: [PATCH] Migrate with SQL statements. (#4105) --- migrations/versions/969126bd800f_.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/migrations/versions/969126bd800f_.py b/migrations/versions/969126bd800f_.py index 051b97e695..4a476ef956 100644 --- a/migrations/versions/969126bd800f_.py +++ b/migrations/versions/969126bd800f_.py @@ -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): @@ -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.