Meteor how to perform database migrations?

后端 未结 3 1090
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 04:02

How do you perform database migrations with Meteor? With Ruby on Rails there is ActiveRecord::Migration. Is there an equivalent mechanism in Meteor?

For example, I mak

3条回答
  •  遥遥无期
    2021-01-31 04:57

    There's nothing built in for this. What I've done myself for now is similar to how Rails works, but as part of startup instead of a separate task. First create a Meteor.Collection called Migrations, and then for each discrete migration, create a function under the server subdirectory that runs on startup. It should only run the migration if it hasn't run before, and it should flag the migration in the Migrations collection once its done.

    // database migrations
    Migrations = new Meteor.Collection('migrations');
    
    Meteor.startup(function () {
      if (!Migrations.findOne({name: "addFullName"})) {
        Users.find().forEach(function (user) {
          Users.update(user._id, {$set: {fullname: users.firstname + ' ' + users.lastname}});
        });
        Migrations.insert({name: "addFullName"});
      }
    });
    

    You could extend this technique to support down migrations (look for the existence of a given migration and reverse it), enforce a sort order on the migrations, and split each migration into a separate file if you wanted.

    It'd be interesting to think about a smart package for automating this.

提交回复
热议问题