-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: allow specifying multi-column foreign keys
* improvement: add match_with option on references * improvement: add match_type option on references
- Loading branch information
Showing
7 changed files
with
379 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
lib/transformers/prevent_attribute_multitenancy_and_non_full_match_type.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule AshPostgres.Transformers.PreventAttributeMultitenancyAndNonFullMatchType do | ||
@moduledoc false | ||
use Spark.Dsl.Transformer | ||
alias Spark.Dsl.Transformer | ||
|
||
def transform(dsl) do | ||
if Transformer.get_option(dsl, [:multitenancy], :strategy) == :attribute do | ||
dsl | ||
|> AshPostgres.DataLayer.Info.references() | ||
|> Enum.filter(&(&1.match_type && &1.match_type != :full)) | ||
|> Enum.each(fn reference -> | ||
relationship = Ash.Resource.Info.relationship(dsl, reference.relationship) | ||
|
||
if uses_attribute_strategy?(relationship) and | ||
not targets_primary_key?(relationship) and | ||
not targets_multitenancy_attribute?(relationship) do | ||
resource = Transformer.get_persisted(dsl, :module) | ||
|
||
raise Spark.Error.DslError, | ||
module: resource, | ||
message: """ | ||
Unsupported match_type. | ||
The reference #{inspect(resource)}.#{reference.relationship} can't have `match_type: :#{reference.match_type}` because it's referencing another multitenant resource with attribute strategy using a non-primary key index, which requires using `match_type: :full`. | ||
""", | ||
path: [:postgres, :references, reference.relationship] | ||
else | ||
:ok | ||
end | ||
end) | ||
else | ||
{:ok, dsl} | ||
end | ||
end | ||
|
||
defp uses_attribute_strategy?(relationship) do | ||
Ash.Resource.Info.multitenancy_strategy(relationship.destination) == :attribute | ||
end | ||
|
||
defp targets_primary_key?(relationship) do | ||
Ash.Resource.Info.attribute( | ||
relationship.destination, | ||
relationship.destination_attribute | ||
) | ||
|> Map.fetch!(:primary_key?) | ||
end | ||
|
||
defp targets_multitenancy_attribute?(relationship) do | ||
relationship.destination_attribute == | ||
Ash.Resource.Info.multitenancy_attribute(relationship.destination) | ||
end | ||
end |
Oops, something went wrong.