Ruby on rails 3.2 rake db:回滚还原错误的迁移

Ruby on rails 3.2 rake db:回滚还原错误的迁移,ruby-on-rails-3.2,rake,rails-migrations,Ruby On Rails 3.2,Rake,Rails Migrations,我在恢复上次迁移时遇到问题 自从我安装“letrate”gem进行评级以来,任何rake db:rollback都会精确地还原letrate gem迁移,而不是预期的最后一次迁移 我怀疑这是由于宝石本身 有没有办法解决这个问题,这样我就可以享受非常方便的回滚 与以下问题相同: rake db:migrate:redo 结果: == CreateRates: reverting ==================================================== -- dr

我在恢复上次迁移时遇到问题

自从我安装“letrate”gem进行评级以来,任何
rake db:rollback
都会精确地还原letrate gem迁移,而不是预期的最后一次迁移

我怀疑这是由于宝石本身

有没有办法解决这个问题,这样我就可以享受非常方便的回滚

与以下问题相同:

rake db:migrate:redo
结果:

==  CreateRates: reverting ====================================================
-- drop_table(:rates)
   -> 0.0224s
==  CreateRates: reverted (0.0225s) ===========================================

==  CreateRates: migrating ====================================================
-- create_table(:rates)
NOTICE:  CREATE TABLE will create implicit sequence "rates_id_seq" for serial column "rates.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rates_pkey" for table "rates"
   -> 0.1787s
-- add_index(:rates, :rater_id)
   -> 0.0032s
-- add_index(:rates, [:rateable_id, :rateable_type])
   -> 0.0024s
==  CreateRates: migrated (0.1850s) ===========================================
rake数据库:迁移:状态

...
   up     20121205224038  Rename user address column
   up     20121206125016587  ********** NO FILE **********
   up     20121206125016605  ********** NO FILE **********
   up     20121210152550  Create reservations
   up     20121210180233  Create transactions
   up     20121210215840  ********** NO FILE **********
   up     20121218144200  Create videos
   up     20121218144800  Add video to videos
   up     20130108225007  Devise invitable add to users
   up     20130130202046  Acts as taggable on migration
   up     20130205154206  Create commissions
   up     20130207133520  Add user id to event transition
档案呢

-rw-r--r--@  1 joel  staff   137 Dec  7 16:40 20121205224038_rename_user_address_column.rb
-rw-r--r--@  1 joel  staff   443 Dec  7 16:40 20121206125016587_create_rating_caches.rb
-rw-r--r--@  1 joel  staff   432 Dec  7 16:40 20121206125016605_create_rates.rb
-rw-r--r--@  1 joel  staff   429 Dec 10 23:30 20121210152550_create_reservations.rb
-rw-r--r--@  1 joel  staff   414 Dec 10 19:03 20121210180233_create_transactions.rb
-rw-r--r--@  1 joel  staff   237 Dec 18 15:44 20121218144200_create_videos.rb
-rw-r--r--@  1 joel  staff   172 Dec 18 16:18 20121218144800_add_video_to_videos.rb
-rw-r--r--@  1 joel  staff   758 Jan  8 23:50 20130108225007_devise_invitable_add_to_users.rb
-rw-r--r--   1 joel  admin   775 Jan 30 21:20 20130130202046_acts_as_taggable_on_migration.rb
-rw-r--r--@  1 joel  admin   422 Feb  5 17:05 20130205154206_create_commissions.rb
-rw-r--r--@  1 joel  admin   266 Feb  7 15:20 20130207133520_add_user_id_to_event_transition.rb

好的,问题是出租率迁移的版本号。Rails只是对迁移文件中的时间戳进行排序,以知道最近应用了哪个时间戳。时间戳中还有3个数字,20121206125016605_create_rates.rb20121206125016587_create_rating_cache将始终被检测为最后一次迁移

让我们尝试修复它并清理您的迁移状态。首先回滚有问题的迁移:

rake db:rollback STEP=2
您的
rake db:migrate:status
现在应该如下所示:

