Skip to content

Commit

Permalink
Add references defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Jul 6, 2020
1 parent 18954e6 commit 0baf797
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
32 changes: 27 additions & 5 deletions lib/pow/ecto/schema/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Pow.Ecto.Schema.Migration do
create table(:<%= schema.table %><%= if schema.binary_id do %>, primary_key: false<% end %>) do
<%= if schema.binary_id do %> add :id, :binary_id, primary_key: true
<% end %><%= for {k, v} <- schema.attrs do %> add <%= inspect k %>, <%= inspect v %><%= schema.migration_defaults[k] %>
<% 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 %>)
<% 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] %>
<% end %>
timestamps()
end
Expand Down Expand Up @@ -66,6 +66,7 @@ defmodule Pow.Ecto.Schema.Migration do
migration_attrs = migration_attrs(attrs)
binary_id = opts[:binary_id]
migration_defaults = defaults(migration_attrs)
reference_defaults = reference_defaults(migration_attrs)
{assocs, attrs} = partition_attrs(context_base, migration_attrs)
indexes = migration_indexes(indexes, table)

Expand All @@ -76,6 +77,7 @@ defmodule Pow.Ecto.Schema.Migration do
binary_id: binary_id,
attrs: attrs,
migration_defaults: migration_defaults,
reference_defaults: reference_defaults,
assocs: assocs,
indexes: indexes
}
Expand All @@ -85,6 +87,7 @@ defmodule Pow.Ecto.Schema.Migration do
attrs
|> Enum.reject(&is_virtual?/1)
|> Enum.map(&to_migration_attr/1)
|> Enum.map(&to_reference_attr/1)
end

defp is_virtual?({_name, _type}), do: false
Expand All @@ -99,27 +102,46 @@ defmodule Pow.Ecto.Schema.Migration do
to_migration_attr({name, type})
end
defp to_migration_attr({name, type, defaults}) do
defaults = Enum.map_join(defaults, ", ", fn {k, v} -> "#{k}: #{v}" end)
{name, type, ", #{join_defaults(defaults)}"}
end

defp join_defaults(defaults), do: Enum.map_join(defaults, ", ", fn {k, v} -> "#{k}: #{inspect v}" end)

{name, type, ", #{defaults}"}
defp to_reference_attr({name, {:references, source}, defaults}) do
{name, {:references, source, ", on_delete: :nothing"}, defaults}
end
defp to_reference_attr({name, {:references, source, reference_defaults}, defaults}) do
{name, {:references, source, ", #{join_defaults(reference_defaults)}"}, defaults}
end
defp to_reference_attr({name, type, defaults}), do: {name, type, defaults}

defp defaults(attrs) do
Enum.map(attrs, fn {key, _value, defaults} ->
{key, defaults}
end)
end

defp reference_defaults(attrs) do
attrs
|> Enum.filter(fn
{_key, {:references, _source, _reference_defaults}, _defaults} -> true
_any -> false
end)
|> Enum.map(fn {key, {:references, _source, reference_defaults}, _defaults} ->
{key, reference_defaults}
end)
end

defp partition_attrs(context_base, attrs) do
{assocs, attrs} =
Enum.split_with(attrs, fn
{_, {:references, _}, _} -> true
{_, {:references, _, _}, _} -> true
_ -> false
end)

attrs = Enum.map(attrs, fn {key_id, type, _defaults} -> {key_id, type} end)
assocs =
Enum.map(assocs, fn {key_id, {:references, source}, _} ->
Enum.map(assocs, fn {key_id, {:references, source, _}, _} ->
key = String.replace(Atom.to_string(key_id), "_id", "")
context = Macro.camelize(source)
schema = Macro.camelize(key)
Expand Down
2 changes: 1 addition & 1 deletion lib/pow/extension/ecto/schema/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Pow.Extension.Ecto.Schema.Migration do
def change do
alter table(:<%= schema.table %>) do<%= for {k, v} <- schema.attrs do %>
add <%= inspect k %>, <%= inspect v %><%= schema.migration_defaults[k] %><% 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 %>)<% end %>
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 %>
end
<%= for index <- schema.indexes do %>
<%= index %><% end %>
Expand Down
6 changes: 6 additions & 0 deletions test/pow/ecto/schema/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ defmodule Pow.Ecto.Schema.MigrationTest do
content = Migration.gen(Migration.new(Pow, "users", attrs: [{:organization_id, {:references, "organizations"}}], binary_id: true))
assert content =~ "add :organization_id, references(\"organizations\", on_delete: :nothing, type: :binary_id)"

content = Migration.gen(Migration.new(Pow, "users", attrs: [{:organization_id, {:references, "organizations"}, null: false}]))
assert content =~ "add :organization_id, references(\"organizations\", on_delete: :nothing), null: false"

content = Migration.gen(Migration.new(Pow, "users", attrs: [{:organization_id, {:references, "organizations", on_delete: :delete_all}}]))
assert content =~ "add :organization_id, references(\"organizations\", on_delete: :delete_all)"

content = Migration.gen(Migration.new(Pow, "users", indexes: [{:key, true}]))
assert content =~ "create unique_index(:users, [:key])"

Expand Down

0 comments on commit 0baf797

Please sign in to comment.