Meteor如何执行数据库迁移?

Jos*_*itt 24 database upgrade migrate downgrade meteor

如何使用Meteor执行数据库迁移?使用Ruby on Rails,有ActiveRecord :: Migration.Meteor中有相同的机制吗?

例如,我使用一些用户数据制作应用程序.我使用JSON格式将数据存储在Mongo中.应用程序更改,JSON数据库架构需要更改.我可以编写一个迁移方法来更改模式,但是,如果服务器数据库已过期,我只希望它运行.

deb*_*lis 35

没有内置的东西.我现在所做的与Rails的工作方式类似,但是作为启动的一部分而不是单独的任务.首先创建一个Meteor.Collection名为Migrations,然后为每个离散迁移,server在启动时运行的子目录下创建一个函数.它应该只在以前没有运行的情况下运行迁移,并且应该在迁移集合完成后标记迁移.

// 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"});
  }
});
Run Code Online (Sandbox Code Playgroud)

您可以扩展这种技术支持下迁移(寻找特定迁移的存在和扭转它),实施对迁移排序顺序,以及每个迁移拆分成一个独立的文件,如果你想要的.

考虑一个用于自动化的智能软件包会很有趣.


Art*_*iom 6

正如Aram已经在评论中指出的那样,迁移包可以为您提供所需的内容.样品

Migrations.add({
    version: 1,
    name: 'Adds pants to some people in the db.',
    up: function() {//code to migrate up to version 1}
    down: function() {//code to migrate down to version 0}
});

Migrations.add({
    version: 2,
    name: 'Adds a hat to all people in the db who are wearing pants.',
    up: function() {//code to migrate up to version 2}
    down: function() {//code to migrate down to version 1}
});
Run Code Online (Sandbox Code Playgroud)


Ran*_*Ran 5

我为此用例创建了一个智能包。
参见https://atmosphere.meteor.com/package/migrations

  • 还有https://github.com/percolatestudio/meteor-migrations,在我看来,它的设计比https://github.com/rantav/meteor-migrations更干净。 (5认同)