管理 monoid 迁移
有人可以给我简单介绍一下如何使用 Mongoid 在 Rails 中进行数据库迁移吗?我对每个文档的惰性迁移特别感兴趣。我的意思是,每当您从数据库中读取文档时,您都会将其迁移到最新版本并再次保存。
以前有人做过这样的事情吗?我遇到过 mongoid_rails_migrations,但它没有提供任何类型的文档,虽然它看起来像这样做,但我我不太确定如何使用它。
我应该指出,我只是在概念上熟悉 ActiveRecord 迁移。
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.
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.
I should point out I'm only conceptually familiar with ActiveRecord migrations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想一次完成整个迁移,那么 mongoid_rails_migrations 将满足您的需要。没有太多需要记录的内容,它复制了标准 ActiveRecord 迁移的功能。您编写迁移,然后使用 rake db:migrate 来应用它们,它会处理哪些迁移已经运行,哪些尚未运行。如果您想了解具体内容,我可以回答更多问题。
对于惰性迁移,最简单的解决方案是使用 after_initialize 回调。检查某个字段是否与旧数据方案匹配,如果匹配,则修改该对象并更新它,例如:
使用我上面给出的特定方法时要记住的权衡:
如果运行返回很多的请求记录,例如
Person.all.each {|p| put p.name}
并且 100 人具有旧格式,它将立即运行 100 组查询。您也可以改为调用self.name = "#{self.first_name} #{self.last_name}".strip
,但这意味着只有保存记录后才会迁移您的数据。您可能遇到的一般问题是,在迁移所有数据之前,任何批量查询(例如
Person.where(:name => /Foo/).count
)都会失败。此外,如果您执行Person.only(:name).first
,迁移也会失败,因为您忘记包含first_name
和last_name
字段。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.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:
The tradeoffs to keep in mind with the specific approach I gave above:
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 callself.name = "#{self.first_name} #{self.last_name}".strip
instead, but that means your data will only be migrated if the record is saved.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 doPerson.only(:name).first
the migration would fail because you forgot to include thefirst_name
andlast_name
fields.Zachary Anker 在他的回答中解释了很多。使用 mongoid_rails_migrations 是迁移的一个不错的选择。
以下是一些示例链接,可帮助您浏览和使用 mongoid_rails_migrations
使用 Mongo 驱动程序进行 Mongoid 迁移
嵌入 Mongoid 文档和数据迁移
除此之外,< a href="https://github.com/adacosta/mongoid_rails_migrations/blob/master/README.rdoc" rel="nofollow">自述文件 应该足够了实现 mongoid 迁移的示例
Zachary Anker has explained a lot in his answer.using mongoid_rails_migrations is a good option for migration.
Here are some links with example that will be useful for you to go through and use mongoid_rails_migrations
Mongoid Migrations using the Mongo Driver
Embedding Mongoid documents and data migrations
Other then this the Readme is should be enough with this example to implement mongoid migration
我有同样的需要。
这是我想到的: https://github.com/nviennot/mongoid_lazy_migration
我很乐意感激一些反馈
I have the same need.
Here is what I came up with: https://github.com/nviennot/mongoid_lazy_migration
I would gladly appreciate some feedback