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

本文是小编为大家收集整理的关于Meteor如何进行数据库迁移?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

您如何用流星执行数据库迁移?在Ruby上,有Activerecord ::移民.流星中是否有等效机制?

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

推荐答案

这没有任何内置.我目前所做的工作类似于Rails的工作原理,但作为启动的一部分,而不是单独的任务.首先创建一个称为迁移的Meteor.Collection,然后为每个离散迁移创建一个在启动上运行的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"});
  }
});

您可以扩展此技术以支持下降迁移(寻找给定的迁移并逆转它),在迁移上执行排序订单,并在需要的情况下将每个迁移分为单独的文件.

.

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

其他推荐答案

正如Aram所指向的评论中,P ercallation "> ercaly:迁移:迁移 package package fash you you you您需要什么.样品

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}
});

其他推荐答案

我为此用例创建了一个智能软件包.
请参阅 https://atmosphere.meteor.meteor.com/package/migrations/migrations

本文地址:https://www.itbaoku.cn/post/597471.html

问题描述

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 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.

推荐答案

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.

其他推荐答案

As Aram pointed already in the comment, percolate:migrations package gives you what you need. Sample

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}
});

其他推荐答案

I created a smart package for this use case.
See https://atmosphere.meteor.com/package/migrations