Skip to content

Commit

Permalink
Add option to specify custom index name
Browse files Browse the repository at this point in the history
  • Loading branch information
grobie committed Mar 24, 2012
1 parent 9d97475 commit f9b48f3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 16 additions & 8 deletions lib/lhm/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions spec/integration/integration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }'
Expand Down
28 changes: 24 additions & 4 deletions spec/integration/lhm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -67,7 +77,7 @@
end

slave do
key?(:users, :group).must_equal(true)
index_on_columns?(:users, :group).must_equal(true)
end
end

Expand All @@ -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

Expand All @@ -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

Expand Down
38 changes: 35 additions & 3 deletions spec/unit/migrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9b48f3

Please sign in to comment.