提交 6626833d 编写于 作者: J José Valim

Revert "Add index length support for MySQL [#1852 state:open]"

This commit breaks dumping a few tables, as the sessions table.
To reproduce, just create a new application and:

  rake db:sessions:create
  rake db:migrate
  rake db:test:prepare

And then look at the db/schema.rb file (ht: Sam Ruby).

This reverts commit 5b95730e.
上级 5b95730e
*Rails 3.0.0 [beta 4/release candidate] (unreleased)* *Rails 3.0.0 [beta 4/release candidate] (unreleased)*
* Add index length support for MySQL. #1852 [Emili Parreno, Pratik Naik]
Example:
add_index(:accounts, :name, :name => 'by_name', :length => 10)
=> CREATE INDEX by_name ON accounts(name(10))
add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})
=> CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
* find_or_create_by_attr(value, ...) works when attr is protected. #4457 [Santiago Pastorino, Marc-André Lafortune] * find_or_create_by_attr(value, ...) works when attr is protected. #4457 [Santiago Pastorino, Marc-André Lafortune]
* New callbacks: after_commit and after_rollback. Do expensive operations like image thumbnailing after_commit instead of after_save. #2991 [Brian Durand] * New callbacks: after_commit and after_rollback. Do expensive operations like image thumbnailing after_commit instead of after_save. #2991 [Brian Durand]
......
...@@ -258,7 +258,7 @@ def simplified_type(field_type) ...@@ -258,7 +258,7 @@ def simplified_type(field_type)
end end
end end
class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths) #:nodoc: class IndexDefinition < Struct.new(:table, :name, :unique, :columns) #:nodoc:
end end
# Abstract representation of a column definition. Instances of this type # Abstract representation of a column definition. Instances of this type
......
...@@ -256,32 +256,18 @@ def rename_column(table_name, column_name, new_column_name) ...@@ -256,32 +256,18 @@ def rename_column(table_name, column_name, new_column_name)
# name. # name.
# #
# ===== Examples # ===== Examples
#
# ====== Creating a simple index # ====== Creating a simple index
# add_index(:suppliers, :name) # add_index(:suppliers, :name)
# generates # generates
# CREATE INDEX suppliers_name_index ON suppliers(name) # CREATE INDEX suppliers_name_index ON suppliers(name)
#
# ====== Creating a unique index # ====== Creating a unique index
# add_index(:accounts, [:branch_id, :party_id], :unique => true) # add_index(:accounts, [:branch_id, :party_id], :unique => true)
# generates # generates
# CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id) # CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
#
# ====== Creating a named index # ====== Creating a named index
# add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party') # add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
# generates # generates
# CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id) # CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
#
# ====== Creating an index with specific key length
# add_index(:accounts, :name, :name => 'by_name', :length => 10)
# generates
# CREATE INDEX by_name ON accounts(name(10))
#
# add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})
# generates
# CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
#
# Note: SQLite doesn't support index length
def add_index(table_name, column_name, options = {}) def add_index(table_name, column_name, options = {})
column_names = Array.wrap(column_name) column_names = Array.wrap(column_name)
index_name = index_name(table_name, :column => column_names) index_name = index_name(table_name, :column => column_names)
...@@ -292,9 +278,7 @@ def add_index(table_name, column_name, options = {}) ...@@ -292,9 +278,7 @@ def add_index(table_name, column_name, options = {})
else else
index_type = options index_type = options
end end
quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
quoted_column_names = quoted_columns_for_index(column_names, options).join(", ")
execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})" execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})"
end end
...@@ -446,11 +430,6 @@ def remove_timestamps(table_name) ...@@ -446,11 +430,6 @@ def remove_timestamps(table_name)
end end
protected protected
# Overridden by the mysql adapter for supporting index lengths
def quoted_columns_for_index(column_names, options = {})
column_names.map {|name| quote_column_name(name) }
end
def options_include_default?(options) def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?) options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end end
......
...@@ -461,11 +461,10 @@ def indexes(table_name, name = nil)#:nodoc: ...@@ -461,11 +461,10 @@ def indexes(table_name, name = nil)#:nodoc:
if current_index != row[2] if current_index != row[2]
next if row[2] == "PRIMARY" # skip the primary key next if row[2] == "PRIMARY" # skip the primary key
current_index = row[2] current_index = row[2]
indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [], []) indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [])
end end
indexes.last.columns << row[4] indexes.last.columns << row[4]
indexes.last.lengths << row[7]
end end
result.free result.free
indexes indexes
...@@ -595,18 +594,6 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ...@@ -595,18 +594,6 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
end end
protected protected
def quoted_columns_for_index(column_names, options = {})
length = options[:length] if options.is_a?(Hash)
quoted_column_names = case length
when Hash
column_names.map {|name| length[name] ? "#{quote_column_name(name)}(#{length[name]})" : quote_column_name(name) }
when Fixnum
column_names.map {|name| "#{quote_column_name(name)}(#{length})"}
else
column_names.map {|name| quote_column_name(name) }
end
end
def translate_exception(exception, message) def translate_exception(exception, message)
return super unless exception.respond_to?(:errno) return super unless exception.respond_to?(:errno)
......
...@@ -177,7 +177,6 @@ def indexes(table, stream) ...@@ -177,7 +177,6 @@ def indexes(table, stream)
statment_parts << index.columns.inspect statment_parts << index.columns.inspect
statment_parts << (':name => ' + index.name.inspect) statment_parts << (':name => ' + index.name.inspect)
statment_parts << ':unique => true' if index.unique statment_parts << ':unique => true' if index.unique
statment_parts << (':length => ' + Hash[*index.columns.zip(index.lengths).flatten].inspect) if index.lengths.compact.present?
' ' + statment_parts.join(', ') ' ' + statment_parts.join(', ')
end end
......
...@@ -15,23 +15,6 @@ def teardown ...@@ -15,23 +15,6 @@ def teardown
end end
end end
def test_add_index
expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`)"
assert_equal expected, add_index(:people, :last_name, :length => nil)
expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`(10))"
assert_equal expected, add_index(:people, :last_name, :length => 10)
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(15))"
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => 15)
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`)"
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15})
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(10))"
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15, :first_name => 10})
end
def test_drop_table def test_drop_table
assert_equal "DROP TABLE `people`", drop_table(:people) assert_equal "DROP TABLE `people`", drop_table(:people)
end end
......
...@@ -92,14 +92,6 @@ def test_add_index ...@@ -92,14 +92,6 @@ def test_add_index
assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") } assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") }
assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => 10) }
assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => {:last_name => 10}) }
assert_nothing_raised { Person.connection.remove_index("people", ["last_name"]) }
assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => 10) }
assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => {:last_name => 10, :first_name => 20}) }
assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
end end
# quoting # quoting
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册