up     20121205224038  Rename user address column
up     20121210152550  Create reservations
up     20121210180233  Create transactions
up     20121210215840  ********** NO FILE **********
up     20121218144200  Create videos
up     20121218144800  Add video to videos
up     20130108225007  Devise invitable add to users
up     20130130202046  Acts as taggable on migration
up     20130205154206  Create commissions
up     20130207133520  Add user id to event transition
现在,让我们修复它们的版本号(假设您的迁移位于默认的
db/migrate
文件夹中)

现在,您的
db:migrate:status
应该如下所示:

up     20121205224038  Rename user address column
down   20121206125016  Create rating caches
down   20121206125016  Create rates  
up     20121210152550  Create reservations
up     20121210180233  Create transactions
up     20121210215840  ********** NO FILE **********
up     20121218144200  Create videos
up     20121218144800  Add video to videos
up     20130108225007  Devise invitable add to users
up     20130130202046  Acts as taggable on migration
up     20130205154206  Create commissions
up     20130207133520  Add user id to event transition
def down
  # This is the old code
  # which I will uncomment later...
end
现在“时间表”已经确定,但我们仍然需要重新应用这些迁移
rake db:migrate
将不起作用,因为最近的迁移现在是20130207133520\u添加用户id到事件转换。rb并且已经应用,所以rake认为它是最新的。。。因此,我们必须欺骗rake:通过注释
down
方法中的所有内容,编辑迁移状态输出中20121206125017\u create\u rates.rb之后出现的每个迁移文件。如果只有
change
方法,请对其进行注释并创建空的up和down方法。所有这些向下的方法都是这样的:

up     20121205224038  Rename user address column
down   20121206125016  Create rating caches
down   20121206125016  Create rates  
up     20121210152550  Create reservations
up     20121210180233  Create transactions
up     20121210215840  ********** NO FILE **********
up     20121218144200  Create videos
up     20121218144800  Add video to videos
up     20130108225007  Devise invitable add to users
up     20130130202046  Acts as taggable on migration
up     20130205154206  Create commissions
up     20130207133520  Add user id to event transition
def down
  # This is the old code
  # which I will uncomment later...
end
您还需要创建一个空迁移,因为存在一个没有关联文件的奇怪迁移(没有文件)。因此,创建一个名为db/migrate/20121210215840_ghost_migration.rb的文件,其内容如下:

class GhostMigration < ActiveRecord::Migration
  def up
  end

  def down
  end
end
迁移状态输出现在应该是

up     20121205224038  Rename user address column
down   20121206125016  Create rating caches
down   20121206125016  Create ratings
down   20121210152550  Create reservations
down   20121210180233  Create transactions
down   20121210215840  Ghost migration
down   20121218144200  Create videos
down   20121218144800  Add video to videos
down   20130108225007  Devise invitable add to users
down   20130130202046  Acts as taggable on migration
down   20130205154206  Create commissions
down   20130207133520  Add user id to event transition
现在,您可以通过取消注释之前的注释并删除“重影迁移”文件,将文件更改回其原始状态。实际上,您应该像以前对down方法那样对“up”方法进行注释,删除“Ghost migration”文件并迁移所有内容

rake db:migrate
最后,取消对文件中注释掉的所有内容的注释,之后事情应该会顺利进行(我希望如此)

关于为什么会发生这种情况,我认为这实际上是由于gem本身,在我看来,gem不应该生成具有无效(或至少非标准)版本号的迁移。看起来gem在同一秒内生成了两个迁移,所以作者可能添加了这3个额外的数字以防止版本号冲突。我认为在同一次迁移中做任何事情都会更好

我希望这能帮助你解决你的问题

更新


也许我把事情搞得太复杂了。如果您不介意实际回滚所有迁移,然后再次迁移它们,那么您不需要对任何文件进行任何注释(尽管“幽灵迁移”技巧仍然是必要的)。我只是觉得这样更安全。

我会尽快试试。谢谢。我会看看我是否能给你+50的赏金。这是你应得的。没问题,我只是希望一切都能像我期望的那样工作,你会把它修好的。:)