Skip to content

Instantly share code, notes, and snippets.

@rwc9u
Created June 19, 2012 07:57
Show Gist options
  • Save rwc9u/2952889 to your computer and use it in GitHub Desktop.
Save rwc9u/2952889 to your computer and use it in GitHub Desktop.
Adding Index on Heroku without a Restart
module ActiveRecord::ConnectionAdapters::SchemaStatements
def add_index_with_quiet(table_name, column_names, options = {})
quiet = options.delete(:quiet)
add_index_without_quiet table_name, column_names, options
rescue
raise unless quiet and $!.message =~ /^Index name '.*' on table '#{table_name}' already exists/i
puts "Failed to create index #{table_name} #{column_names.inspect} #{options.inspect}"
end
alias_method_chain :add_index, :quiet
end
heroku run console --app <your app name>
irb(main):002:0* class AddIndexToSearches < ActiveRecord::Migration
irb(main):003:1> def change
irb(main):004:2> add_index :searches, :search_string, :quiet => true
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> t = AddIndexToSearches.new
=> #<AddIndexToSearches:0x00000006a240a8 @name="AddIndexToSearches", @version=nil, @connection=nil>
irb(main):008:0> t.change
-- add_index(:searches, :search_string)
(91.8ms) SELECT distinct i.relname, d.indisunique, d.indkey, t.oid
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
WHERE i.relkind = 'i'
AND d.indisprimary = 'f'
AND t.relname = 'searches'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
ORDER BY i.relname
(207509.6ms) CREATE INDEX "index_searches_on_search_string" ON "searches" ("search_string")
-> 207.6028s
=> #<PGresult:0x00000006c81328>
irb(main):009:0>
irb(main):010:0*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment