Skip to content

Commit

Permalink
Handle from: {reference, opts} in FK migrations (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisMT committed May 10, 2023
1 parent fa5aff1 commit a4c70f5
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ if Code.ensure_loaded?(MyXQL) do
defp constraint_if_not_exists_expr(%Reference{} = ref, table, name),
do: [", ADD " | reference_expr("FOREIGN KEY IF NOT EXISTS", ref, table, name)]

defp drop_constraint_expr({%Reference{} = ref, _opts}, table, name),
do: drop_constraint_expr(ref, table, name)
defp drop_constraint_expr(%Reference{} = ref, table, name),
do: ["DROP FOREIGN KEY ", reference_name(ref, table, name), ", "]
defp drop_constraint_expr(_, _, _),
Expand Down
2 changes: 2 additions & 0 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,8 @@ if Code.ensure_loaded?(Postgrex) do
reference_on_update(ref.on_update), validate(ref.validate)]
end

defp drop_reference_expr({%Reference{} = ref, _opts}, table, name),
do: drop_reference_expr(ref, table, name)
defp drop_reference_expr(%Reference{} = ref, table, name),
do: ["DROP CONSTRAINT ", reference_name(ref, table, name), ", "]
defp drop_reference_expr(_, _, _),
Expand Down
3 changes: 3 additions & 0 deletions lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,9 @@ if Code.ensure_loaded?(Tds) do

defp default_expr(_table, _name, :error), do: []

defp drop_constraint_from_expr({%Reference{} = ref, _opts}, table, name, stm_prefix),
do: drop_constraint_from_expr(ref, table, name, stm_prefix)

defp drop_constraint_from_expr(%Reference{} = ref, table, name, stm_prefix) do
[stm_prefix, "DROP CONSTRAINT ", reference_name(ref, table, name), "; "]
end
Expand Down
3 changes: 3 additions & 0 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ defmodule Ecto.Adapters.MyXQLTest do
{:modify, :permalink_id, %Reference{table: :permalinks}, null: false},
{:modify, :status, :string, from: :integer},
{:modify, :user_id, :integer, from: %Reference{table: :users}},
{:modify, :space_id, :integer, null: true, from: {%Reference{table: :author}, null: false}},
{:modify, :group_id, %Reference{table: :groups, column: :gid}, from: %Reference{table: :groups}},
{:modify, :status, :string, [null: false, size: 100, from: {:integer, null: true, size: 50}]},
{:remove, :summary},
Expand All @@ -1433,6 +1434,8 @@ defmodule Ecto.Adapters.MyXQLTest do
MODIFY `status` varchar(255),
DROP FOREIGN KEY `posts_user_id_fkey`,
MODIFY `user_id` integer,
DROP FOREIGN KEY `posts_space_id_fkey`,
MODIFY `space_id` integer NULL,
DROP FOREIGN KEY `posts_group_id_fkey`,
MODIFY `group_id` BIGINT UNSIGNED,
ADD CONSTRAINT `posts_group_id_fkey` FOREIGN KEY (`group_id`) REFERENCES `groups`(`gid`),
Expand Down
4 changes: 4 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ defmodule Ecto.Adapters.PostgresTest do
{:modify, :permalink_id, %Reference{table: :permalinks}, null: false},
{:modify, :status, :string, from: :integer},
{:modify, :user_id, :integer, from: %Reference{table: :users}},
{:modify, :space_id, :integer, null: true, from: {%Reference{table: :author}, null: false}},
{:modify, :group_id, %Reference{table: :groups, column: :gid}, from: %Reference{table: :groups}},
{:modify, :status, :string, [null: false, size: 100, from: {:integer, null: true, size: 50}]},
{:remove, :summary},
Expand Down Expand Up @@ -1781,6 +1782,9 @@ defmodule Ecto.Adapters.PostgresTest do
ALTER COLUMN "status" TYPE varchar(255),
DROP CONSTRAINT "posts_user_id_fkey",
ALTER COLUMN "user_id" TYPE integer,
DROP CONSTRAINT "posts_space_id_fkey",
ALTER COLUMN "space_id" TYPE integer,
ALTER COLUMN "space_id" DROP NOT NULL,
DROP CONSTRAINT "posts_group_id_fkey",
ALTER COLUMN "group_id" TYPE bigint,
ADD CONSTRAINT "posts_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "groups"("gid"),
Expand Down
2 changes: 2 additions & 0 deletions test/ecto/adapters/tds_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ defmodule Ecto.Adapters.TdsTest do
{:modify, :permalink_id, %Reference{table: :permalinks}, null: false},
{:modify, :status, :string, from: :integer},
{:modify, :user_id, :integer, from: %Reference{table: :users}},
{:modify, :space_id, :integer, null: true, from: {%Reference{table: :author}, null: false}},
{:modify, :group_id, %Reference{table: :groups, column: :gid},
from: %Reference{table: :groups}},
{:remove, :summary}
Expand All @@ -1410,6 +1411,7 @@ defmodule Ecto.Adapters.TdsTest do
ALTER TABLE [posts] ADD CONSTRAINT [posts_permalink_id_fkey] FOREIGN KEY ([permalink_id]) REFERENCES [permalinks]([id]) ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE [posts] ALTER COLUMN [status] nvarchar(255); ALTER TABLE [posts] DROP CONSTRAINT [posts_user_id_fkey];
ALTER TABLE [posts] ALTER COLUMN [user_id] integer;
ALTER TABLE [posts] DROP CONSTRAINT [posts_space_id_fkey]; ALTER TABLE [posts] ALTER COLUMN [space_id] integer NULL;
ALTER TABLE [posts] DROP CONSTRAINT [posts_group_id_fkey];
ALTER TABLE [posts] ALTER COLUMN [group_id] BIGINT;
ALTER TABLE [posts] ADD CONSTRAINT [posts_group_id_fkey] FOREIGN KEY ([group_id]) REFERENCES [groups]([gid]) ON DELETE NO ACTION ON UPDATE NO ACTION;
Expand Down
2 changes: 2 additions & 0 deletions test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,13 @@ defmodule Ecto.MigrationTest do
modify :extension, :text, from: :string
modify :author, :string, null: false, from: :text
modify :title, :string, null: false, size: 100, from: {:text, null: true, size: 255}
modify :author_id, references(:authors), null: true, from: {references(:authors), null: false}
end
flush()

assert last_command() ==
{:alter, %Table{name: "posts"}, [
{:modify, :author_id, %Reference{table: "authors"}, [from: {%Reference{table: "authors"}, null: true}, null: false]},
{:modify, :title, :text, [from: {:string, null: false, size: 100}, null: true, size: 255]},
{:modify, :author, :text, [from: :string, null: false]},
{:modify, :extension, :string, from: :text},
Expand Down

0 comments on commit a4c70f5

Please sign in to comment.