diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index 35c84ce..fa6226b 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -117,6 +117,7 @@ defmodule AshPostgres.MigrationGenerator do {primary_key, identities} = merge_primary_keys(existing_snapshot, snapshots) attributes = Enum.flat_map(snapshots, & &1.attributes) + count = Enum.count(snapshots) snapshot_identities = snapshots @@ -125,7 +126,7 @@ defmodule AshPostgres.MigrationGenerator do new_snapshot = %{ snapshot - | attributes: merge_attributes(attributes, snapshot.table), + | attributes: merge_attributes(attributes, snapshot.table, count), identities: snapshot_identities } @@ -167,19 +168,23 @@ defmodule AshPostgres.MigrationGenerator do end) end - defp merge_attributes(attributes, table) do + defp merge_attributes(attributes, table, count) do attributes |> Enum.group_by(& &1.name) |> Enum.map(fn {_name, [attribute]} -> - attribute + if count > 1 do + %{attribute | allow_nil?: true} + else + attribute + end {name, attributes} -> %{ name: name, type: merge_types(Enum.map(attributes, & &1.type), name, table), default: merge_defaults(Enum.map(attributes, & &1.default)), - allow_nil?: Enum.any?(attributes, & &1.allow_nil?), + allow_nil?: Enum.any?(attributes, & &1.allow_nil?) || Enum.count(attributes) < count, references: merge_references(Enum.map(attributes, & &1.references), name, table), primary_key?: false } diff --git a/lib/migration_generator/operation.ex b/lib/migration_generator/operation.ex index d074092..b84507a 100644 --- a/lib/migration_generator/operation.ex +++ b/lib/migration_generator/operation.ex @@ -268,7 +268,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do } } ) do - "references(:#{table}, type: #{inspect(type)}, column: #{inspect(destination_field)}, prefix(): \"public\")" + "references(:#{table}, type: #{inspect(type)}, column: #{inspect(destination_field)}, prefix: \"public\")" end defp reference( diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index c77a23c..705ab2c 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -348,6 +348,39 @@ defmodule AshPostgres.MigrationGeneratorTest do assert File.read!(file2) =~ ~S[add :foobar, :text] end + + test "when an attribute exists only on some of the resources that use the same table, it isn't marked as null: false" do + defposts do + attributes do + uuid_primary_key(:id) + attribute(:title, :string) + attribute(:example, :string, allow_nil?: false) + end + end + + defposts Post2 do + attributes do + uuid_primary_key(:id) + end + end + + defapi([Post, Post2]) + + AshPostgres.MigrationGenerator.generate(Api, + snapshot_path: "test_snapshots_path", + migration_path: "test_migration_path", + quiet: true, + format: false + ) + + assert [_file1, file2] = + Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")) + + 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