<commit_before>/**
 * Module dependencies
 */

var _ = require('lodash'),
  async = require('async'),
  getRelations = require('../../../utils/getRelations');



/**
 * Try and synchronize the underlying physical-layer schema
 * to work with our app's collections. (i.e. models)
 *
 * @param  {Function} cb
 */
module.exports = function(cb) {
  var self = this;


  //
  // TODO:
  // Refuse to run this migration strategy in production.
  //

  // Find any junctionTables that reference this collection
  var relations = getRelations({
    schema: self.query.waterline.schema,
    parentCollection: self.collection
  });

  var backupData;

  // Check that collection exists--
  self.describe(function afterDescribe(err, attrs) {
    if(err) return cb(err);

    // if it doesn't go ahead and add it and get out
    if(!attrs) return self.define(cb);


    //
    // TODO:
    // Take a look and see if anything important has changed.
    // If it has (at all), we still have to follow the naive strategy below,
    // but it will at least save time in the general case.
    // (because it really sucks to have to wait for all of this to happen
    //  every time you initialize Waterline.)
    //


    //
    // OK so we have to fix up the schema and migrate the data...
    //
    // ... we'll let Waterline do it for us.
    //
    // Load all data from this collection into memory.
    // If this doesn't work, crash to avoid corrupting any data.
    // (see `waterline/lib/adapter/ddl/README.md` for more info about this)
    //
    self.find({}, function (err, existingData) {
      if (err) {
        //
        // TODO:
        // If this was a memory error, log a more useful error
        // explaining what happened.
        //
        return cb(err);
      }

      //
      // From this point forward, we must be very careful.
      //
      backupData = _.cloneDeep(existingData);


      // Now we'll drop the collection.
      self.drop(relations, function (err) {
        if (err) return uhoh(err, backupData, cb);

        // Now we'll redefine the collection.
        self.define(function (err) {
          if (err) return uhoh(err, backupData, cb);

          // Now we'll create the `backupData` again,
          // being careful not to run any lifecycle callbacks
          // and disable automatic updating of `createdAt` and
          // `updatedAt` attributes:
          //
          // ((((TODO: actually be careful about said things))))
          //
          self.createEach(backupData, function(err) {
            if (err) return uhoh(err, backupData, cb);

            // Done.
            return cb();
          });<commit_msg>Fix empty collection issue during schema sync<commit_after>/**
 * Module dependencies
 */

var _ = require('lodash'),
  async = require('async'),
  getRelations = require('../../../utils/getRelations');



/**
 * Try and synchronize the underlying physical-layer schema
 * to work with our app's collections. (i.e. models)
 *
 * @param  {Function} cb
 */
module.exports = function(cb) {
  var self = this;


  //
  // TODO:
  // Refuse to run this migration strategy in production.
  //

  // Find any junctionTables that reference this collection
  var relations = getRelations({
    schema: self.query.waterline.schema,
    parentCollection: self.collection
  });

  var backupData;

  // Check that collection exists--
  self.describe(function afterDescribe(err, attrs) {
    if(err) return cb(err);

    // if it doesn't go ahead and add it and get out
    if(!attrs) return self.define(cb);


    //
    // TODO:
    // Take a look and see if anything important has changed.
    // If it has (at all), we still have to follow the naive strategy below,
    // but it will at least save time in the general case.
    // (because it really sucks to have to wait for all of this to happen
    //  every time you initialize Waterline.)
    //


    //
    // OK so we have to fix up the schema and migrate the data...
    //
    // ... we'll let Waterline do it for us.
    //
    // Load all data from this collection into memory.
    // If this doesn't work, crash to avoid corrupting any data.
    // (see `waterline/lib/adapter/ddl/README.md` for more info about this)
    //
    self.find({}, function (err, existingData) {
      if (err) {
        //
        // TODO:
        // If this was a memory error, log a more useful error
        // explaining what happened.
        //
        return cb(err);
      }

      // If the collection exists but is empty, then do nothing.
      if (existingData.length === 0) return cb();

      //
      // From this point forward, we must be very careful.
      //
      backupData = _.cloneDeep(existingData);


      // Now we'll drop the collection.
      self.drop(relations, function (err) {
        if (err) return uhoh(err, backupData, cb);

        // Now we'll redefine the collection.
        self.define(function (err) {
          if (err) return uhoh(err, backupData, cb);

          // Now we'll create the `backupData` again,
          // being careful not to run any lifecycle callbacks
          // and disable automatic updating of `createdAt` and
          // `updatedAt` attributes:
          //
          // ((((TODO: actually be careful about said things))))
          //
          self.createEach(backupData, function(err) {
            if (err) return uhoh(err, backupData, cb);

            // Done.
            return cb();
          });
{'fraction_non_alphanumeric': 0.11503291880029261, 'fraction_numerical': 0.000182882223847842, 'mean_word_length': 1.993431855500821, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6978488', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1485, 'n_words': 587}