@@ -25,7 +25,7 @@ defmodule Pow.Ecto.Schema.Migration do
25
25
create table(:<%= schema.table %><%= if schema.binary_id do %>, primary_key: false<% end %>) do
26
26
<%= if schema.binary_id do %> add :id, :binary_id, primary_key: true
27
27
<% 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] %>
29
29
<% end %>
30
30
timestamps()
31
31
end
@@ -66,6 +66,7 @@ defmodule Pow.Ecto.Schema.Migration do
66
66
migration_attrs = migration_attrs ( attrs )
67
67
binary_id = opts [ :binary_id ]
68
68
migration_defaults = defaults ( migration_attrs )
69
+ reference_defaults = reference_defaults ( migration_attrs )
69
70
{ assocs , attrs } = partition_attrs ( context_base , migration_attrs )
70
71
indexes = migration_indexes ( indexes , table )
71
72
@@ -76,6 +77,7 @@ defmodule Pow.Ecto.Schema.Migration do
76
77
binary_id: binary_id ,
77
78
attrs: attrs ,
78
79
migration_defaults: migration_defaults ,
80
+ reference_defaults: reference_defaults ,
79
81
assocs: assocs ,
80
82
indexes: indexes
81
83
}
@@ -85,6 +87,7 @@ defmodule Pow.Ecto.Schema.Migration do
85
87
attrs
86
88
|> Enum . reject ( & is_virtual? / 1 )
87
89
|> Enum . map ( & to_migration_attr / 1 )
90
+ |> Enum . map ( & to_reference_attr / 1 )
88
91
end
89
92
90
93
defp is_virtual? ( { _name , _type } ) , do: false
@@ -99,27 +102,46 @@ defmodule Pow.Ecto.Schema.Migration do
99
102
to_migration_attr ( { name , type } )
100
103
end
101
104
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 )
103
109
104
- { name , type , ", #{ defaults } " }
110
+ defp to_reference_attr ( { name , { :references , source } , defaults } ) do
111
+ { name , { :references , source , ", on_delete: :nothing" } , defaults }
105
112
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 }
106
117
107
118
defp defaults ( attrs ) do
108
119
Enum . map ( attrs , fn { key , _value , defaults } ->
109
120
{ key , defaults }
110
121
end )
111
122
end
112
123
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
+
113
135
defp partition_attrs ( context_base , attrs ) do
114
136
{ assocs , attrs } =
115
137
Enum . split_with ( attrs , fn
116
- { _ , { :references , _ } , _ } -> true
138
+ { _ , { :references , _ , _ } , _ } -> true
117
139
_ -> false
118
140
end )
119
141
120
142
attrs = Enum . map ( attrs , fn { key_id , type , _defaults } -> { key_id , type } end )
121
143
assocs =
122
- Enum . map ( assocs , fn { key_id , { :references , source } , _ } ->
144
+ Enum . map ( assocs , fn { key_id , { :references , source , _ } , _ } ->
123
145
key = String . replace ( Atom . to_string ( key_id ) , "_id" , "" )
124
146
context = Macro . camelize ( source )
125
147
schema = Macro . camelize ( key )
0 commit comments