Skip to content
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
10 changes: 8 additions & 2 deletions lib/ash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4242,6 +4242,12 @@ defmodule Ash do
end

@transaction_opts_schema [
tenant: [
type: {:protocol, Ash.ToTenant},
doc: """
The tenant, placed on the transaction reason for data layers that select a connection per tenant.
"""
],
timeout: [
type: :timeout,
doc: """
Expand Down Expand Up @@ -4328,7 +4334,7 @@ defmodule Ash do
resource_or_resources,
func,
opts[:timeout],
%{type: :custom, metadata: %{}}
%{type: :custom, metadata: %{}, tenant: opts[:tenant]}
) do
if opts[:return_notifications?] do
notifications = Process.delete(:ash_notifications) || []
Expand Down Expand Up @@ -4431,7 +4437,7 @@ defmodule Ash do
resource_or_resources,
func,
opts[:timeout],
%{type: :custom, metadata: %{}},
%{type: :custom, metadata: %{}, tenant: opts[:tenant]},
rollback_on_error?: true
) do
if opts[:return_notifications?] do
Expand Down
1 change: 1 addition & 0 deletions lib/ash/actions/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ defmodule Ash.Actions.Action do
input: input,
actor: opts[:actor]
},
tenant: input.tenant,
data_layer_context: input.context[:data_layer] || %{}
},
rollback_on_error?: false
Expand Down
2 changes: 2 additions & 0 deletions lib/ash/actions/create/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ defmodule Ash.Actions.Create.Bulk do
action: action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || %{}
},
rollback_on_error?: false
Expand Down Expand Up @@ -671,6 +672,7 @@ defmodule Ash.Actions.Create.Bulk do
action: action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || context
},
rollback_on_error?: false
Expand Down
3 changes: 3 additions & 0 deletions lib/ash/actions/destroy/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ defmodule Ash.Actions.Destroy.Bulk do
action: atomic_changeset.action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || %{}
},
rollback_on_error?: false
Expand Down Expand Up @@ -539,6 +540,7 @@ defmodule Ash.Actions.Destroy.Bulk do
action: action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || %{}
},
rollback_on_error?: false
Expand Down Expand Up @@ -1644,6 +1646,7 @@ defmodule Ash.Actions.Destroy.Bulk do
action: action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || context
},
rollback_on_error?: false
Expand Down
1 change: 1 addition & 0 deletions lib/ash/actions/read/read.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ defmodule Ash.Actions.Read do
resource: query.resource,
action: query.action.name
},
tenant: query.tenant,
data_layer_context: query.context[:data_layer]
},
rollback_on_error?: false
Expand Down
3 changes: 3 additions & 0 deletions lib/ash/actions/update/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ defmodule Ash.Actions.Update.Bulk do
action: atomic_changeset.action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || %{}
},
rollback_on_error?: false
Expand Down Expand Up @@ -594,6 +595,7 @@ defmodule Ash.Actions.Update.Bulk do
action: action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || %{}
},
rollback_on_error?: false
Expand Down Expand Up @@ -2021,6 +2023,7 @@ defmodule Ash.Actions.Update.Bulk do
action: action.name,
actor: opts[:actor]
},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || context
},
rollback_on_error?: false
Expand Down
7 changes: 6 additions & 1 deletion lib/ash/actions/update/update_many.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ defmodule Ash.Actions.Update.UpdateMany do
atomic_notifications ++ manual_notifications}
end,
nil,
%{type: :bulk_update, metadata: %{resource: resource, action: action.name}}
%{
type: :bulk_update,
metadata: %{resource: resource, action: action.name},
tenant: opts[:tenant],
data_layer_context: opts[:data_layer_context] || %{}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were also missing data_layer_context for Update Many, so I've added it here as well

}
)
|> case do
{:ok, {records, errors, notifications}} -> {records, errors, notifications}
Expand Down
8 changes: 3 additions & 5 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4685,11 +4685,9 @@ defmodule Ash.Changeset do
end
end,
changeset.timeout || :infinity,
Map.put(
opts[:transaction_metadata],
:data_layer_context,
changeset.context[:data_layer] || %{}
),
opts[:transaction_metadata]
|> Map.put(:data_layer_context, changeset.context[:data_layer] || %{})
|> Map.put(:tenant, changeset.tenant),
rollback_on_error?: false
)
|> case do
Expand Down
18 changes: 16 additions & 2 deletions lib/ash/data_layer/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Ash.DataLayer do
%{
required(:type) => :create,
required(:metadata) => %{resource: Ash.Resource.t(), action: atom},
optional(:tenant) => term(),
optional(:data_layer_context) => %{}
}
| %{
Expand All @@ -26,6 +27,7 @@ defmodule Ash.DataLayer do
record: Ash.Resource.Record.t(),
actor: term()
},
optional(:tenant) => term(),
optional(:data_layer_context) => %{}
}
| %{
Expand All @@ -36,6 +38,7 @@ defmodule Ash.DataLayer do
record: Ash.Resource.Record.t(),
actor: term()
},
optional(:tenant) => term(),
optional(:data_layer_context) => %{}
}
| %{
Expand All @@ -45,6 +48,7 @@ defmodule Ash.DataLayer do
query: Ash.Query.t(),
actor: term()
},
optional(:tenant) => term(),
optional(:data_layer_context) => %{}
}
| %{
Expand All @@ -55,6 +59,7 @@ defmodule Ash.DataLayer do
action: atom,
actor: term()
},
optional(:tenant) => term(),
optional(:data_layer_context) => %{}
}
| %{
Expand All @@ -64,10 +69,19 @@ defmodule Ash.DataLayer do
flow: module(),
actor: term()
},
optional(:tenant) => term(),
optional(:data_layer_context) => %{}
}
| %{required(:type) => :custom, required(:metadata) => map()}
| %{required(:type) => atom, required(:metadata) => map()}
| %{
required(:type) => :custom,
required(:metadata) => map(),
optional(:tenant) => term()
}
| %{
required(:type) => atom,
required(:metadata) => map(),
optional(:tenant) => term()
}

@type combination_type :: :union | :union_all | :intersection

Expand Down
Loading