From 4b64aff2f0fae665d1aabd0648c6703b0247f2d7 Mon Sep 17 00:00:00 2001 From: zimt28 <1764689+zimt28@users.noreply.github.com> Date: Wed, 28 Apr 2021 22:34:18 +0200 Subject: [PATCH 1/4] Preserve attribute order --- .../migration_generator.ex | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index be743754..60e47aca 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -24,15 +24,7 @@ defmodule AshPostgres.MigrationGenerator do def generate(apis, opts \\ []) do apis = List.wrap(apis) - - opts = - case struct(__MODULE__, opts) do - %{check_generated: true} = opts -> - %{opts | dry_run: true} - - opts -> - opts - end + opts = opts(opts) all_resources = Enum.flat_map(apis, &Ash.Api.resources/1) @@ -184,7 +176,10 @@ defmodule AshPostgres.MigrationGenerator do |> Enum.each(fn {repo, snapshots} -> deduped = deduplicate_snapshots(snapshots, opts) - snapshots_with_operations = fetch_operations(deduped, opts) + snapshots_with_operations = + deduped + |> fetch_operations(opts) + |> Enum.map(&add_order_to_operations/1) snapshots = Enum.map(snapshots_with_operations, &elem(&1, 0)) @@ -217,6 +212,28 @@ defmodule AshPostgres.MigrationGenerator do end) end + defp add_order_to_operations({snapshot, operations}) do + %{attributes: attributes} = snapshot + + operations_with_order = Enum.map(operations, &add_order_to_operation(&1, attributes)) + + {snapshot, operations_with_order} + end + + defp add_order_to_operation(%{attribute: attribute} = op, attributes) do + order = Enum.find_index(attributes, &(&1.name == attribute.name)) + attribute = Map.put(attribute, :order, order) + + %{op | attribute: attribute} + end + + defp add_order_to_operation(%{new_attribute: attribute} = op, attributes) do + order = Enum.find_index(attributes, &(&1.name == attribute.name)) + attribute = Map.put(attribute, :order, order) + + %{op | new_attribute: attribute} + end + defp organize_operations([]), do: [] defp organize_operations(operations) do @@ -314,6 +331,8 @@ defmodule AshPostgres.MigrationGenerator do defp merge_attributes(attributes, table, count) do attributes + |> Enum.with_index() + |> Enum.map(fn {attr, i} -> Map.put(attr, :order, i) end) |> Enum.group_by(& &1.name) |> Enum.map(fn {name, attributes} -> %{ @@ -323,9 +342,12 @@ defmodule AshPostgres.MigrationGenerator do allow_nil?: Enum.any?(attributes, & &1.allow_nil?) || Enum.count(attributes) < count, generated?: Enum.any?(attributes, & &1.generated?), references: merge_references(Enum.map(attributes, & &1.references), name, table), - primary_key?: false + primary_key?: false, + order: attributes |> Enum.map(& &1.order) |> Enum.min() } end) + |> Enum.sort(&(&1.order < &2.order)) + |> Enum.map(&Map.drop(&1, [:order])) end defp merge_references(references, name, table) do @@ -834,6 +856,12 @@ defmodule AshPostgres.MigrationGenerator do sort_operations(rest, new_acc) end + defp after?( + %Operation.AddAttribute{attribute: %{order: l}, table: table}, + %Operation.AddAttribute{attribute: %{order: r}, table: table} + ), + do: l > r + defp after?( %Operation.RenameUniqueIndex{ table: table @@ -1520,7 +1548,6 @@ defmodule AshPostgres.MigrationGenerator do resource |> Ash.Resource.Info.attributes() - |> Enum.sort_by(& &1.name) |> Enum.map(&Map.take(&1, [:name, :type, :default, :allow_nil?, :generated?, :primary_key?])) |> Enum.map(fn attribute -> default = default(attribute, repo) From f75dc0739d959c3c149114c502844590b23e3326 Mon Sep 17 00:00:00 2001 From: zimt28 <1764689+zimt28@users.noreply.github.com> Date: Wed, 28 Apr 2021 22:39:38 +0200 Subject: [PATCH 2/4] Update migration_generator.ex --- lib/migration_generator/migration_generator.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 60e47aca..67ee78ca 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -234,6 +234,8 @@ defmodule AshPostgres.MigrationGenerator do %{op | new_attribute: attribute} end + defp add_order_to_operation(op, _), do: op + defp organize_operations([]), do: [] defp organize_operations(operations) do From 967e82be48e846048d17fa341dace6b5280bd0df Mon Sep 17 00:00:00 2001 From: zimt28 <1764689+zimt28@users.noreply.github.com> Date: Wed, 28 Apr 2021 22:52:49 +0200 Subject: [PATCH 3/4] Update migration_generator.ex --- lib/migration_generator/migration_generator.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 67ee78ca..ad5d0e39 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -213,9 +213,7 @@ defmodule AshPostgres.MigrationGenerator do end defp add_order_to_operations({snapshot, operations}) do - %{attributes: attributes} = snapshot - - operations_with_order = Enum.map(operations, &add_order_to_operation(&1, attributes)) + operations_with_order = Enum.map(operations, &add_order_to_operation(&1, snapshot.attributes)) {snapshot, operations_with_order} end From 594db762501a80b975b42a498299949771a299ef Mon Sep 17 00:00:00 2001 From: zimt28 <1764689+zimt28@users.noreply.github.com> Date: Wed, 28 Apr 2021 23:01:57 +0200 Subject: [PATCH 4/4] Update migration_generator.ex --- lib/migration_generator/migration_generator.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index ad5d0e39..c46a7d06 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -1037,6 +1037,9 @@ defmodule AshPostgres.MigrationGenerator do when not is_nil(references), do: true + defp after?(%Operation.AddCheckConstraint{}, _), do: true + defp after?(%Operation.RemoveCheckConstraint{}, _), do: true + defp after?(_, _), do: false defp fetch_operations(snapshots, opts) do