code: function(db, couchdb, concurrentDocLimit, docsToDownload) {
    if (docsToDownload.length) {
      var changeSet = docsToDownload.splice(0, concurrentDocLimit);
  
      return couchdb.allDocs({
        include_docs: true,
        keys: _.pluck(changeSet, 'id')
      }).then(function(couchDbResult) {
        log.debug('Pulled ' + couchDbResult.rows.length + ' results from couchdb');
  
        log.debug('Clearing any existing documents from postgresql');
  
        return deleteDocuments(db, _.pluck(couchDbResult.rows, 'id'))
          .then(function() {
            return couchDbResult;
          });
      }).then(function(couchDbResult) {
        log.debug('Inserting ' + couchDbResult.rows.length + ' results into postgresql');
  
        var insertSql = format(INSERT_DOC_STMT,
          couchDbResult.rows.map(function(row) {
            return [row.doc];
          }));
  
        insertSql = sanitise(insertSql);
  
        return db.query(insertSql);
      }).then(function() {
        return loadAndStoreDocs(db, couchdb, concurrentDocLimit, docsToDownload);
      });
    }
  }
docs: /*
  Downloads all given documents from couchdb and stores them in Postgres, in batches.
  We presume if a document is on this list it has changed, and thus needs updating.