Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down