Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created March 27, 2013 17:28
Show Gist options
  • Select an option

  • Save kenmazaika/5256344 to your computer and use it in GitHub Desktop.

Select an option

Save kenmazaika/5256344 to your computer and use it in GitHub Desktop.
class CreatePizzas < ActiveRecord::Migration
def change
create_table :pizzas, :options => 'ENGINE=MyISAM' do |t|
t.string :awesome
t.timestamps
end
add_index :pizzas, :awesome, type: 'FULLTEXT'
end
end
class CreateYolos < ActiveRecord::Migration
def change
create_table :yolos do |t|
t.string :swag
t.timestamps
end
add_index "yolos", ["swag"], name: "you_only_live_once_bro"
end
end
def add_index_options(table_name, column_name, options = {})
column_names = Array(column_name)
index_name = index_name(table_name, column: column_names)
if Hash === options # legacy support, since this param was a string
puts "add_index_options(#{table_name.inspect}, #{column_name.inspect}, #{options.inspect})"
puts "options class: #{options.class}"
puts "options[:type] = #{options[:type]}"
puts "options[:name] = #{options[:name]}"
options = options.symbolize_keys
puts "options[:type] = #{options[:type]}"
puts "options[:name] = #{options[:name]}"
options.assert_valid_keys(:unique, :order, :name, :where, :length, :internal, :using, :algorithm, :type)
index_type = options[:unique] ? "UNIQUE" : ""
index_type = options[:type].to_s if options.key?(:type)
index_name = options[:name].to_s if options.key?(:name)
max_index_length = options.fetch(:internal, false) ? index_name_length : allowed_index_name_length
if index_algorithms.key?(options[:algorithm])
algorithm = index_algorithms[options[:algorithm]]
elsif options[:algorithm].present?
raise ArgumentError.new("Algorithm must be one of the following: #{index_algorithms.keys.map(&:inspect).join(', ')}")
end
using = "USING #{options[:using]}" if options[:using].present?
if supports_partial_index?
index_options = options[:where] ? " WHERE #{options[:where]}" : ""
end
else
if options
message = "Passing a string as third argument of `add_index` is deprecated and will" +
" be removed in Rails 4.1." +
" Use add_index(#{table_name.inspect}, #{column_name.inspect}, unique: true) instead"
ActiveSupport::Deprecation.warn message
end
index_type = options
max_index_length = allowed_index_name_length
algorithm = using = nil
end
if index_name.length > max_index_length
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{max_index_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
index_columns = quoted_columns_for_index(column_names, options).join(", ")
[index_name, index_type, index_columns, index_options, algorithm, using]
end
== CreatePizzas: migrating ===================================================
-- create_table(:pizzas, {:options=>"ENGINE=MyISAM"})
-> 0.0085s
-- add_index(:pizzas, :awesome, {:type=>"FULLTEXT"})
add_index_options("pizzas", :awesome, {:type=>"FULLTEXT"})
options class: Hash
options[:type] = FULLTEXT
options[:name] =
options[:type] = FULLTEXT
options[:name] =
-> 0.0040s
== CreatePizzas: migrated (0.0127s) ==========================================
== CreateYolos: migrating ====================================================
-- create_table(:yolos)
-> 0.0035s
-- add_index("yolos", ["swag"], {:name=>"you_only_live_once_bro"})
add_index_options("yolos", ["swag"], {:name=>"you_only_live_once_bro"})
options class: Hash
options[:type] =
options[:name] = you_only_live_once_bro
options[:type] =
options[:name] = you_only_live_once_bro
-> 0.0074s
== CreateYolos: migrated (0.0111s) ===========================================
create_table "pizzas", force: true do |t|
t.string "awesome"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "pizzas", ["awesome"], name: "index_pizzas_on_awesome", type: :fulltext
create_table "yolos", force: true do |t|
t.string "swag"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "yolos", ["swag"], name: "you_only_live_once_bro", using: :btree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment