@@ -229,14 +229,14 @@ defmodule AshPostgres.MigrationGenerator do
229229 end
230230
231231 defp add_order_to_operation ( % { attribute: attribute } = op , attributes ) do
232- order = Enum . find_index ( attributes , & ( & 1 . name == attribute . name ) )
232+ order = Enum . find_index ( attributes , & ( & 1 . source == attribute . source ) )
233233 attribute = Map . put ( attribute , :order , order )
234234
235235 % { op | attribute: attribute }
236236 end
237237
238238 defp add_order_to_operation ( % { new_attribute: attribute } = op , attributes ) do
239- order = Enum . find_index ( attributes , & ( & 1 . name == attribute . name ) )
239+ order = Enum . find_index ( attributes , & ( & 1 . source == attribute . source ) )
240240 attribute = Map . put ( attribute , :order , order )
241241
242242 % { op | new_attribute: attribute }
@@ -330,7 +330,7 @@ defmodule AshPostgres.MigrationGenerator do
330330 new_snapshot
331331 | attributes:
332332 Enum . map ( new_snapshot . attributes , fn attribute ->
333- if attribute . name in primary_key do
333+ if attribute . source in primary_key do
334334 % { attribute | primary_key?: true }
335335 else
336336 % { attribute | primary_key?: false }
@@ -346,8 +346,8 @@ defmodule AshPostgres.MigrationGenerator do
346346 attributes
347347 |> Enum . with_index ( )
348348 |> Enum . map ( fn { attr , i } -> Map . put ( attr , :order , i ) end )
349- |> Enum . group_by ( & & 1 . name )
350- |> Enum . map ( fn { name , attributes } ->
349+ |> Enum . group_by ( & & 1 . source )
350+ |> Enum . map ( fn { source , attributes } ->
351351 size =
352352 attributes
353353 |> Enum . map ( & & 1 . size )
@@ -361,13 +361,13 @@ defmodule AshPostgres.MigrationGenerator do
361361 end
362362
363363 % {
364- name: name ,
365- type: merge_types ( Enum . map ( attributes , & & 1 . type ) , name , table ) ,
364+ source: source ,
365+ type: merge_types ( Enum . map ( attributes , & & 1 . type ) , source , table ) ,
366366 size: size ,
367367 default: merge_defaults ( Enum . map ( attributes , & & 1 . default ) ) ,
368368 allow_nil?: Enum . any? ( attributes , & & 1 . allow_nil? ) || Enum . count ( attributes ) < count ,
369369 generated?: Enum . any? ( attributes , & & 1 . generated? ) ,
370- references: merge_references ( Enum . map ( attributes , & & 1 . references ) , name , table ) ,
370+ references: merge_references ( Enum . map ( attributes , & & 1 . references ) , source , table ) ,
371371 primary_key?: false ,
372372 order: attributes |> Enum . map ( & & 1 . order ) |> Enum . min ( )
373373 }
@@ -534,7 +534,7 @@ defmodule AshPostgres.MigrationGenerator do
534534 defp pkey_names ( attributes ) do
535535 attributes
536536 |> Enum . filter ( & & 1 . primary_key? )
537- |> Enum . map ( & & 1 . name )
537+ |> Enum . map ( & & 1 . source )
538538 |> Enum . sort ( )
539539 end
540540
@@ -1263,12 +1263,12 @@ defmodule AshPostgres.MigrationGenerator do
12631263 defp attribute_operations ( snapshot , old_snapshot , opts ) do
12641264 attributes_to_add =
12651265 Enum . reject ( snapshot . attributes , fn attribute ->
1266- Enum . find ( old_snapshot . attributes , & ( & 1 . name == attribute . name ) )
1266+ Enum . find ( old_snapshot . attributes , & ( & 1 . source == attribute . source ) )
12671267 end )
12681268
12691269 attributes_to_remove =
12701270 Enum . reject ( old_snapshot . attributes , fn attribute ->
1271- Enum . find ( snapshot . attributes , & ( & 1 . name == attribute . name ) )
1271+ Enum . find ( snapshot . attributes , & ( & 1 . source == attribute . source ) )
12721272 end )
12731273
12741274 { attributes_to_add , attributes_to_remove , attributes_to_rename } =
@@ -1278,7 +1278,7 @@ defmodule AshPostgres.MigrationGenerator do
12781278 snapshot . attributes
12791279 |> Enum . map ( fn attribute ->
12801280 { attribute ,
1281- Enum . find ( old_snapshot . attributes , & ( & 1 . name == attribute . name && & 1 != attribute ) ) }
1281+ Enum . find ( old_snapshot . attributes , & ( & 1 . source == attribute . source && & 1 != attribute ) ) }
12821282 end )
12831283 |> Enum . filter ( & elem ( & 1 , 1 ) )
12841284
@@ -1439,7 +1439,7 @@ defmodule AshPostgres.MigrationGenerator do
14391439 defp resolve_renames ( _table , [ ] , removing , _opts ) , do: { [ ] , removing , [ ] }
14401440
14411441 defp resolve_renames ( table , [ adding ] , [ removing ] , opts ) do
1442- if renaming_to? ( table , removing . name , adding . name , opts ) do
1442+ if renaming_to? ( table , removing . source , adding . source , opts ) do
14431443 { [ ] , [ ] , [ { adding , removing } ] }
14441444 else
14451445 { [ adding ] , [ removing ] , [ ] }
@@ -1476,9 +1476,9 @@ defmodule AshPostgres.MigrationGenerator do
14761476
14771477 defp renaming? ( table , removing , opts ) do
14781478 if opts . no_shell? do
1479- raise "Unimplemented: cannot determine: Are you renaming #{ table } .#{ removing . name } ? without shell input"
1479+ raise "Unimplemented: cannot determine: Are you renaming #{ table } .#{ removing . source } ? without shell input"
14801480 else
1481- Mix . shell ( ) . yes? ( "Are you renaming #{ table } .#{ removing . name } ?" )
1481+ Mix . shell ( ) . yes? ( "Are you renaming #{ table } .#{ removing . source } ?" )
14821482 end
14831483 end
14841484
@@ -1491,7 +1491,7 @@ defmodule AshPostgres.MigrationGenerator do
14911491 defp get_new_attribute ( adding , tries ) do
14921492 name =
14931493 Mix . shell ( ) . prompt (
1494- "What are you renaming it to?: #{ Enum . map_join ( adding , ", " , & & 1 . name ) } "
1494+ "What are you renaming it to?: #{ Enum . map_join ( adding , ", " , & & 1 . source ) } "
14951495 )
14961496
14971497 name =
@@ -1501,7 +1501,7 @@ defmodule AshPostgres.MigrationGenerator do
15011501 nil
15021502 end
15031503
1504- case Enum . find ( adding , & ( to_string ( & 1 . name ) == name ) ) do
1504+ case Enum . find ( adding , & ( to_string ( & 1 . source ) == name ) ) do
15051505 nil -> get_new_attribute ( adding , tries - 1 )
15061506 new_attribute -> new_attribute
15071507 end
@@ -1546,7 +1546,12 @@ defmodule AshPostgres.MigrationGenerator do
15461546 end )
15471547 |> Map . update! ( :attributes , fn attributes ->
15481548 Enum . map ( attributes , fn attribute ->
1549- if attribute . name == relationship . destination_field do
1549+ destination_field_source =
1550+ relationship . destination
1551+ |> Ash.Resource.Info . attribute ( relationship . destination_field )
1552+ |> Map . get ( :source )
1553+
1554+ if attribute . source == destination_field_source do
15501555 source_attribute =
15511556 Ash.Resource.Info . attribute ( relationship . source , relationship . source_field )
15521557
@@ -1657,7 +1662,9 @@ defmodule AshPostgres.MigrationGenerator do
16571662
16581663 resource
16591664 |> Ash.Resource.Info . attributes ( )
1660- |> Enum . map ( & Map . take ( & 1 , [ :name , :type , :default , :allow_nil? , :generated? , :primary_key? ] ) )
1665+ |> Enum . map (
1666+ & Map . take ( & 1 , [ :name , :source , :type , :default , :allow_nil? , :generated? , :primary_key? ] )
1667+ )
16611668 |> Enum . map ( fn attribute ->
16621669 default = default ( attribute , repo )
16631670
@@ -1687,6 +1694,7 @@ defmodule AshPostgres.MigrationGenerator do
16871694 |> Map . put ( :default , default )
16881695 |> Map . put ( :size , size )
16891696 |> Map . put ( :type , type )
1697+ |> Map . delete ( :name )
16901698 end )
16911699 |> Enum . map ( fn attribute ->
16921700 references = find_reference ( resource , table , attribute )
@@ -1697,9 +1705,15 @@ defmodule AshPostgres.MigrationGenerator do
16971705
16981706 defp find_reference ( resource , table , attribute ) do
16991707 Enum . find_value ( Ash.Resource.Info . relationships ( resource ) , fn relationship ->
1700- if attribute . name == relationship . source_field && relationship . type == :belongs_to &&
1708+ source_field_name =
1709+ relationship . source
1710+ |> Ash.Resource.Info . attribute ( relationship . source_field )
1711+ |> Map . get ( :source )
1712+
1713+ if attribute . source == source_field_name && relationship . type == :belongs_to &&
17011714 foreign_key? ( relationship ) do
1702- configured_reference = configured_reference ( resource , table , attribute . name , relationship )
1715+ configured_reference =
1716+ configured_reference ( resource , table , attribute . source , relationship )
17031717
17041718 % {
17051719 destination_field: relationship . destination_field ,
@@ -1917,8 +1931,14 @@ defmodule AshPostgres.MigrationGenerator do
19171931 { other , nil }
19181932 end
19191933
1934+ attribute =
1935+ if Map . has_key? ( attribute , :name ) do
1936+ Map . put ( attribute , :source , String . to_atom ( attribute . name ) )
1937+ else
1938+ Map . update! ( attribute , :source , & String . to_atom / 1 )
1939+ end
1940+
19201941 attribute
1921- |> Map . update! ( :name , & String . to_atom / 1 )
19221942 |> Map . put ( :type , type )
19231943 |> Map . put ( :size , size )
19241944 |> Map . put_new ( :default , "nil" )
@@ -1936,7 +1956,10 @@ defmodule AshPostgres.MigrationGenerator do
19361956 |> Map . put_new ( :on_update , nil )
19371957 |> Map . update! ( :on_delete , & ( & 1 && String . to_atom ( & 1 ) ) )
19381958 |> Map . update! ( :on_update , & ( & 1 && String . to_atom ( & 1 ) ) )
1939- |> Map . put ( :name , Map . get ( references , :name ) || "#{ table } _#{ attribute . name } _fkey" )
1959+ |> Map . put (
1960+ :name ,
1961+ Map . get ( references , :name ) || "#{ table } _#{ attribute . source } _fkey"
1962+ )
19401963 |> Map . put_new ( :multitenancy , % {
19411964 attribute: nil ,
19421965 strategy: nil ,
0 commit comments