Created
July 27, 2012 04:00
-
-
Save croaky/3186117 to your computer and use it in GitHub Desktop.
Adding indexes CONCURRENTLY in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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