管理蒙古样迁移 [英] Managing mongoid migrations

查看:83
本文介绍了管理蒙古样迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我简短介绍如何使用Mongoid在Rails中进行数据库迁移吗?我对每个文档的懒惰迁移特别感兴趣.通过这种方式,我的意思是,每当您从数据库中读取文档时,都将其迁移到最新版本并再次保存.

Can someone give me a short introduction to doing DB migrations in Rails using Mongoid? I'm particularly interested in lazy per document migrations. By this, I mean that whenever you read a document from the database, you migrate it to its latest version and save it again.

有人做过这种事吗?我遇到过 mongoid_rails_migrations ,但是它没有提供任何文档,尽管看起来确实如此,但我我不太确定如何使用它.

Has anyone done this sort of thing before? I've come across mongoid_rails_migrations, but it doesn't provide any sort of documentation, and although it looks like it does this, I'm not really sure how to use it.

我应该指出,我只是在概念上熟悉ActiveRecord迁移.

I should point out I'm only conceptually familiar with ActiveRecord migrations.

推荐答案

如果您想一次完成整个迁移,那么mongoid_rails_migrations会满足您的需求.真正需要记录的东西并不多,它重复了标准ActiveRecord迁移的功能.您编写了迁移,然后使用rake db:migrate来应用它们,它可以确定哪些已运行和尚未运行.如果您想了解一些特定的信息,我可以回答其他问题.

If you want to do the entire migration at once, then mongoid_rails_migrations will do what you need. There isn't really much to document, it duplicates the functionality of the standard ActiveRecord migration. You write your migrations, and then you use rake db:migrate to apply them and it handles figuring out which ones have and haven't been ran. I can answer further questions if there is something specific you want to know about it.

对于延迟迁移,最简单的解决方案是使用 after_initialize 回调.检查字段是否与旧的数据方案匹配,是否对它进行了修改并更新了对象,例如:

For lazy migrations, the easiest solution is to use the after_initialize callback. Check if a field matches the old data scheme, and if it does you modify it the object and update it, so for example:

class Person
    include Mongoid::Document

    after_initialize :migrate_data

    field :name, :type => String

    def migrate_data
        if !self[:first_name].blank? or !self[:last_name].blank?
            self.set(:name, "#{self[:first_name]} #{self[:last_name]}".strip)
            self.remove_attribute(:first_name)
            self.remove_attribute(:last_name)
        end
    end
end

我上面给出的特定方法要记住的权衡因素:

The tradeoffs to keep in mind with the specific approach I gave above:

如果您运行的请求返回很多记录,例如Person.all.each {|p| puts p.name}并且100位用户使用的是旧格式,它将立即运行100个集合查询.您也可以调用self.name = "#{self.first_name} #{self.last_name}".strip,但这意味着只有保存记录后,您的数据才会被迁移.

If you run a request that returns a lot of records, such as Person.all.each {|p| puts p.name} and 100 people have the old format, it will immediately run 100 set queries. You could also call self.name = "#{self.first_name} #{self.last_name}".strip instead, but that means your data will only be migrated if the record is saved.

您可能遇到的一般问题是,所有数据查询(例如Person.where(:name => /Foo/).count)都将失败,直到所有数据都被迁移为止.同样,如果您执行Person.only(:name).first,则迁移会失败,因为您忘记了包含first_namelast_name字段.

General issues you might have is that any mass queries such as Person.where(:name => /Foo/).count will fail until all of the data is migrated. Also if you do Person.only(:name).first the migration would fail because you forgot to include the first_name and last_name fields.

这篇关于管理蒙古样迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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