Permalink
Browse files

Add option to specify custom index name

  • Loading branch information...
1 parent 9d97475 commit f9b48f3b62e3570474a369b14e43a5a788be9975 @grobie committed Mar 24, 2012
Showing with 86 additions and 17 deletions.
  1. +4 −0 CHANGELOG.md
  2. +16 −8 lib/lhm/migrator.rb
  3. +7 −2 spec/integration/integration_helper.rb
  4. +24 −4 spec/integration/lhm_spec.rb
  5. +35 −3 spec/unit/migrator_spec.rb
View
@@ -1,3 +1,7 @@
+# 1.1.0 (not released yet)
+
+* Add option to specify custom index name
+
# 1.0.3 (February 23, 2012)
* Improve change_column
View
@@ -95,8 +95,10 @@ def remove_column(name)
# @param [String, Symbol, Array<String, Symbol>] columns
# A column name given as String or Symbol. An Array of Strings or Symbols
# for compound indexes. It's possible to pass a length limit.
- def add_index(columns)
- ddl(index_ddl(columns))
+ # @param [String, Symbol] index_name
+ # Optional name of the index to be created
+ def add_index(columns, index_name = nil)
+ ddl(index_ddl(columns, false, index_name))
end
# Add a unique index to a table
@@ -112,8 +114,10 @@ def add_index(columns)
# @param [String, Symbol, Array<String, Symbol>] columns
# A column name given as String or Symbol. An Array of Strings or Symbols
# for compound indexes. It's possible to pass a length limit.
- def add_unique_index(columns)
- ddl(index_ddl(columns, :unique))
+ # @param [String, Symbol] index_name
+ # Optional name of the index to be created
+ def add_unique_index(columns, index_name = nil)
+ ddl(index_ddl(columns, true, index_name))
end
# Remove an index from a table
@@ -128,8 +132,11 @@ def add_unique_index(columns)
# @param [String, Symbol, Array<String, Symbol>] columns
# A column name given as String or Symbol. An Array of Strings or Symbols
# for compound indexes.
- def remove_index(columns)
- ddl("drop index `%s` on `%s`" % [idx_name(@origin.name, columns), @name])
+ # @param [String, Symbol] index_name
+ # Optional name of the index to be removed
+ def remove_index(columns, index_name = nil)
+ index_name ||= idx_name(@origin.name, columns)
+ ddl("drop index `%s` on `%s`" % [index_name, @name])
end
private
@@ -167,9 +174,10 @@ def destination_read
Table.parse(@origin.destination_name, connection)
end
- def index_ddl(cols, unique = nil)
+ def index_ddl(cols, unique = nil, index_name = nil)
type = unique ? "unique index" : "index"
- parts = [type, idx_name(@origin.name, cols), @name, idx_spec(cols)]
+ index_name ||= idx_name(@origin.name, cols)
+ parts = [type, index_name, @name, idx_spec(cols)]
"create %s `%s` on `%s` (%s)" % parts
end
end
@@ -99,10 +99,15 @@ def count_all(table)
select_value(query).to_i
end
- def key?(table_name, cols, type = :non_unique)
- non_unique = type == :non_unique ? 1 : 0
+ def index_on_columns?(table_name, cols, type = :non_unique)
key_name = Lhm::SqlHelper.idx_name(table_name, cols)
+ index?(table_name, key_name, type)
+ end
+
+ def index?(table_name, key_name, type = :non_unique)
+ non_unique = type == :non_unique ? 1 : 0
+
!!select_value(%Q<
show indexes in #{ table_name }
where key_name = '#{ key_name }'
@@ -57,7 +57,17 @@
end
slave do
- key?(:users, [:comment, :created_at]).must_equal(true)
+ index_on_columns?(:users, [:comment, :created_at]).must_equal(true)
+ end
+ end
+
+ it "should add an index with a custom name" do
+ Lhm.change_table(:users) do |t|
+ t.add_index([:comment, :created_at], :my_index_name)
+ end
+
+ slave do
+ index?(:users, :my_index_name).must_equal(true)
end
end
@@ -67,7 +77,7 @@
end
slave do
- key?(:users, :group).must_equal(true)
+ index_on_columns?(:users, :group).must_equal(true)
end
end
@@ -77,7 +87,7 @@
end
slave do
- key?(:users, :comment, :unique).must_equal(true)
+ index_on_columns?(:users, :comment, :unique).must_equal(true)
end
end
@@ -87,7 +97,17 @@
end
slave do
- key?(:users, [:username, :created_at]).must_equal(false)
+ index_on_columns?(:users, [:username, :created_at]).must_equal(false)
+ end
+ end
+
+ it "should remove an index with a custom name" do
+ Lhm.change_table(:users) do |t|
+ t.remove_index(:reference, :index_users_on_reference)
+ end
+
+ slave do
+ index?(:users, :index_users_on_reference).must_equal(false)
end
end
View
@@ -16,36 +16,68 @@
describe "index changes" do
it "should add an index" do
- @creator.add_index(["a", "b"])
+ @creator.add_index(:a)
+
+ @creator.statements.must_equal([
+ "create index `index_alt_on_a` on `lhmn_alt` (`a`)"
+ ])
+ end
+
+ it "should add a composite index" do
+ @creator.add_index([:a, :b])
@creator.statements.must_equal([
"create index `index_alt_on_a_and_b` on `lhmn_alt` (`a`, `b`)"
])
end
- it "should add an index with prefixed columns" do
+ it "should add an index with prefix length" do
@creator.add_index(["a(10)", "b"])
@creator.statements.must_equal([
"create index `index_alt_on_a_and_b` on `lhmn_alt` (`a`(10), `b`)"
])
end
- it "should add an unique index" do
+ it "should add an index with a custom name" do
+ @creator.add_index([:a, :b], :custom_index_name)
+
+ @creator.statements.must_equal([
+ "create index `custom_index_name` on `lhmn_alt` (`a`, `b`)"
+ ])
+ end
+
+ it "should add a unique index" do
@creator.add_unique_index(["a(5)", :b])
@creator.statements.must_equal([
"create unique index `index_alt_on_a_and_b` on `lhmn_alt` (`a`(5), `b`)"
])
end
+ it "should add a unique index with a custom name" do
+ @creator.add_unique_index([:a, :b], :custom_index_name)
+
+ @creator.statements.must_equal([
+ "create unique index `custom_index_name` on `lhmn_alt` (`a`, `b`)"
+ ])
+ end
+
it "should remove an index" do
@creator.remove_index(["b", "a"])
@creator.statements.must_equal([
"drop index `index_alt_on_b_and_a` on `lhmn_alt`"
])
end
+
+ it "should remove an index with a custom name" do
+ @creator.remove_index([:a, :b], :custom_index_name)
+
+ @creator.statements.must_equal([
+ "drop index `custom_index_name` on `lhmn_alt`"
+ ])
+ end
end
describe "column changes" do

0 comments on commit f9b48f3

Please sign in to comment.