Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe7d188

Browse files
authored
improvement: Preserve attribute order (ash-project#63)
1 parent5f27e32 commitfe7d188

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

‎lib/migration_generator/migration_generator.ex‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ defmodule AshPostgres.MigrationGenerator do
2424

2525
defgenerate(apis,opts\\[])do
2626
apis=List.wrap(apis)
27-
28-
opts=
29-
casestruct(__MODULE__,opts)do
30-
%{check_generated:true}=opts->
31-
%{opts|dry_run:true}
32-
33-
opts->
34-
opts
35-
end
27+
opts=opts(opts)
3628

3729
all_resources=Enum.flat_map(apis,&Ash.Api.resources/1)
3830

@@ -184,7 +176,10 @@ defmodule AshPostgres.MigrationGenerator do
184176
|>Enum.each(fn{repo,snapshots}->
185177
deduped=deduplicate_snapshots(snapshots,opts)
186178

187-
snapshots_with_operations=fetch_operations(deduped,opts)
179+
snapshots_with_operations=
180+
deduped
181+
|>fetch_operations(opts)
182+
|>Enum.map(&add_order_to_operations/1)
188183

189184
snapshots=Enum.map(snapshots_with_operations,&elem(&1,0))
190185

@@ -217,6 +212,28 @@ defmodule AshPostgres.MigrationGenerator do
217212
end)
218213
end
219214

215+
defpadd_order_to_operations({snapshot,operations})do
216+
operations_with_order=Enum.map(operations,&add_order_to_operation(&1,snapshot.attributes))
217+
218+
{snapshot,operations_with_order}
219+
end
220+
221+
defpadd_order_to_operation(%{attribute:attribute}=op,attributes)do
222+
order=Enum.find_index(attributes,&(&1.name==attribute.name))
223+
attribute=Map.put(attribute,:order,order)
224+
225+
%{op|attribute:attribute}
226+
end
227+
228+
defpadd_order_to_operation(%{new_attribute:attribute}=op,attributes)do
229+
order=Enum.find_index(attributes,&(&1.name==attribute.name))
230+
attribute=Map.put(attribute,:order,order)
231+
232+
%{op|new_attribute:attribute}
233+
end
234+
235+
defpadd_order_to_operation(op,_),do:op
236+
220237
defporganize_operations([]),do:[]
221238

222239
defporganize_operations(operations)do
@@ -314,6 +331,8 @@ defmodule AshPostgres.MigrationGenerator do
314331

315332
defpmerge_attributes(attributes,table,count)do
316333
attributes
334+
|>Enum.with_index()
335+
|>Enum.map(fn{attr,i}->Map.put(attr,:order,i)end)
317336
|>Enum.group_by(&&1.name)
318337
|>Enum.map(fn{name,attributes}->
319338
%{
@@ -323,9 +342,12 @@ defmodule AshPostgres.MigrationGenerator do
323342
allow_nil?:Enum.any?(attributes,&&1.allow_nil?)||Enum.count(attributes)<count,
324343
generated?:Enum.any?(attributes,&&1.generated?),
325344
references:merge_references(Enum.map(attributes,&&1.references),name,table),
326-
primary_key?:false
345+
primary_key?:false,
346+
order:attributes|>Enum.map(&&1.order)|>Enum.min()
327347
}
328348
end)
349+
|>Enum.sort(&(&1.order<&2.order))
350+
|>Enum.map(&Map.drop(&1,[:order]))
329351
end
330352

331353
defpmerge_references(references,name,table)do
@@ -834,6 +856,12 @@ defmodule AshPostgres.MigrationGenerator do
834856
sort_operations(rest,new_acc)
835857
end
836858

859+
defpafter?(
860+
%Operation.AddAttribute{attribute:%{order:l},table:table},
861+
%Operation.AddAttribute{attribute:%{order:r},table:table}
862+
),
863+
do:l>r
864+
837865
defpafter?(
838866
%Operation.RenameUniqueIndex{
839867
table:table
@@ -1009,6 +1037,9 @@ defmodule AshPostgres.MigrationGenerator do
10091037
whennotis_nil(references),
10101038
do:true
10111039

1040+
defpafter?(%Operation.AddCheckConstraint{},_),do:true
1041+
defpafter?(%Operation.RemoveCheckConstraint{},_),do:true
1042+
10121043
defpafter?(_,_),do:false
10131044

10141045
defpfetch_operations(snapshots,opts)do
@@ -1520,7 +1551,6 @@ defmodule AshPostgres.MigrationGenerator do
15201551

15211552
resource
15221553
|>Ash.Resource.Info.attributes()
1523-
|>Enum.sort_by(&&1.name)
15241554
|>Enum.map(&Map.take(&1,[:name,:type,:default,:allow_nil?,:generated?,:primary_key?]))
15251555
|>Enum.map(fnattribute->
15261556
default=default(attribute,repo)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp