Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/199572

Change subject: Linearize chained db queries
......................................................................

Linearize chained db queries

Reduces nesting, should make errors bubble up correctly

Change-Id: I1612f9fdf2d6004111cd8be499c668ea81d24512
---
M persistence.js
1 file changed, 40 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/dash 
refs/changes/72/199572/1

diff --git a/persistence.js b/persistence.js
index d776716..a9ee39d 100644
--- a/persistence.js
+++ b/persistence.js
@@ -60,31 +60,39 @@
                                'SELECT dwi.id, @beboard, 2 FROM 
dash_widget_instance dwi JOIN dash_widget dw ON dwi.widget_id = dw.id WHERE 
owner_id = @uid AND code = \'distance-to-goal-chart\';\n' +
                                'INSERT INTO dash_widget_instance_board ( 
instance_id, board_id, widget_position )\n' +
                                'SELECT dwi.id, @beboard, 3 FROM 
dash_widget_instance dwi JOIN dash_widget dw ON dwi.widget_id = dw.id WHERE 
owner_id = @uid AND code = \'amt-per-second-chart\';',
-                       connection = getConnection();
+                       connection = getConnection(),
+                       defaultBoard,
+                       userId;
 
-               return connection.query( insertUser, params ).then( function() {
-                       return connection.query( getInfo, params ).then( 
function( dbResults ) {
-                               var userId                      = 
dbResults[0][0].id,
-                                       defaultBoard    = 
dbResults[0][0].default_board,
-                                       avatar                  = 
dbResults[0][0].avatar,
+               return connection.query( insertUser, params )
+                       .then( function() {
+                               return connection.query( getInfo, params )
+                       } )
+                       .then( function( dbResults ) {
+                               var avatar                      = 
dbResults[0][0].avatar,
                                        title                   = 
dbResults[0][0].title,
                                        email                   = 
dbResults[0][0].email;
+
+                               userId = dbResults[0][0].id,
+                               defaultBoard = dbResults[0][0].default_board;
                                user.localId = userId;
                                user.avatar = avatar;
                                user.title = title;
                                user.email = email;
-
                                if ( defaultBoard ) {
                                        user.defaultBoard = defaultBoard;
                                        return;
                                }
                                // If user doesn't have a default board, insert 
one now
-                               return connection.query( insertBoard, [ 
'Default dashboard for ' + user.displayName, userId, userId ] ).then( function( 
dbResults ) {
-                                       user.defaultBoard = 
dbResults[0][2][0].id;
-                                       return connection.query( 
insertBigEnglish, [ userId ] );
-                               });
-                       });
-               });
+                               return connection.query( insertBoard, [ 
'Default dashboard for ' + user.displayName, userId, userId ] );
+                       } )
+                       .then( function( dbResults ) {
+                               if ( !dbResults ) {
+                                       return;
+                               }
+                               user.defaultBoard = dbResults[0][2][0].id;
+                               return connection.query( insertBigEnglish, [ 
userId ] );
+                       } );
        },
        /**
         * Saves a widget configuration
@@ -199,20 +207,27 @@
                        if ( board.deleteWidget ) {
                                return connection.query( deleteWidget, 
deleteWidgetParams );
                        }
-                       return connection.query( update, updateParams ).then( 
function( dbResults ) {
-                               if ( dbResults[0].affectedRows !== 1 ) {
-                                       // Either the board doesn't exist or 
it's not ours
-                                       throw new Error( 'Board ' + board.id  + 
' with owner ' + board.ownerId + ' not found' );
-                               }
-                               return connection.query( deleteWidgets, [ 
board.id ] ).then( function() {
+                       return connection.query( update, updateParams )
+                               .then( function( dbResults ) {
+                                       if ( dbResults[0].affectedRows !== 1 ) {
+                                               // Either the board doesn't 
exist or it's not ours
+                                               throw new Error( 'Board ' + 
board.id  + ' with owner ' + board.ownerId + ' not found' );
+                                       }
+                               } )
+                               .then( function() {
+                                       return connection.query( deleteWidgets, 
[ board.id ] )
+                               } )
+                               .then( function() {
                                        return insertWidgetList( board, 
connection );
-                               });
-                       });
+                               } );
                }
-               return connection.query( insert, insertParams ).then( function( 
dbResults ) {
-                       board.id = dbResults[0].insertId;
-                       return insertWidgetList( board, connection );
-               });
+               return connection.query( insert, insertParams )
+                       .then( function( dbResults ) {
+                               board.id = dbResults[0].insertId;
+                       } )
+                       .then( function() {
+                               return insertWidgetList( board, connection );
+                       } );
        },
        /**
         * @param number boardId ID of board to fetch

-- 
To view, visit https://gerrit.wikimedia.org/r/199572
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1612f9fdf2d6004111cd8be499c668ea81d24512
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/dash
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to