Skip to content

Instantly share code, notes, and snippets.

@croaky
Created July 27, 2012 04:00
Show Gist options
  • Save croaky/3186117 to your computer and use it in GitHub Desktop.
Save croaky/3186117 to your computer and use it in GitHub Desktop.
Adding indexes CONCURRENTLY in Rails
class AddIndexToUsersSubscriptionToken < ActiveRecord::Migration
def self.up
change_column_null :users, :subscription_token, false
if ENV['NO_DDL_TRANSACTION']
execute <<-eosql
CREATE UNIQUE INDEX CONCURRENTLY index_users_on_subscription_token
ON users (subscription_token)
eosql
else
add_index :users, :subscription_token, :unique => true
end
end
def self.down
remove_index :users, :subscription_token
change_column_null :users, :subscription_token, true
end
end
class ActiveRecord::Migrator
def ddl_transaction_with_override(&block)
if ENV['NO_DDL_TRANSACTION']
block.call
else
ddl_transaction_without_override &block
end
end
alias_method_chain :ddl_transaction, :override
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment