Skip to content

Commit 0baf797

Browse files
committed
Add references defaults
1 parent 18954e6 commit 0baf797

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

lib/pow/ecto/schema/migration.ex

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Pow.Ecto.Schema.Migration do
2525
create table(:<%= schema.table %><%= if schema.binary_id do %>, primary_key: false<% end %>) do
2626
<%= if schema.binary_id do %> add :id, :binary_id, primary_key: true
2727
<% end %><%= for {k, v} <- schema.attrs do %> add <%= inspect k %>, <%= inspect v %><%= schema.migration_defaults[k] %>
28-
<% end %><%= for {_, i, _, s} <- schema.assocs do %> add <%= if(String.ends_with?(inspect(i), "_id"), do: inspect(i), else: inspect(i) <> "_id") %>, references(<%= inspect(s) %>, on_delete: :nothing<%= if schema.binary_id do %>, type: :binary_id<% end %>)
28+
<% end %><%= for {_, i, _, s} <- schema.assocs do %> add <%= if(String.ends_with?(inspect(i), "_id"), do: inspect(i), else: inspect(i) <> "_id") %>, references(<%= inspect(s) %><%= schema.reference_defaults[i] %><%= if schema.binary_id do %>, type: :binary_id<% end %>)<%= schema.migration_defaults[i] %>
2929
<% end %>
3030
timestamps()
3131
end
@@ -66,6 +66,7 @@ defmodule Pow.Ecto.Schema.Migration do
6666
migration_attrs = migration_attrs(attrs)
6767
binary_id = opts[:binary_id]
6868
migration_defaults = defaults(migration_attrs)
69+
reference_defaults = reference_defaults(migration_attrs)
6970
{assocs, attrs} = partition_attrs(context_base, migration_attrs)
7071
indexes = migration_indexes(indexes, table)
7172

@@ -76,6 +77,7 @@ defmodule Pow.Ecto.Schema.Migration do
7677
binary_id: binary_id,
7778
attrs: attrs,
7879
migration_defaults: migration_defaults,
80+
reference_defaults: reference_defaults,
7981
assocs: assocs,
8082
indexes: indexes
8183
}
@@ -85,6 +87,7 @@ defmodule Pow.Ecto.Schema.Migration do
8587
attrs
8688
|> Enum.reject(&is_virtual?/1)
8789
|> Enum.map(&to_migration_attr/1)
90+
|> Enum.map(&to_reference_attr/1)
8891
end
8992

9093
defp is_virtual?({_name, _type}), do: false
@@ -99,27 +102,46 @@ defmodule Pow.Ecto.Schema.Migration do
99102
to_migration_attr({name, type})
100103
end
101104
defp to_migration_attr({name, type, defaults}) do
102-
defaults = Enum.map_join(defaults, ", ", fn {k, v} -> "#{k}: #{v}" end)
105+
{name, type, ", #{join_defaults(defaults)}"}
106+
end
107+
108+
defp join_defaults(defaults), do: Enum.map_join(defaults, ", ", fn {k, v} -> "#{k}: #{inspect v}" end)
103109

104-
{name, type, ", #{defaults}"}
110+
defp to_reference_attr({name, {:references, source}, defaults}) do
111+
{name, {:references, source, ", on_delete: :nothing"}, defaults}
105112
end
113+
defp to_reference_attr({name, {:references, source, reference_defaults}, defaults}) do
114+
{name, {:references, source, ", #{join_defaults(reference_defaults)}"}, defaults}
115+
end
116+
defp to_reference_attr({name, type, defaults}), do: {name, type, defaults}
106117

107118
defp defaults(attrs) do
108119
Enum.map(attrs, fn {key, _value, defaults} ->
109120
{key, defaults}
110121
end)
111122
end
112123

124+
defp reference_defaults(attrs) do
125+
attrs
126+
|> Enum.filter(fn
127+
{_key, {:references, _source, _reference_defaults}, _defaults} -> true
128+
_any -> false
129+
end)
130+
|> Enum.map(fn {key, {:references, _source, reference_defaults}, _defaults} ->
131+
{key, reference_defaults}
132+
end)
133+
end
134+
113135
defp partition_attrs(context_base, attrs) do
114136
{assocs, attrs} =
115137
Enum.split_with(attrs, fn
116-
{_, {:references, _}, _} -> true
138+
{_, {:references, _, _}, _} -> true
117139
_ -> false
118140
end)
119141

120142
attrs = Enum.map(attrs, fn {key_id, type, _defaults} -> {key_id, type} end)
121143
assocs =
122-
Enum.map(assocs, fn {key_id, {:references, source}, _} ->
144+
Enum.map(assocs, fn {key_id, {:references, source, _}, _} ->
123145
key = String.replace(Atom.to_string(key_id), "_id", "")
124146
context = Macro.camelize(source)
125147
schema = Macro.camelize(key)

lib/pow/extension/ecto/schema/migration.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Pow.Extension.Ecto.Schema.Migration do
1111
def change do
1212
alter table(:<%= schema.table %>) do<%= for {k, v} <- schema.attrs do %>
1313
add <%= inspect k %>, <%= inspect v %><%= schema.migration_defaults[k] %><% end %><%= for {_, i, _, s} <- schema.assocs do %>
14-
add <%= if(String.ends_with?(inspect(i), "_id"), do: inspect(i), else: inspect(i) <> "_id") %>, references(<%= inspect(s) %>, on_delete: :nothing<%= if schema.binary_id do %>, type: :binary_id<% end %>)<% end %>
14+
add <%= if(String.ends_with?(inspect(i), "_id"), do: inspect(i), else: inspect(i) <> "_id") %>, references(<%= inspect(s) %><%= schema.reference_defaults[i] %><%= if schema.binary_id do %>, type: :binary_id<% end %>)<%= schema.migration_defaults[i] %><% end %>
1515
end
1616
<%= for index <- schema.indexes do %>
1717
<%= index %><% end %>

test/pow/ecto/schema/migration_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ defmodule Pow.Ecto.Schema.MigrationTest do
4949
content = Migration.gen(Migration.new(Pow, "users", attrs: [{:organization_id, {:references, "organizations"}}], binary_id: true))
5050
assert content =~ "add :organization_id, references(\"organizations\", on_delete: :nothing, type: :binary_id)"
5151

52+
content = Migration.gen(Migration.new(Pow, "users", attrs: [{:organization_id, {:references, "organizations"}, null: false}]))
53+
assert content =~ "add :organization_id, references(\"organizations\", on_delete: :nothing), null: false"
54+
55+
content = Migration.gen(Migration.new(Pow, "users", attrs: [{:organization_id, {:references, "organizations", on_delete: :delete_all}}]))
56+
assert content =~ "add :organization_id, references(\"organizations\", on_delete: :delete_all)"
57+
5258
content = Migration.gen(Migration.new(Pow, "users", indexes: [{:key, true}]))
5359
assert content =~ "create unique_index(:users, [:key])"
5460

0 commit comments

Comments
 (0)