Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created July 14, 2011 00:11
Show Gist options
  • Select an option

  • Save nesquena/1081618 to your computer and use it in GitHub Desktop.

Select an option

Save nesquena/1081618 to your computer and use it in GitHub Desktop.

Revisions

  1. nesquena revised this gist Aug 31, 2011. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion index_users_emails.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    class IndexUsersEmails < ActiveRecord::Migration
    def self.up
    execute "END"
    execute "CREATE INDEX CONCURRENTLY index_users_on_email ON users(email)"
    add_pg_index :users, :email, :lock => false
    execute "BEGIN"
    end
    end
    2 changes: 1 addition & 1 deletion migrations_ext.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    # This is here purely to allow me to use concurrent indexes
    module ActiveRecord::ConnectionAdapters::SchemaStatements
    # add_pg_index :feed_items, [:entity_id, :entity_type, :offset_seconds, :feedable_type, :id], :lock => true
    # add_pg_index :users, :email, :lock => true
    def add_pg_index(table_name, column_name, options = {})
    column_names = Array.wrap(column_name)
    index_name = index_name(table_name, :column => column_names)
  2. nesquena revised this gist Aug 31, 2011. 2 changed files with 30 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions index_users_emails.rb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    class IndexUsersEmails < ActiveRecord::Migration
    def ddl_transaction(&block)
    block.call # do not start a transaction
    end

    def self.up
    execute "END"
    execute "CREATE INDEX CONCURRENTLY index_users_on_email ON users(email)"
    execute "BEGIN"
    end
    end
    28 changes: 28 additions & 0 deletions migrations_ext.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # lib/extensions/migrations_ext.rb

    # This is here purely to allow me to use concurrent indexes
    module ActiveRecord::ConnectionAdapters::SchemaStatements
    # add_pg_index :feed_items, [:entity_id, :entity_type, :offset_seconds, :feedable_type, :id], :lock => true
    def add_pg_index(table_name, column_name, options = {})
    column_names = Array.wrap(column_name)
    index_name = index_name(table_name, :column => column_names)

    if Hash === options # legacy support, since this param was a string
    index_type = options[:unique] ? "UNIQUE" : ""
    index_name = options[:name].to_s if options.key?(:name)
    index_lock = options[:lock] ? "" : "CONCURRENTLY"
    else
    index_type = options
    end

    if index_name.length > index_name_length
    raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters"
    end
    if index_name_exists?(table_name, index_name, false)
    raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
    end
    quoted_column_names = quoted_columns_for_index(column_names, options).join(", ")

    execute "CREATE #{index_type} INDEX #{index_lock} #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})"
    end
    end
  3. nesquena created this gist Jul 14, 2011.
    9 changes: 9 additions & 0 deletions index_users_emails.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class IndexUsersEmails < ActiveRecord::Migration
    def ddl_transaction(&block)
    block.call # do not start a transaction
    end

    def self.up
    execute "CREATE INDEX CONCURRENTLY index_users_on_email ON users(email)"
    end
    end