From 9feb228fe4ec29cebd915230dcb9b60915befd60 Mon Sep 17 00:00:00 2001 From: Nacy Dev Date: Sun, 1 Nov 2026 01:45:03 +0700 Subject: [PATCH] fix: include manual create actions in has_create_action?/1 --- .../migration_generator.ex | 2 +- test/migration_generator_test.exs | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 861727fd..37c2de65 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -3876,7 +3876,7 @@ defmodule AshPostgres.MigrationGenerator do defp has_create_action?(resource) do resource |> Ash.Resource.Info.actions() - |> Enum.any?(&(&1.type == :create && !&1.manual)) + |> Enum.any?(&(&1.type == :create)) end defp check_constraints(resource) do diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index f9990173..8f72ce9f 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -2511,6 +2511,86 @@ defmodule AshPostgres.MigrationGeneratorTest do refute File.read!(file2) =~ ~S[null: false] end + + test "when an attribute exists only on some of the resources that use the same table with manual create actions, it isn't marked as null: false", + %{snapshot_path: snapshot_path, migration_path: migration_path} do + defmodule ManualCreate do + use Ash.Resource.ManualCreate + + def create(changeset, _opts, _context) do + Ash.Changeset.apply_attributes(changeset) + end + end + + defresource Post do + postgres do + table "posts" + repo(AshPostgres.TestRepo) + + custom_indexes do + index(["id"]) + index(["id"], unique: true, name: "test_unique_index") + end + end + + actions do + defaults([:read, :update, :destroy]) + + create :create do + manual(ManualCreate) + end + end + + attributes do + uuid_primary_key(:id) + attribute(:title, :string, public?: true) + attribute(:example, :string, allow_nil?: false, public?: true) + end + end + + defresource Post2 do + postgres do + table "posts" + repo(AshPostgres.TestRepo) + + custom_indexes do + index(["id"]) + index(["id"], unique: true, name: "test_unique_index") + end + end + + actions do + defaults([:read, :update, :destroy]) + + create :create do + manual(ManualCreate) + end + end + + attributes do + uuid_primary_key(:id) + end + end + + defdomain([Post, Post2]) + + AshPostgres.MigrationGenerator.generate(Domain, + snapshot_path: snapshot_path, + migration_path: migration_path, + quiet: true, + format: false, + auto_name: true + ) + + assert [_file1, file2] = + Enum.sort(Path.wildcard("#{migration_path}/**/*_migrate_resources*.exs")) + |> Enum.reject(&String.contains?(&1, "extensions")) + + assert File.read!(file2) =~ + ~S[add :example, :text] <> "\n" + + refute File.read!(file2) =~ ~S[null: false] + end end describe "auto incrementing integer, when generated" do