From 3737b6353f82d5a72e31bee8d853ecb42e4457a1 Mon Sep 17 00:00:00 2001 From: Dan Schultzer Date: Sun, 24 May 2020 22:21:38 -0700 Subject: [PATCH] Add references defaults --- lib/pow/ecto/schema/migration.ex | 32 ++++++++++++++++++---- lib/pow/extension/ecto/schema/migration.ex | 2 +- test/pow/ecto/schema/migration_test.exs | 6 ++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/pow/ecto/schema/migration.ex b/lib/pow/ecto/schema/migration.ex index 2d19e9b8..4cf3f98c 100644 --- a/lib/pow/ecto/schema/migration.ex +++ b/lib/pow/ecto/schema/migration.ex @@ -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 @@ -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) @@ -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 } @@ -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 @@ -99,10 +102,18 @@ 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} -> @@ -110,16 +121,27 @@ defmodule Pow.Ecto.Schema.Migration do 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) diff --git a/lib/pow/extension/ecto/schema/migration.ex b/lib/pow/extension/ecto/schema/migration.ex index f687770f..b03363aa 100644 --- a/lib/pow/extension/ecto/schema/migration.ex +++ b/lib/pow/extension/ecto/schema/migration.ex @@ -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 %> diff --git a/test/pow/ecto/schema/migration_test.exs b/test/pow/ecto/schema/migration_test.exs index fe940998..bd7079d6 100644 --- a/test/pow/ecto/schema/migration_test.exs +++ b/test/pow/ecto/schema/migration_test.exs @@ -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])"