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

时间:2015-06-17 15:48:15

标签: ruby-on-rails ruby heroku migration

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;

1 个答案:

答案 0 :(得分:0)

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

您应该将更改方法更改为up,并添加一个可以反转up方法效果的down方法。