From 6f917aaa3a66d521d8f7eeb1f961496de9932b8d Mon Sep 17 00:00:00 2001 From: Paul Ostazeski Date: Thu, 23 Jan 2025 09:49:47 -0500 Subject: [PATCH 1/3] 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. --- lib/ecto/migration.ex | 2 +- test/ecto/migration_test.exs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/ecto/migration.ex b/lib/ecto/migration.ex index 8b0d8191..8073bd1e 100644 --- a/lib/ecto/migration.ex +++ b/lib/ecto/migration.ex @@ -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 diff --git a/test/ecto/migration_test.exs b/test/ecto/migration_test.exs index 4b0ede88..10f48eab 100644 --- a/test/ecto/migration_test.exs +++ b/test/ecto/migration_test.exs @@ -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() From 3b20b86203af4e44b99e9e3aed2b1ad377c6ba3a Mon Sep 17 00:00:00 2001 From: Paul Ostazeski Date: Thu, 23 Jan 2025 13:24:40 -0500 Subject: [PATCH 2/3] Adding test for reverse direction --- test/ecto/migration_test.exs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/ecto/migration_test.exs b/test/ecto/migration_test.exs index 10f48eab..fb251ee2 100644 --- a/test/ecto/migration_test.exs +++ b/test/ecto/migration_test.exs @@ -1021,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) From dcdf541a387746c3105b4dfdd707d9f8fbe7408e Mon Sep 17 00:00:00 2001 From: Paul Ostazeski Date: Thu, 23 Jan 2025 15:34:25 -0500 Subject: [PATCH 3/3] CI failure _looks_ unrelated to change