Skip to content

Commit cc17a0d

Browse files
Add prefix support to renaming indices (#653)
1 parent 2091490 commit cc17a0d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/ecto/migration.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ defmodule Ecto.Migration do
11951195
end
11961196

11971197
def rename(%Index{} = current_index, to: new_name) do
1198-
Runner.execute({:rename, current_index, new_name})
1198+
Runner.execute({:rename, __prefix__(current_index), new_name})
11991199
%{current_index | name: new_name}
12001200
end
12011201

test/ecto/migration_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,23 @@ defmodule Ecto.MigrationTest do
821821
assert index.prefix == "baz"
822822
end
823823

824+
test "renames an index" do
825+
rename index(:people, [:name]), to: "person_names_idx"
826+
flush()
827+
{_, index, new_name} = last_command()
828+
assert new_name == "person_names_idx"
829+
assert is_nil(index.prefix)
830+
end
831+
832+
@tag prefix: "foo"
833+
test "renames an index with a prefix" do
834+
rename index(:people, [:name]), to: "person_names_idx"
835+
flush()
836+
{_, index, new_name} = last_command()
837+
assert new_name == "person_names_idx"
838+
assert index.prefix == "foo"
839+
end
840+
824841
test "executes a command" do
825842
execute "SELECT 1", "SELECT 2"
826843
flush()
@@ -1004,6 +1021,23 @@ defmodule Ecto.MigrationTest do
10041021
assert {:create, %Index{}} = last_command()
10051022
end
10061023

1024+
test "renames an index" do
1025+
rename index(:people, [:name]), to: "person_names_idx"
1026+
flush()
1027+
{_, index, old_name} = last_command()
1028+
assert old_name == :people_name_index
1029+
assert is_nil(index.prefix)
1030+
end
1031+
1032+
@tag prefix: "foo"
1033+
test "renames an index with a prefix" do
1034+
rename index(:people, [:name]), to: "person_names_idx"
1035+
flush()
1036+
{_, index, old_name} = last_command()
1037+
assert old_name == :people_name_index
1038+
assert index.prefix == "foo"
1039+
end
1040+
10071041
test "drops a constraint" do
10081042
assert_raise Ecto.MigrationError, ~r/cannot reverse migration command/, fn ->
10091043
drop_if_exists constraint(:posts, :price)

0 commit comments

Comments
 (0)