percolatestudio/meteor-migrations

Fail to migrations as expecte

bakabird opened this issue · 2 comments

import { Songs } from '../imports/api/songs.js';

// omit the version 1

Migrations.add({
  version: 2,
  name: 'update the songId',
  up() {
    // This is how to get access to the raw MongoDB node collection that the Meteor server collection wraps
    const batch = Songs.rawCollection().initializeUnorderedBulkOp();
    // Mongo throws an error if we execute a batch operation without actual operations, e.g. when Lists was empty.
    let hasUpdates = false;

    let count = 0;
    Songs.find({}).forEach(function(song) {
      console.log(count);
      batch.find({ _id: song._id }).updateOne({ $set: { songId: count } });
      hasUpdates = true;
      count++;
    }, function(err) {
      console.info(err);
      console.log('seetting songId finished!');
    });

    if (hasUpdates) {
      // We need to wrap the async function to get a synchronous API that migrations expects
      const execute = Meteor.wrapAsync(batch.execute, batch);
      return execute();
    }
    return true;
  },
  down() {
    // This is how to get access to the raw MongoDB node collection that the Meteor server collection wraps
    const batch = Songs.rawCollection().initializeUnorderedBulkOp();
    // Mongo throws an error if we execute a batch operation without actual operations, e.g. when Lists was empty.
    let hasUpdates = false;

    Songs.find({}).forEach(function(song) {
      batch.find({ _id: song._id }).updateOne({ $unset: { songId: '' } });
      hasUpdates = true;
    });

    if (hasUpdates) {
      // We need to wrap the async function to get a synchronous API that migrations expects
      const execute = Meteor.wrapAsync(batch.execute, batch);
      return execute();
    }
    return true;
  },
});

I'm tried to think about this problem.
I could not succed add the songId to my songs collections's document after running it.Have i did somthing wrong? Guys,Help me.

version 1 just working fine.

@bakabird you'll probably have better luck asking on https://stackoverflow.com/