Skip to content

Commit 97bd185

Browse files
committed
feat: support cast_in_query?/0 and source
1 parent e6ff1d8 commit 97bd185

File tree

8 files changed

+88
-41
lines changed

8 files changed

+88
-41
lines changed

.github/workflows/elixir.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
matrix:
1919
otp: ["23"]
2020
elixir: ["1.11.0"]
21-
ash: ["master", "1.50.20"]
21+
ash: ["master", "1.51.0"]
2222
pg_version: ["9.6", "11"]
2323
env:
2424
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

lib/migration_generator/migration_generator.ex

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

lib/migration_generator/operation.ex

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
9292
end
9393

9494
[
95-
"add #{inspect(attribute.name)}",
95+
"add #{inspect(attribute.source)}",
9696
"references(:#{table}",
9797
[
9898
"column: #{inspect(destination_field)}",
@@ -129,7 +129,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
129129
end
130130

131131
[
132-
"add #{inspect(attribute.name)}",
132+
"add #{inspect(attribute.source)}",
133133
"references(:#{table}",
134134
[
135135
"column: #{inspect(destination_field)}",
@@ -163,7 +163,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
163163
end
164164

165165
[
166-
"add #{inspect(attribute.name)}",
166+
"add #{inspect(attribute.source)}",
167167
inspect(attribute.type),
168168
maybe_add_default(attribute.default),
169169
maybe_add_primary_key(attribute.primary_key?),
@@ -191,7 +191,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
191191
end
192192

193193
[
194-
"add #{inspect(attribute.name)}",
194+
"add #{inspect(attribute.source)}",
195195
"references(:#{table}",
196196
[
197197
"column: #{inspect(destination_field)}",
@@ -227,7 +227,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
227227
end
228228

229229
[
230-
"add #{inspect(attribute.name)}",
230+
"add #{inspect(attribute.source)}",
231231
"references(:#{table}",
232232
[
233233
"column: #{inspect(destination_field)}",
@@ -257,7 +257,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
257257
end
258258

259259
[
260-
"add #{inspect(attribute.name)}",
260+
"add #{inspect(attribute.source)}",
261261
"references(:#{table}",
262262
[
263263
"column: #{inspect(destination_field)}",
@@ -277,7 +277,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
277277

278278
def up(%{attribute: %{type: :bigint, default: "nil", generated?: true} = attribute}) do
279279
[
280-
"add #{inspect(attribute.name)}",
280+
"add #{inspect(attribute.source)}",
281281
":bigserial",
282282
maybe_add_null(attribute.allow_nil?),
283283
maybe_add_primary_key(attribute.primary_key?)
@@ -287,7 +287,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
287287

288288
def up(%{attribute: %{type: :integer, default: "nil", generated?: true} = attribute}) do
289289
[
290-
"add #{inspect(attribute.name)}",
290+
"add #{inspect(attribute.source)}",
291291
":serial",
292292
maybe_add_null(attribute.allow_nil?),
293293
maybe_add_primary_key(attribute.primary_key?)
@@ -302,7 +302,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
302302
end
303303

304304
[
305-
"add #{inspect(attribute.name)}",
305+
"add #{inspect(attribute.source)}",
306306
"#{inspect(attribute.type)}",
307307
maybe_add_null(attribute.allow_nil?),
308308
maybe_add_default(attribute.default),
@@ -370,7 +370,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
370370
inspect(attribute.type)
371371
end
372372

373-
"modify #{inspect(attribute.name)}, #{type_or_reference}#{alter_opts(attribute, old_attribute)}"
373+
"modify #{inspect(attribute.source)}, #{type_or_reference}#{alter_opts(attribute, old_attribute)}"
374374
end
375375

376376
defp reference(
@@ -533,11 +533,11 @@ defmodule AshPostgres.MigrationGenerator.Operation do
533533
]
534534

535535
def up(%{old_attribute: old_attribute, new_attribute: new_attribute, table: table}) do
536-
"rename table(:#{table}), #{inspect(old_attribute.name)}, to: #{inspect(new_attribute.name)}"
536+
"rename table(:#{table}), #{inspect(old_attribute.source)}, to: #{inspect(new_attribute.source)}"
537537
end
538538

539539
def down(%{new_attribute: old_attribute, old_attribute: new_attribute, table: table}) do
540-
"rename table(:#{table}), #{inspect(old_attribute.name)}, to: #{inspect(new_attribute.name)}"
540+
"rename table(:#{table}), #{inspect(old_attribute.source)}, to: #{inspect(new_attribute.source)}"
541541
end
542542
end
543543

@@ -549,19 +549,19 @@ defmodule AshPostgres.MigrationGenerator.Operation do
549549
"""
550550
# Attribute removal has been commented out to avoid data loss. See the migration generator documentation for more
551551
# If you uncomment this, be sure to also uncomment the corresponding attribute *addition* in the `down` migration
552-
# remove #{inspect(attribute.name)}
552+
# remove #{inspect(attribute.source)}
553553
"""
554554
end
555555

556556
def up(%{attribute: attribute}) do
557-
"remove #{inspect(attribute.name)}"
557+
"remove #{inspect(attribute.source)}"
558558
end
559559

560560
def down(%{attribute: attribute, multitenancy: multitenancy, commented?: true}) do
561561
prefix = """
562562
# This is the `down` migration of the statement:
563563
#
564-
# remove #{inspect(attribute.name)}
564+
# remove #{inspect(attribute.source)}
565565
#
566566
"""
567567

lib/types/types.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ defmodule AshPostgres.Types do
1313

1414
def parameterized_type(type, constraints) do
1515
if Ash.Type.ash_type?(type) do
16-
parameterized_type(Ash.Type.ecto_type(type), constraints)
16+
if Ash.Type.cast_in_query?(type) do
17+
parameterized_type(Ash.Type.ecto_type(type), constraints)
18+
else
19+
:any
20+
end
1721
else
1822
if is_atom(type) && :erlang.function_exported(type, :type, 1) do
1923
{:parameterized, type, constraints || []}

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ defmodule AshPostgres.MixProject do
9797
{:ecto, github: "elixir-ecto/ecto", branch: "master", override: true},
9898
{:jason, "~> 1.0"},
9999
{:postgrex, ">= 0.0.0"},
100-
{:ash, ash_version("~> 1.50 and >= 1.50.20")},
100+
{:ash, ash_version("~> 1.51 and >= 1.51.0")},
101101
{:git_ops, "~> 2.4.5", only: :dev},
102102
{:ex_doc, "~> 0.22", only: :dev, runtime: false},
103103
{:ex_check, "~> 0.11.0", only: :dev},

test/filter_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ defmodule AshPostgres.FilterTest do
419419
|> Ash.Query.filter(status_enum == ^"open")
420420
|> Api.read_one!()
421421
end
422+
423+
test "it allows simple filtering without casting" do
424+
Post
425+
|> Ash.Changeset.new(status_enum_no_cast: "open")
426+
|> Api.create!()
427+
428+
assert %{status_enum_no_cast: :open} =
429+
Post
430+
|> Ash.Query.filter(status_enum_no_cast == ^"open")
431+
|> Api.read_one!()
432+
end
422433
end
423434

424435
describe "atom filters" do

test/support/resources/post.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ defmodule AshPostgres.Test.Post do
5353
attribute(:decimal, :decimal, default: Decimal.new(0))
5454
attribute(:status, AshPostgres.Test.Types.Status)
5555
attribute(:status_enum, AshPostgres.Test.Types.StatusEnum)
56+
attribute(:status_enum_no_cast, AshPostgres.Test.Types.StatusEnumNoCast, source: :status_enum)
5657
create_timestamp(:created_at)
5758
end
5859

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule AshPostgres.Test.Types.StatusEnumNoCast do
2+
@moduledoc false
3+
use Ash.Type.Enum, values: [:open, :closed]
4+
5+
def storage_type, do: :status
6+
7+
def cast_in_query?, do: false
8+
end

0 commit comments

Comments
 (0)