Skip to content

Migrations.migrateTo() fails if not in server/main.js #66

@hanskohls

Description

@hanskohls

I have written the following migration that works fine if run from the server/main.js file ...

import '../imports/startup/server';

import { Meteor } from 'meteor/meteor';
import { Migrations } from 'meteor/percolate:migrations';
import { Tags } from '../imports/api/collections/tags/tags.js';


Migrations.add({
  'version': 1,
  'name': "Add multi practice capability to user.",
  'up': function(){
    // the users are assumed to be the owner.
    
    const ownerTagId = Tags.findOne({name: "Practice Owner"})._id;
    if(!ownerTagId){
      return;
    }
    const search = { $and: [
      {practiceId: { $exists: true }},
      {practices: { $exists: false }}
    ]};

    let users = Meteor.users.find(search);
    users.forEach(user =>{
      const practice = {
        id: user.practiceId,
        tags: [ ownerTagId ],
        active: true
      };
      const practices = [practice];
      Meteor.users.update(user._id, {$set: {practices: practices}});
    });
  },
  'down': function(){
    const users =  Meteor.users.find();
    users.forEach((user)=>{
      Meteor.users.update(user._id, {
        $unset: { practices: "" } 
      });
    });
  }
});

Meteor.startup(()=>{
  Migrations.migrateTo(1);  
});

However I really wanted to have a separate migrations.js file or maybe even one per version to sit in the /imports/startup/server folder.

However when trying to run that way - despite having all imports working, I get an error like this:

W20161111-23:36:20.538(0)? (STDERR) TypeError: Cannot read property 'findOne' of undefined
W20161111-23:36:20.539(0)? (STDERR) at Object.Migrations.getControl (packages/percolatemigrations.js:269:33)
W20161111-23:36:20.539(0)? (STDERR) at Object.Migrations.migrateTo (packages/percolatemigrations.js:185:22)
W20161111-23:36:20.540(0)? (STDERR) at Object.Migrations.migrateTo (packages/percolate_migrations.js:167:10)
W20161111-23:36:20.540(0)? (STDERR) at meteorInstall.imports.startup.server.initializeApplication.js (imports/startup/server/initializeApplication.js:60:12)
W20161111-23:36:20.541(0)? (STDERR) at fileEvaluate (packages/modules-runtime.js:181:9)
W20161111-23:36:20.541(0)? (STDERR) at Module.require (packages/modules-runtime.js:106:16)
W20161111-23:36:20.541(0)? (STDERR) at Module.Mp.import (/home/hans/.meteor/packages/modules/.0.7.7.4uxp90++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20161111-23:36:20.542(0)? (STDERR) at meteorInstall.imports.startup.server.index.js (imports/startup/server/index.js:1:1)
W20161111-23:36:20.542(0)? (STDERR) at fileEvaluate (packages/modules-runtime.js:181:9)
W20161111-23:36:20.543(0)? (STDERR) at Module.require (packages/modules-runtime.js:106:16)
=> Exited with code: 1

It appears that Migrations._collecction is not defined in the getControl method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions