Tag: 数据迁移

如何在Rails迁移中将列(包含内容)移动到另一个表?

我需要将一些列从一个现有表移动到另一个表。 如何使用rails迁移执行此操作? class AddPropertyToUser < ActiveRecord::Migration def self.up add_column :users, :someprop, :string remove_column :profiles, :someprop end def self.down add_column :profiles, :someprop, :string remove_column :users, :someprop end end 以上只是创建新列,但值保持为空… 我想避免登录到数据库以手动更新表。 如果有一种以编程方式移动列值的方法,那么性能特征是什么? 它会逐行进行,还是有办法大量更新?

是否有Ruby数据库迁移gem,它可以帮助您将内容从旧结构移动到新结构?

是否有任何Ruby gems /库可以帮助您从旧的DB结构迁移到新的结构? ActiveRecord迁移可以很好地跟踪新的数据库结构,但我想知道是否有一些东西可以帮助您将整个遗留数据库迁移到新结构: 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一样轻松地进行这些变换,但我希望魔术库可以有更多的变换。 伯恩斯

迁移数据 – 不仅仅是模式,Rails

有时,需要进行数据迁移。 随着时间的推移,使用域模型的代码更改和迁移不再有效,迁移失败。 迁移数据的最佳做法是什么? 我试过一个例子来澄清问题: 考虑一下。 你有一个迁移 class ChangeFromPartnerAppliedToAppliedAt < ActiveRecord::Migration def up User.all.each do |user| user.applied_at = user.partner_application_at user.save end end 当然,这完全没问题。 稍后,您需要更改架构 class AddAcceptanceConfirmedAt < ActiveRecord::Migration def change add_column :users, :acceptance_confirmed_at, :datetime end end class User < ActiveRecord::Base before_save :do_something_with_acceptance_confirmed_at end 对你来说没问题。 它完美运行。 但是,如果您的同事今天同时进行了这两项操作, 但尚未运行第一次迁移 , 那么在运行第一次迁移时会出现此错误: rake aborted! An error has occurred, this […]