正在撤消特定 Rails 迁移,无法删除

Reversing a specific Rails Migration, can't remove

class UpdateIndexOnUsers < ActiveRecord::Migration
  def change
    sql = 'DROP INDEX index_users_on_email'
  sql << ' ON users' if Rails.env == 'production' # Heroku pg
  ActiveRecord::Base.connection.execute(sql)
  end
end

我必须撤消此迁移。当我使用 rake db:migrate VERSION=20150611173430 还原它时,出现此错误。

StandardError: An error has occurred, this and all later migrations canceled:

PG::SyntaxError: ERROR:  syntax error at end of input
LINE 1: CREATE INDEX index_users_on_email
                                         ^
    : CREATE INDEX index_users_on_email/Users/goda/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'

然后我添加一个新的迁移文件来尝试反转它。

class FixUpdateIndexOnUsers < ActiveRecord::Migration
  def change
    sql = 'CREATE INDEX index_users_on_email'
    sql << ' ON users' if Rails.env == 'production' # Heroku pg
    ActiveRecord::Base.connection.execute(sql)
  end
end

但是在 heroku 上,heroku run rake db:migrate 失败了。因为它在我无法删除的第一次迁移中遇到了语法错误。应该怎么办?

编辑:固定语法,仍然抛出错误。

PG::SyntaxError: ERROR:  syntax error at or near "ON"
LINE 1: DROP INDEX index_users_on_email ON users;

您使用的是更改方法而不是向上,您应该只对 rails 可以自动撤消的迁移子集执行此操作。

应该把change方法改成up,加一个down方法,可以反转up方法的效果。