Meteor如何执行数据库迁移? [英] Meteor how to perform database migrations?

查看:156
本文介绍了Meteor如何执行数据库迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Meteor执行数据库迁移? Ruby on Rails有ActiveRecord :: Migration。在Meteor中是否有等效机制?

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

例如,我使用一些用户数据创建一个应用程序。我使用JSON格式在Mongo中存储数据。应用程序更改,并且JSON数据库模式需要更改。我可以写一个迁移方法来改变模式,但是,我只希望在服务器数据库过期时运行。

For example, I make an app with some user data. I'm storing the data in Mongo using a JSON format. The app changes, and the JSON database schema needs to change. I can write a migration method to change the schema, however, I only want this to run if the server database is out of date.

推荐答案

没有内置的。我现在做的自己现在是类似于Rails的工作原理,但作为启动的一部分,而不是一个单独的任务。首先创建一个 Meteor.Collection 调用Migrations,然后对每个离散迁移,在 server 子目录下创建一个函数在启动时运行。它应该只运行迁移,如果它没有运行之前,它应该标记Migrations集合中的迁移完成后。

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.

这篇关于Meteor如何执行数据库迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