Skip to content

Commit 38d2f7e

Browse files
Fix nil prefix with references (#593)
--------- Co-authored-by: José Valim <jose.valim@gmail.com>
1 parent c0e97cb commit 38d2f7e

9 files changed

Lines changed: 33 additions & 14 deletions

File tree

Earthfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION 0.5
1+
VERSION 0.6
22

33
all:
44
ARG ELIXIR_BASE=1.15.6-erlang-25.3.2.6-alpine-3.17.4

lib/ecto/adapters/myxql/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ if Code.ensure_loaded?(MyXQL) do
13141314
quote_names(current_columns),
13151315
?),
13161316
" REFERENCES ",
1317-
quote_table(ref.prefix || table.prefix, ref.table),
1317+
quote_table(Keyword.get(ref.options, :prefix, table.prefix), ref.table),
13181318
?(,
13191319
quote_names(reference_columns),
13201320
?),

lib/ecto/adapters/postgres/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1706,7 +1706,7 @@ if Code.ensure_loaded?(Postgrex) do
17061706
"FOREIGN KEY (",
17071707
quote_names(current_columns),
17081708
") REFERENCES ",
1709-
quote_name(ref.prefix || table.prefix, ref.table),
1709+
quote_name(Keyword.get(ref.options, :prefix, table.prefix), ref.table),
17101710
?(,
17111711
quote_names(reference_columns),
17121712
?),

lib/ecto/adapters/tds/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ if Code.ensure_loaded?(Tds) do
15711571
reference_name(ref, table, name),
15721572
" FOREIGN KEY (#{quote_names(current_columns)})",
15731573
" REFERENCES ",
1574-
quote_table(ref.prefix || table.prefix, ref.table),
1574+
quote_table(Keyword.get(ref.options, :prefix, table.prefix), ref.table),
15751575
"(#{quote_names(reference_columns)})",
15761576
reference_on_delete(ref.on_delete),
15771577
reference_on_update(ref.on_update)

lib/ecto/migration.ex

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,14 @@ defmodule Ecto.Migration do
443443
on_update: :nothing,
444444
validate: true,
445445
with: [],
446-
match: nil
446+
match: nil,
447+
options: []
447448

449+
@typedoc """
450+
The reference struct.
451+
452+
The `:prefix` field is deprecated and should instead be stored in the `:options` field.
453+
"""
448454
@type t :: %__MODULE__{
449455
table: String.t(),
450456
prefix: atom | nil,
@@ -454,7 +460,8 @@ defmodule Ecto.Migration do
454460
on_update: atom,
455461
validate: boolean,
456462
with: list,
457-
match: atom | nil
463+
match: atom | nil,
464+
options: [{:prefix, atom | nil}]
458465
}
459466
end
460467

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

14181425
def references(table, opts) when is_binary(table) and is_list(opts) do
1419-
opts = Keyword.merge(foreign_key_repo_opts(), opts)
1426+
reference_options = Keyword.take(opts, [:prefix])
1427+
1428+
opts =
1429+
foreign_key_repo_opts()
1430+
|> Keyword.merge(opts)
1431+
|> Keyword.put(:options, reference_options)
1432+
14201433
reference = struct!(%Reference{table: table}, opts)
14211434
check_on_delete!(reference.on_delete)
14221435
check_on_update!(reference.on_update)

test/ecto/adapters/myxql_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,8 +1661,8 @@ defmodule Ecto.Adapters.MyXQLTest do
16611661
{:add, :category_3, %Reference{table: :categories, on_delete: :delete_all},
16621662
[null: false]},
16631663
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
1664-
{:add, :category_5, %Reference{table: :categories, prefix: :foo, on_delete: :nilify_all},
1665-
[]},
1664+
{:add, :category_5,
1665+
%Reference{table: :categories, options: [prefix: :foo], on_delete: :nilify_all}, []},
16661666
{:add, :category_6,
16671667
%Reference{table: :categories, with: [here: :there], on_delete: :nilify_all}, []}
16681668
]}

test/ecto/adapters/postgres_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,8 +2102,8 @@ defmodule Ecto.Adapters.PostgresTest do
21022102
[null: false]},
21032103
{:add, :category_9, %Reference{table: :categories, on_delete: :restrict}, []},
21042104
{:add, :category_10, %Reference{table: :categories, on_update: :restrict}, []},
2105-
{:add, :category_11, %Reference{table: :categories, prefix: "foo", on_update: :restrict},
2106-
[]},
2105+
{:add, :category_11,
2106+
%Reference{table: :categories, options: [prefix: "foo"], on_update: :restrict}, []},
21072107
{:add, :category_12, %Reference{table: :categories, with: [here: :there]}, []},
21082108
{:add, :category_13,
21092109
%Reference{

test/ecto/adapters/tds_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,8 +1457,8 @@ defmodule Ecto.Adapters.TdsTest do
14571457
{:add, :category_3, %Reference{table: :categories, on_delete: :delete_all},
14581458
[null: false]},
14591459
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
1460-
{:add, :category_5, %Reference{table: :categories, prefix: :foo, on_delete: :nilify_all},
1461-
[]},
1460+
{:add, :category_5,
1461+
%Reference{table: :categories, options: [prefix: :foo], on_delete: :nilify_all}, []},
14621462
{:add, :category_6,
14631463
%Reference{table: :categories, with: [here: :there], on_delete: :nilify_all}, []}
14641464
]}

test/ecto/migration_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ defmodule Ecto.MigrationTest do
130130
%Reference{table: "posts", column: :other, type: :uuid, prefix: nil}
131131

132132
assert references(:posts, type: :uuid, column: :other, prefix: :blog) ==
133-
%Reference{table: "posts", column: :other, type: :uuid, prefix: :blog}
133+
%Reference{
134+
table: "posts",
135+
column: :other,
136+
type: :uuid,
137+
prefix: :blog,
138+
options: [prefix: :blog]
139+
}
134140
end
135141

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

0 commit comments

Comments
 (0)