Skip to content

Commit

Permalink
Add prefix support to renaming indices (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulOstazeski authored Jan 23, 2025
1 parent 2091490 commit cc17a0d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ defmodule Ecto.Migration do
end

def rename(%Index{} = current_index, to: new_name) do
Runner.execute({:rename, current_index, new_name})
Runner.execute({:rename, __prefix__(current_index), new_name})
%{current_index | name: new_name}
end

Expand Down
34 changes: 34 additions & 0 deletions test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,23 @@ defmodule Ecto.MigrationTest do
assert index.prefix == "baz"
end

test "renames an index" do
rename index(:people, [:name]), to: "person_names_idx"
flush()
{_, index, new_name} = last_command()
assert new_name == "person_names_idx"
assert is_nil(index.prefix)
end

@tag prefix: "foo"
test "renames an index with a prefix" do
rename index(:people, [:name]), to: "person_names_idx"
flush()
{_, index, new_name} = last_command()
assert new_name == "person_names_idx"
assert index.prefix == "foo"
end

test "executes a command" do
execute "SELECT 1", "SELECT 2"
flush()
Expand Down Expand Up @@ -1004,6 +1021,23 @@ defmodule Ecto.MigrationTest do
assert {:create, %Index{}} = last_command()
end

test "renames an index" do
rename index(:people, [:name]), to: "person_names_idx"
flush()
{_, index, old_name} = last_command()
assert old_name == :people_name_index
assert is_nil(index.prefix)
end

@tag prefix: "foo"
test "renames an index with a prefix" do
rename index(:people, [:name]), to: "person_names_idx"
flush()
{_, index, old_name} = last_command()
assert old_name == :people_name_index
assert index.prefix == "foo"
end

test "drops a constraint" do
assert_raise Ecto.MigrationError, ~r/cannot reverse migration command/, fn ->
drop_if_exists constraint(:posts, :price)
Expand Down

0 comments on commit cc17a0d

Please sign in to comment.