是否有一个Ruby数据库迁移工具,可以帮助你将内容从一个旧的结构转移到一个新的结构?[英] Is there a Ruby database migration gem, that helps you move content from an old structure to a new structure?

本文是小编为大家收集整理的关于是否有一个Ruby数据库迁移工具,可以帮助你将内容从一个旧的结构转移到一个新的结构?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有任何红宝石宝石/库可以帮助您从旧的DB结构迁移到新结构? Activerecord迁移做得很好,跟踪新的DB结构,但我想知道是否有一些可以帮助您将整个旧版DB迁移到新结构:

transfer_from(:source_table => 'person', :destination_table => 'dudes_and_dudets') do

  from :name, :to => :full_name

  from :dob, :to => :age do |dob|    # this would, for example, load the result  
    (Date.today - dob)/60/60/24/365  # of the block into :age
  end

end

(我意识到您可以通过AR轻松地进行这些转换,但是我希望 魔术库会有更多的转换.

伯恩斯

推荐答案

我已经开始为此工作.

如果有人想提供有关更好/更惯用或更有效实施的提示,请告诉我.

http://github.com/btelles/legacy_migrations

编辑:

我现在有这种精确的语法在上述github存储库上工作...计划添加一些镜头任务,以将旧结构映射到新的ActivereCord类中,以及更多的转换...以防万一任何人感兴趣.

.

它也在gemcutter/rubygems上:gem install lecacy_migrations

其他推荐答案

您可以从迁移中访问所有模型,从而处理所有数据迁移.如果您已经知道这一点,而您的问题是关于这样做的整洁方法,那么这当然不是您要寻找的答案.

您的示例一个问题是您不能迁移到较早的版本,但这仅仅是因为您在转换中演示的块功能.

我承认您的示例很好且简洁,但这是一个常规的迁移示例:

class FooBar < ActiveRecord::Migration
  def self.up
    # This is only needed if the new table will have the same name.
    # Move the old one aside.
    rename_table :users, :old_users

    # The new table structure
    create_table :users do |t|
      t.string :full_name
      t.date   :age
    end

    # Data migration
    OldUsers.all.each do |orig|
      User.create!(
        :full_name => orig.name,
        :age => (Date.today - orig.dob)/60/60/24/365
      )
    end

    # Clean up
    drop_table :old_users
  end

  def self.down
    # Exercise for the reader!
  end
end

# Temporary class for accessing the old table during conversion
class OldUsers < ActiveRecord::Base; end

其他推荐答案

我必须做类似您描述的(我想)的事情,并且我使用了>续集为此.续集是适应性的,通常有用,可以直接以方便的方式与SQL一起使用,并且可以访问多种不同的DBS.

文档非常方便,我建议它.

这就是一个例子使用续集从 geonames 中获取一个巨大的任意扁平文件并使用它来填写DB并进行查询.这可能是一个很好的例子,让您做一些事情要做自己想做的事.

它是轨道 - 不可思议的.不需要附上模型,迁移或除了几个宝石以外的其他任何内容.

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

问题描述

Are there any Ruby gems/libraries that help you migrate from an old DB structure to a new structure? ActiveRecord migrations do a good job keeping track of a new db structure, but I wonder if there's something that helps you migrate an entire legacy db to a new structure:

transfer_from(:source_table => 'person', :destination_table => 'dudes_and_dudets') do

  from :name, :to => :full_name

  from :dob, :to => :age do |dob|    # this would, for example, load the result  
    (Date.today - dob)/60/60/24/365  # of the block into :age
  end

end

(I realize you could do these transforms just as easily with AR, but I'm hoping the magic library would have many more transforms.

Berns

推荐答案

I've begun working on this.

If anyone would like to give tips on a better/more idiomatic, or more efficient implementation, please let me know.

http://github.com/btelles/legacy_migrations

edit:

I now have this exact syntax working on the above github repository... plan to add a few rake tasks for mapping the old structure to new ActiveRecord classes, and more transforms...in case anyone's interested.

It's also on gemcutter/rubygems: gem install legacy_migrations

其他推荐答案

You can access all your models from within a migration, and thus handle all your data migrations right there too. If you already knew this, and your question was about a neater way of doing it, then of course this is not the answer you're looking for.

One problem with your example is that you can't migrate down to an earlier version, but only because of the block feature you demonstrate in conversions.

I admit that your example is nice and terse, but here's a regular migration example any way:

class FooBar < ActiveRecord::Migration
  def self.up
    # This is only needed if the new table will have the same name.
    # Move the old one aside.
    rename_table :users, :old_users

    # The new table structure
    create_table :users do |t|
      t.string :full_name
      t.date   :age
    end

    # Data migration
    OldUsers.all.each do |orig|
      User.create!(
        :full_name => orig.name,
        :age => (Date.today - orig.dob)/60/60/24/365
      )
    end

    # Clean up
    drop_table :old_users
  end

  def self.down
    # Exercise for the reader!
  end
end

# Temporary class for accessing the old table during conversion
class OldUsers < ActiveRecord::Base; end

其他推荐答案

I've had to do something like (what I think) you're describing, and I used Sequel for it. Sequel is adaptable and generally useful and can work with SQL directly in a fairly handy fashion and can access multiple different sorts of DBs.

The documentation is very handy, and I recommend it entirely.

Here's an example of using sequel to take a huge arbitrarily flatfile from geonames and use it to fill out a db and make queries. This is probably a good example for you to make something to do what you want.

It's rails-agnostic. doesn't need to be attached to a model, migration, or anything else other than a couple gems.