fix: typo in references for multitenancy

fix: `null: true` when attr isn't on all resources for a table
This commit is contained in:
Zach Daniel 2021-03-02 11:38:12 -05:00
parent 42ab86de69
commit ef9ddd9c54
3 changed files with 43 additions and 5 deletions

View file

@ -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
}

View file

@ -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(

View file

@ -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