From 6ee09bca6c79ec273a6cf178f45f594fb51dd68b Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Tue, 31 Oct 2023 16:22:53 -0400 Subject: [PATCH] Don't add pk on remove migration (#571) --- lib/ecto/adapters/myxql/connection.ex | 3 ++- lib/ecto/adapters/postgres/connection.ex | 3 ++- lib/ecto/adapters/tds/connection.ex | 6 +++++- test/ecto/adapters/myxql_test.exs | 12 ++++++++++-- test/ecto/adapters/postgres_test.exs | 9 +++++++++ test/ecto/adapters/tds_test.exs | 7 +++++++ 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/ecto/adapters/myxql/connection.ex b/lib/ecto/adapters/myxql/connection.ex index b5cc5bff..04b807e6 100644 --- a/lib/ecto/adapters/myxql/connection.ex +++ b/lib/ecto/adapters/myxql/connection.ex @@ -1086,7 +1086,8 @@ if Code.ensure_loaded?(MyXQL) do defp pk_definitions(columns, prefix) do pks = - for {_, name, _, opts} <- columns, + for {action, name, _, opts} <- columns, + action != :remove, opts[:primary_key], do: name diff --git a/lib/ecto/adapters/postgres/connection.ex b/lib/ecto/adapters/postgres/connection.ex index 980edd5a..09d3c2f0 100644 --- a/lib/ecto/adapters/postgres/connection.ex +++ b/lib/ecto/adapters/postgres/connection.ex @@ -1340,7 +1340,8 @@ if Code.ensure_loaded?(Postgrex) do defp pk_definition(columns, prefix) do pks = - for {_, name, _, opts} <- columns, + for {action, name, _, opts} <- columns, + action != :remove, opts[:primary_key], do: name diff --git a/lib/ecto/adapters/tds/connection.ex b/lib/ecto/adapters/tds/connection.ex index 8ef788d0..bba45684 100644 --- a/lib/ecto/adapters/tds/connection.ex +++ b/lib/ecto/adapters/tds/connection.ex @@ -1307,7 +1307,8 @@ if Code.ensure_loaded?(Tds) do defp pk_definitions(columns, prefix) do pks = - for {_, name, _, opts} <- columns, + for {action, name, _, opts} <- columns, + action != :remove, opts[:primary_key], do: name @@ -1435,6 +1436,9 @@ if Code.ensure_loaded?(Tds) do [statement_prefix, "DROP COLUMN ", quote_name(name), "; "] end + defp column_change(statement_prefix, _table, {:remove, name, _type, _opts}), + do: [statement_prefix, "DROP COLUMN ", quote_name(name)] + defp column_change( statement_prefix, %{name: table, prefix: prefix}, diff --git a/test/ecto/adapters/myxql_test.exs b/test/ecto/adapters/myxql_test.exs index 37996bb2..4968b069 100644 --- a/test/ecto/adapters/myxql_test.exs +++ b/test/ecto/adapters/myxql_test.exs @@ -2009,8 +2009,9 @@ defmodule Ecto.Adapters.MyXQLTest do ] end - test "alter table with primary key" do - alter = {:alter, table(:posts), [{:add, :my_pk, :serial, [primary_key: true]}]} + test "alter table add/3 with primary key" do + alter = + {:alter, table(:posts), [{:add, :my_pk, :serial, [primary_key: true]}]} assert execute_ddl(alter) == [ """ @@ -2022,6 +2023,13 @@ defmodule Ecto.Adapters.MyXQLTest do ] end + test "alter table remove/3 with primary key" do + alter = + {:alter, table(:posts), [{:remove, :my_pk, :serial, [primary_key: true]}]} + + assert execute_ddl(alter) == ["ALTER TABLE `posts` DROP `my_pk`" |> remove_newlines] + end + test "alter table with invalid reference opts" do alter = {:alter, table(:posts), diff --git a/test/ecto/adapters/postgres_test.exs b/test/ecto/adapters/postgres_test.exs index 0e15be94..c7c2f6bf 100644 --- a/test/ecto/adapters/postgres_test.exs +++ b/test/ecto/adapters/postgres_test.exs @@ -2542,6 +2542,15 @@ defmodule Ecto.Adapters.PostgresTest do ] end + test "alter table remove/3 with primary key" do + alter = + {:alter, table(:posts), [{:remove, :my_pk, :serial, [primary_key: true]}]} + + assert execute_ddl(alter) == [ + "ALTER TABLE \"posts\" DROP COLUMN \"my_pk\"" |> remove_newlines + ] + end + test "create index" do create = {:create, index(:posts, [:category_id, :permalink])} diff --git a/test/ecto/adapters/tds_test.exs b/test/ecto/adapters/tds_test.exs index b9e344c6..567e1428 100644 --- a/test/ecto/adapters/tds_test.exs +++ b/test/ecto/adapters/tds_test.exs @@ -1646,6 +1646,13 @@ defmodule Ecto.Adapters.TdsTest do ] end + test "alter table remove/3 with primary key" do + alter = + {:alter, table(:posts), [{:remove, :my_pk, :serial, [primary_key: true]}]} + + assert execute_ddl(alter) == ["ALTER TABLE [posts] DROP COLUMN [my_pk]" |> remove_newlines] + end + test "alter table with invalid reference opts" do alter = {:alter, table(:posts),