Skip to content

add tenant to ash bindings #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2025
Merged
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
7 changes: 7 additions & 0 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3073,6 +3073,13 @@ defmodule AshPostgres.DataLayer do
changeset.context
)
|> pkey_filter(changeset.data)
|> then(fn query ->
Map.put(
query,
:__ash_bindings__,
Map.put_new(query.__ash_bindings__, :tenant, changeset.tenant)
)
end)

changeset =
Ash.Changeset.set_context(changeset, %{
Expand Down
31 changes: 15 additions & 16 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ defmodule AshPostgres.MixProject do
defp deps do
[
{:ash, ash_version("~> 3.4 and >= 3.4.69")},
# {:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.68")},
{:ash_sql, github: "ash-project/ash_sql"},
{:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.72")},
{:igniter, "~> 0.5 and >= 0.5.16", optional: true},
{:ecto_sql, "~> 3.12"},
{:ecto, "~> 3.12 and >= 3.12.1"},
Expand Down Expand Up @@ -209,24 +208,24 @@ defmodule AshPostgres.MixProject do
end
end

# defp ash_sql_version(default_version) do
# case System.get_env("ASH_SQL_VERSION") do
# nil ->
# default_version
defp ash_sql_version(default_version) do
case System.get_env("ASH_SQL_VERSION") do
nil ->
default_version

# "local" ->
# [path: "../ash_sql", override: true]
"local" ->
[path: "../ash_sql", override: true]

# "main" ->
# [git: "https://github.com/ash-project/ash_sql.git"]
"main" ->
[git: "https://github.com/ash-project/ash_sql.git"]

# version when is_binary(version) ->
# "~> #{version}"
version when is_binary(version) ->
"~> #{version}"

# version ->
# version
# end
# end
version ->
version
end
end

defp aliases do
[
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%{
"ash": {:hex, :ash, "3.5.6", "2f187150110b4c280c8551ad411f56d95862fcb37c067a0b8b94eb682bcc43e8", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.5.24 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.11", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.29 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, ">= 0.2.6 and < 1.0.0-0", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d0d9aeb5aacfdc12253fae1e7e4720991868c5f69632c2766afb03b2b1830f55"},
"ash_sql": {:hex, :ash_sql, "0.2.72", "3c353fd3361257310864dce131acd93e407379439cf8667d7ddb4cba99a3fe2c", [:mix], [{:ash, "~> 3.5", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "0a802299580b8406258f9d72ec8c2a39b4d4b9ad49a2631d47a15d3290d7b0eb"},
"benchee": {:hex, :benchee, "1.4.0", "9f1f96a30ac80bab94faad644b39a9031d5632e517416a8ab0a6b0ac4df124ce", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "299cd10dd8ce51c9ea3ddb74bb150f93d25e968f93e4c1fa31698a8e4fa5d715"},
"ash_sql": {:git, "https://github.com/ash-project/ash_sql.git", "91d990700d18055759004a60cc35f3074da3d1a9", []},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"credo": {:hex, :credo, "1.7.12", "9e3c20463de4b5f3f23721527fcaf16722ec815e70ff6c60b86412c695d426c1", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8493d45c656c5427d9c729235b99d498bd133421f3e0a683e5c1b561471291e5"},
"db_connection": {:hex, :db_connection, "2.7.0", "b99faa9291bb09892c7da373bb82cba59aefa9b36300f6145c5f201c7adf48ec", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dcf08f31b2701f857dfc787fbad78223d61a32204f217f15e881dd93e4bdd3ff"},
Expand Down
47 changes: 24 additions & 23 deletions test/atomics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AshPostgres.AtomicsTest do
alias AshPostgres.Test.Comment

use AshPostgres.RepoCase, async: false

alias AshPostgres.Test.Invite
alias AshPostgres.Test.Post
alias AshPostgres.Test.User
Expand Down Expand Up @@ -367,29 +368,29 @@ defmodule AshPostgres.AtomicsTest do
|> Ash.update!()
end

# assert_raise Ash.Error.Invalid, ~r/Can only update if Post has no comments/, fn ->
# post
# |> Ash.Changeset.new()
# |> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
# |> Ash.Changeset.for_update(:update_if_no_comments_non_atomic, %{title: "bar"})
# |> Ash.update!()
# end

# assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
# post
# |> Ash.Changeset.new()
# |> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
# |> Ash.Changeset.for_destroy(:destroy_if_no_comments_non_atomic, %{})
# |> Ash.destroy!()
# end

# assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
# post
# |> Ash.Changeset.new()
# |> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
# |> Ash.Changeset.for_destroy(:destroy_if_no_comments, %{})
# |> Ash.destroy!()
# end
assert_raise Ash.Error.Invalid, ~r/Can only update if Post has no comments/, fn ->
post
|> Ash.Changeset.new()
|> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_update(:update_if_no_comments_non_atomic, %{title: "bar"})
|> Ash.update!()
end

assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.new()
|> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_destroy(:destroy_if_no_comments_non_atomic, %{})
|> Ash.destroy!()
end

assert_raise Ash.Error.Invalid, ~r/Can only delete if Post has no comments/, fn ->
post
|> Ash.Changeset.new()
|> Ash.Changeset.put_context(:aggregate, unquote(aggregate))
|> Ash.Changeset.for_destroy(:destroy_if_no_comments, %{})
|> Ash.destroy!()
end
end
end
)
Expand Down
Loading