Gulp-在前一个任务完成创建文件后运行一个任务

时间:2019-07-04 13:54:29

标签: mysql gulp

我有两个任务。第一个将数据库转储,第二个将其删除。

function dumpDB ( ) {

  var conn = mysql.createConnection({
    host     : config.db.dbHost,
    user     : config.db.dbUser,
    password : config.db.dbPassword,
  });

  conn.connect();

  // Check if is there are tables in the database to backup.
  // I do this check because if the database is empty when I dump
  // I receive an error in the console.

  return conn.query('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = \'' + config.db.dbName + '\'', function (error, results, fields) {
    if (error) throw error;

    conn.end();
    count = results[0]['COUNT(*)'];

    if(count>0){

      // create trash folder if it doesn't exists
      if(! fs.existsSync(config.paths.trash)){
        fs.mkdirSync(config.paths.trash);
      }else{
        del.sync(config.paths.trash + '**/*');
      }

      // dump the result straight to a file
      mysqldump({
        connection: {
          host     : config.db.dbHost,
          user     : config.db.dbUser,
          password : config.db.dbPassword,
          database : config.db.dbName,
        },
        dumpToFile: config.paths.trash + 'dump_' + new Date().toISOString().slice(0,19).replace(/:/g, "-").replace("T", "@") + '.sql',
      });      
    }else{

      f.alert('Database <' + config.db.dbName + '> doesn\'t contain anything.\n# Nothing will be dumped.')

    }

  });
}


function deleteDB (done) {

  var pool = mysql.createPool({
    host     : config.db.dbHost,
    user     : config.db.dbUser,
    password : config.db.dbPassword
  });

  return pool.getConnection(function(err, connection) {

    if (err) throw err; // not connected!

    // Use the connection
    connection.query('SHOW DATABASES LIKE \'' + config.db.dbName + '\'', function (error, results, fields) {

      // When done with the connection, release it.
      connection.release();

      if (error) throw error;

      if(results.length>0){

        // Use the connection
        connection.query('DROP DATABASE `' + config.db.dbName + '`', function (error, results, fields) {

          if (error) throw error;

          f.alert('Database <' + config.db.dbName + '> successfully deleted.');

        });

      }else{

        f.alert('Database <' + config.db.dbName + '> doesn\'t exists.');

      } // end if(results.length==0){

      pool.end();

    }); // end connection.query('SHOW DATABASES

  }); // pool.getConnection

}

任务是:exports.delete = series(dumpDB,deleteDB);

问题在于deleteDB在dumpDB仍在创建SQL文件时启动,因此它给出了错误,因为它不再找到数据库。

我该如何解决这个问题?

我尝试使用“ gulp-sequence”,但问题是相同的。有没有办法让序列等到sql文件创建完成?

0 个答案:

没有答案