2015-06-16 53 views
0
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 

有没有办法通过一次迁移而不是回滚来改变这种情况?Rails如何恢复迁移更改?

编辑:试图rake db:migrate:down VERSION=20150611173755,但没有奏效。

PG::UndefinedObject: ERROR: index "index_users_on_email" does not exist 
: DROP 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' 
/Users/tingaloo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute' 
/Users/tingaloo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log' 
/Users/tingaloo/.rvm/gems/[email protected]/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
/Users/tingaloo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log' 
/Users/tingaloo/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute' 
/Users/tingaloo/rails/novelshare/db/migrate/20150611173755_update_index_on_users.rb:5:in `change' 

回答

0

定制SQL不reverisble。

但你可以改变它是:

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

def down 
    ** code to add the index back ** 
end 

但我认为你应该使用Rails的语法:

def up 
    remove_index :users, :email if Rails.env == 'production' 
end 

def down 
    add_index :users, :email 
end 
+0

我试图底部的代码,但没有奏效。我不确定最上面的代码是做什么的。 – goda

+0

可能是您的索引'index_users_on_email'的命名可能与'add_index'和'remove_index'的约定名称不同。 – davidwessman