【问题标题】:I messed up migration in my rails app我在 Rails 应用程序中搞砸了迁移
【发布时间】:2017-01-10 08:22:43
【问题描述】:

我是 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 中得到的: 请帮帮我!

【问题讨论】:

  • 运行bundle exec rake db:migrate:up VERSION=20161223064636会得到什么?
  • 我假设您的 rake db:migrate:status 仍然显示 down 用于迁移 VERSION=20161223064636 。根据错误,您确实已将列 cached_votes_total 添加到您的 posts 表中。
  • 你是对的
  • 如果您的迁移 VERSION=20161223064636 仍然 down ,我建议您删除该迁移文件。相反,根据需要向change_column 写入新的迁移创建新的迁移文件以删除现有列并再次添加它们!

标签: ruby-on-rails migration rake


【解决方案1】:

添加两个迁移如下:

  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

【讨论】:

  • 然后运行 ​​db:migrate?
  • remove_column :posts, :cached_votes_total 也应该在def change 内吗?
  • @hekmat:是的,运行rake db:migrate。这对你有用吗?
【解决方案2】:

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

你可以 捆绑执行 rake db:drop bundle exec rake db:create bundle exec rake db:迁移

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

【讨论】:

  • 本地或生产是什么意思?
  • 我的错,开发与生产。在警告中提到的生产中,最好不要删除所有数据(就此而言,任何数据)。在开发环境中,这几乎是日常实践。
  • 请检查编辑有三个命令 1. bundle exec rake db:drop 2. bundle exec rake db:create 3. bundle exec rake db:migrate
  • 太棒了!!我运行了三个命令,我的Acts_as_votable_migration 启动了,但最后一个命令仍然关闭。我是否再次执行相同的过程?
  • 只需捆绑 exec rake db:migrate
猜你喜欢
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多