我在 rails 应用程序中搞砸了迁移

I messed up migration in my rails app

我是 rails 的新手,我想在迁移文件中添加一个字符串。不幸的是,我不知道玩迁移是非常危险的。

ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

我相信我使用了 rake db:rollback 并且我使用了 rake db:redo 并且没有任何变化我不断收到相同的错误

当我尝试 rake db:migrate:status 时,我得到的是:

database: /home/ubuntu/workspace/db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20161022035511  Create posts
   up     20161022044605  Devise create users
   up     20161022045410  Add user id to post
   up     20161022050429  Add name to user
   up     20161022054826  Add attachment image to posts
   up     20161022170851  Create comments
  down    20161022184713  Acts as votable migration
  down    20161223064636  Add cached votes to posts

这是 AddCachedVotesToPosts 的内容:

class AddCachedVotesToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :cached_votes_total, :integer, :default => 0
    add_column :posts, :cached_votes_score, :integer, :default => 0
    add_column :posts, :cached_votes_up, :integer, :default => 0
    add_column :posts, :cached_votes_down, :integer, :default => 0
    add_column :posts, :cached_weighted_score, :integer, :default => 0
    add_column :posts, :cached_weighted_total, :integer, :default => 0
    add_column :posts, :cached_weighted_average, :float, :default => 0.0
    add_index  :posts, :cached_votes_total
    add_index  :posts, :cached_votes_score
    add_index  :posts, :cached_votes_up
    add_index  :posts, :cached_votes_down
    add_index  :posts, :cached_weighted_score
    add_index  :posts, :cached_weighted_total
    add_index  :posts, :cached_weighted_average

    # Uncomment this line to force caching of existing votes
    # Post.find_each(&:update_cached_votes)
  end

  def self.down
    remove_column :posts, :cached_votes_total
    remove_column :posts, :cached_votes_score
    remove_column :posts, :cached_votes_up
    remove_column :posts, :cached_votes_down
    remove_column :posts, :cached_weighted_score
    remove_column :posts, :cached_weighted_total
    remove_column :posts, :cached_weighted_average
  end
end

这是我在 rails c 中得到的: 请帮帮我!

如果这是您的本地数据库而不是生产数据库,请修复此问题。

你可以做到 捆绑执行耙 db:drop 捆绑执行耙 db:create 捆绑执行耙 db:migrate

[警告] 这将删除您存储在本地数据库中的所有内容。

添加两个迁移如下:

  1. rails g migration RemoveCachedVotesTotalFromPost :

其内容应为:

def change
  remove_column :posts, :cached_votes_total
end
  1. rails g migration AddCachedVotesToPost :

其内容应为:

def self.up
  add_column :posts, :cached_votes_total, :integer, :default => 0
  add_column :posts, :cached_votes_score, :integer, :default => 0
  add_column :posts, :cached_votes_up, :integer, :default => 0
  add_column :posts, :cached_votes_down, :integer, :default => 0
  add_column :posts, :cached_weighted_score, :integer, :default => 0
  add_column :posts, :cached_weighted_total, :integer, :default => 0
  add_column :posts, :cached_weighted_average, :float, :default => 0.0
  add_index  :posts, :cached_votes_total
  add_index  :posts, :cached_votes_score
  add_index  :posts, :cached_votes_up
  add_index  :posts, :cached_votes_down
  add_index  :posts, :cached_weighted_score
  add_index  :posts, :cached_weighted_total
  add_index  :posts, :cached_weighted_average
end

def self.down
  remove_column :posts, :cached_votes_total
  remove_column :posts, :cached_votes_score
  remove_column :posts, :cached_votes_up
  remove_column :posts, :cached_votes_down
  remove_column :posts, :cached_weighted_score
  remove_column :posts, :cached_weighted_total
  remove_column :posts, :cached_weighted_average
end