Skip to content

Commit

Permalink
Fix nil prefix with references (#593)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: José Valim <[email protected]>
greg-rychlewski and josevalim authored Jan 26, 2024
1 parent c0e97cb commit 38d2f7e
Showing 9 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION 0.5
VERSION 0.6

all:
ARG ELIXIR_BASE=1.15.6-erlang-25.3.2.6-alpine-3.17.4
2 changes: 1 addition & 1 deletion lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
@@ -1314,7 +1314,7 @@ if Code.ensure_loaded?(MyXQL) do
quote_names(current_columns),
?),
" REFERENCES ",
quote_table(ref.prefix || table.prefix, ref.table),
quote_table(Keyword.get(ref.options, :prefix, table.prefix), ref.table),
?(,
quote_names(reference_columns),
?),
2 changes: 1 addition & 1 deletion lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
@@ -1706,7 +1706,7 @@ if Code.ensure_loaded?(Postgrex) do
"FOREIGN KEY (",
quote_names(current_columns),
") REFERENCES ",
quote_name(ref.prefix || table.prefix, ref.table),
quote_name(Keyword.get(ref.options, :prefix, table.prefix), ref.table),
?(,
quote_names(reference_columns),
?),
2 changes: 1 addition & 1 deletion lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
@@ -1571,7 +1571,7 @@ if Code.ensure_loaded?(Tds) do
reference_name(ref, table, name),
" FOREIGN KEY (#{quote_names(current_columns)})",
" REFERENCES ",
quote_table(ref.prefix || table.prefix, ref.table),
quote_table(Keyword.get(ref.options, :prefix, table.prefix), ref.table),
"(#{quote_names(reference_columns)})",
reference_on_delete(ref.on_delete),
reference_on_update(ref.on_update)
19 changes: 16 additions & 3 deletions lib/ecto/migration.ex
Original file line number Diff line number Diff line change
@@ -443,8 +443,14 @@ defmodule Ecto.Migration do
on_update: :nothing,
validate: true,
with: [],
match: nil
match: nil,
options: []

@typedoc """
The reference struct.
The `:prefix` field is deprecated and should instead be stored in the `:options` field.
"""
@type t :: %__MODULE__{
table: String.t(),
prefix: atom | nil,
@@ -454,7 +460,8 @@ defmodule Ecto.Migration do
on_update: atom,
validate: boolean,
with: list,
match: atom | nil
match: atom | nil,
options: [{:prefix, atom | nil}]
}
end

@@ -1416,7 +1423,13 @@ defmodule Ecto.Migration do
end

def references(table, opts) when is_binary(table) and is_list(opts) do
opts = Keyword.merge(foreign_key_repo_opts(), opts)
reference_options = Keyword.take(opts, [:prefix])

opts =
foreign_key_repo_opts()
|> Keyword.merge(opts)
|> Keyword.put(:options, reference_options)

reference = struct!(%Reference{table: table}, opts)
check_on_delete!(reference.on_delete)
check_on_update!(reference.on_update)
4 changes: 2 additions & 2 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
@@ -1661,8 +1661,8 @@ defmodule Ecto.Adapters.MyXQLTest do
{:add, :category_3, %Reference{table: :categories, on_delete: :delete_all},
[null: false]},
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
{:add, :category_5, %Reference{table: :categories, prefix: :foo, on_delete: :nilify_all},
[]},
{:add, :category_5,
%Reference{table: :categories, options: [prefix: :foo], on_delete: :nilify_all}, []},
{:add, :category_6,
%Reference{table: :categories, with: [here: :there], on_delete: :nilify_all}, []}
]}
4 changes: 2 additions & 2 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
@@ -2102,8 +2102,8 @@ defmodule Ecto.Adapters.PostgresTest do
[null: false]},
{:add, :category_9, %Reference{table: :categories, on_delete: :restrict}, []},
{:add, :category_10, %Reference{table: :categories, on_update: :restrict}, []},
{:add, :category_11, %Reference{table: :categories, prefix: "foo", on_update: :restrict},
[]},
{:add, :category_11,
%Reference{table: :categories, options: [prefix: "foo"], on_update: :restrict}, []},
{:add, :category_12, %Reference{table: :categories, with: [here: :there]}, []},
{:add, :category_13,
%Reference{
4 changes: 2 additions & 2 deletions test/ecto/adapters/tds_test.exs
Original file line number Diff line number Diff line change
@@ -1457,8 +1457,8 @@ defmodule Ecto.Adapters.TdsTest do
{:add, :category_3, %Reference{table: :categories, on_delete: :delete_all},
[null: false]},
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
{:add, :category_5, %Reference{table: :categories, prefix: :foo, on_delete: :nilify_all},
[]},
{:add, :category_5,
%Reference{table: :categories, options: [prefix: :foo], on_delete: :nilify_all}, []},
{:add, :category_6,
%Reference{table: :categories, with: [here: :there], on_delete: :nilify_all}, []}
]}
8 changes: 7 additions & 1 deletion test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
@@ -130,7 +130,13 @@ defmodule Ecto.MigrationTest do
%Reference{table: "posts", column: :other, type: :uuid, prefix: nil}

assert references(:posts, type: :uuid, column: :other, prefix: :blog) ==
%Reference{table: "posts", column: :other, type: :uuid, prefix: :blog}
%Reference{
table: "posts",
column: :other,
type: :uuid,
prefix: :blog,
options: [prefix: :blog]
}
end

@tag repo_config: [migration_foreign_key: [type: :uuid, column: :other, prefix: :blog]]

0 comments on commit 38d2f7e

Please sign in to comment.