Skip to content

Commit 6f917aa

Browse files
committed
Add prefix support to renaming indices
Why: * When using [triplex](https://github.com/ateliware/triplex) or similar for multi-tenancy with query prefixes, renaming _tables_ within a tenant is supported, but renaming an index was not. For example: `rename table(:foo), to: table(:bar)` works `rename index(:tablename, [:columns], name: "old_index_name"), to: "new_index_name"` This change addresses the need by: * Making indices similarly prefix-aware as tables.
1 parent 2091490 commit 6f917aa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-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: 17 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()

0 commit comments

Comments
 (0)