title: How to wait for the query executed done before finishing migration on orientjs
body: <p>I am using migration to update position for messages based on users.
  I mean I will query to get users from DB, so from user, I get messages and update position for them.
  I use sequence to increment position for message</p>
  
  <p>Version 1: see my code below</p>
  
  <pre><code>"use strict";
  const async = require('async');
  
  exports.name = "add position for message";
  exports.up = function (db) {
    return db.select('uuid').from('User').all().then(function(users) {
      return users.forEach(function(user) {
        console.log('each user', user.uuid);
        db.query(`CREATE SEQUENCE positionMessage${user.uuid} TYPE ORDERED`); // SQL CREATE SEQUENCE
  
        return db.query(`select uuid from Message where in('has_message').uuid = :userUuid`, {
          params: {
            userUuid: user.uuid
          }
        })
        .then(function(messages) {
          console.log('messages', messages.length); // result =&gt; get full messages with 2500 messages.
          return messages.forEach(function(message) {
            console.log('message uuid', message.uuid);
            return db.query(`UPDATE Message SET position = sequence('positionMessage${user.uuid}').next() where uuid = :uuid`, { 
              params: {
                uuid: message.uuid
              }
            })
            .then(function(err) {
              console.log(err);
            })
          })
        })
      })
    })
  };
  
  exports.down = function (db) {
    db.select('uuid').from('User').all().then(function(users) {
      users.forEach(function(user) {
        db.query(`DROP SEQUENCE positionMessage${user.uuid}`); // SQL DROP SEQUENCE
      })
    })
    return db.query(`UPDATE Message REMOVE position`)
  };
  </code></pre>
  
  <blockquote>
    <p>==> result: finished with some messages is updated. count is around 5-> 50 records is updated. (total messages is in DB: 2500)</p>
  </blockquote>
  
  <p>I have tried use async water fall, each... see my code below
  Version 2:</p>
  
  <pre><code>"use strict";
  const async = require('async');
  
  exports.name = "add position for message";
  exports.up = function (db) {
  
    return async.waterfall([
      function getUser(callback) {
        return db.select('uuid').from('User').all().then(function(users) 
        {
           callback(null, users);
        })
    },
    function getMessageForUser(users, callback) {
      //getMessageForUser
      return async.each(users, function (user, callback) {
        // run to async each users
        db.query(`CREATE SEQUENCE positionMessage${user.uuid} TYPE ORDERED`); // SQL CREATE SEQUENCE
        return db.query(`select uuid from Message where in('has_message').uuid = :userUuid`, {
          params:{
            userUuid: user.uuid
          }
        })
        .then(function(messages) {
          return async.each(messages, function(message, callback) {
            // go to async each messages
            return db.query(`UPDATE Message SET position = sequence('positionMessage').next() where uuid = ${message.uuid}`)
              .then( function (response) {
                // updated position for message
                callback();
              })
          }, function doneEachMessage(err) {
            //doneEachMessage
            callback(err);
          })
        })
      }, function doneEachUser(err) {
        // doneEachUser
        callback(err);
        })
      }
    ], function doneWaterFall(err) {
      console.log('doneWaterFall');
    })
  };
  
  exports.down = function (db) {
    db.select('uuid').from('User').all().then(function(users) {
      users.forEach(function(user) {
        db.query(`DROP SEQUENCE positionMessage${user.uuid}`); // SQL DROP SEQUENCE
      })
    })
    return db.query(`UPDATE Message REMOVE position`)
  };
  </code></pre>
  
  <blockquote>
    <p>Result: => Nothing happened.</p>
  </blockquote>
  
  <p>orientjs version I am using is 2.2.6</p>
  
  <p>I dont know what I am doing wrong. Can anyone help me for a solution? Thanks so much!</p>