Skip to content
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

poc: support virtual embeds #4216

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 9 additions & 1 deletion integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Ecto.Integration.RepoTest do

alias Ecto.Integration.Post
alias Ecto.Integration.Order
alias Ecto.Integration.Item
alias Ecto.Integration.User
alias Ecto.Integration.Comment
alias Ecto.Integration.Permalink
Expand Down Expand Up @@ -1300,10 +1301,17 @@ defmodule Ecto.Integration.RepoTest do
end

test "virtual field" do
assert %Post{id: id} = TestRepo.insert!(%Post{title: "1"})
assert %Post{id: id} = TestRepo.insert!(%Post{title: "1", temp: "special"})
assert TestRepo.get(Post, id).temp == "temp"
end

test "virtual embed" do
assert %Order{id: id, last_seen_item: %Item{reference: "1"}} =
TestRepo.insert!(%Order{last_seen_item: %Item{reference: "1"}})

assert %{last_seen_item: nil} = TestRepo.get(Order, id)
end

## Query syntax

defmodule Foo do
Expand Down
2 changes: 2 additions & 0 deletions integration_test/support/schemas.exs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,15 @@ defmodule Ecto.Integration.Order do
* Embedding one schema
* Preloading items inside embeds_many
* Preloading items inside embeds_one
* Virtual embeds
* Field source with json_extract_path

"""
use Ecto.Integration.Schema

schema "orders" do
field :metadata, :map, source: :meta
embeds_one :last_seen_item, Ecto.Integration.Item, virtual: true
embeds_one :item, Ecto.Integration.Item
embeds_many :items, Ecto.Integration.Item
belongs_to :permalink, Ecto.Integration.Permalink
Expand Down
17 changes: 14 additions & 3 deletions lib/ecto/embedded.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Ecto.Embedded do
:owner,
:related,
:on_cast,
virtual: false,
on_replace: :raise,
unique: true,
ordered: true
Expand Down Expand Up @@ -144,7 +145,11 @@ defmodule Ecto.Embedded do
@doc false
def prepare(changeset, embeds, adapter, repo_action) do
%{changes: changes, types: types, repo: repo} = changeset
prepare(Map.take(changes, embeds), types, adapter, repo, repo_action)
#non_virtual_embeds = Enum.reject(embeds, & &1.virtual)

changes
|> Map.take(embeds)
|> prepare(types, adapter, repo, repo_action)
end

defp prepare(embeds, _types, _adapter, _repo, _repo_action) when embeds == %{} do
Expand All @@ -153,8 +158,14 @@ defmodule Ecto.Embedded do

defp prepare(embeds, types, adapter, repo, repo_action) do
Enum.reduce embeds, embeds, fn {name, changeset_or_changesets}, acc ->
{:embed, embed} = Map.get(types, name)
Map.put(acc, name, prepare_each(embed, changeset_or_changesets, adapter, repo, repo_action))
case Map.get(types, name) do
{:embed, %{virtual: false} = embed} ->
prepared = prepare_each(embed, changeset_or_changesets, adapter, repo, repo_action)
Map.put(acc, name, prepared)

{:embed, %{virtual: true}} ->
acc
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,7 @@ defmodule Ecto.Schema do
Module.put_attribute(mod, :ecto_changeset_fields, {name, {:assoc, struct}})
end

@valid_embeds_one_options [:on_replace, :source]
@valid_embeds_one_options [:on_replace, :source, :virtual]

@doc false
def __embeds_one__(mod, name, schema, opts) when is_atom(schema) do
Expand All @@ -2099,7 +2099,7 @@ defmodule Ecto.Schema do
"`embeds_one/3` expects `schema` to be a module name, but received #{inspect(schema)}"
end

@valid_embeds_many_options [:on_replace, :source]
@valid_embeds_many_options [:on_replace, :source, :virtual]

@doc false
def __embeds_many__(mod, name, schema, opts) when is_atom(schema) do
Expand Down
3 changes: 2 additions & 1 deletion test/ecto/embedded_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Ecto.EmbeddedTest do
field :name, :string
embeds_one :profile, Profile, on_replace: :delete
embeds_one :post, Post
embeds_one :virtual_post, Post, virtual: true
embeds_many :posts, Post, on_replace: :delete
end
end
Expand All @@ -36,7 +37,7 @@ defmodule Ecto.EmbeddedTest do

test "__schema__" do
assert Author.__schema__(:embeds) ==
[:profile, :post, :posts]
[:profile, :post, :virtual_post, :posts]

assert Author.__schema__(:embed, :profile) ==
%Embedded{field: :profile, cardinality: :one, owner: Author, on_replace: :delete, related: Profile}
Expand Down
4 changes: 2 additions & 2 deletions test/ecto/query/planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ defmodule Ecto.Query.PlannerTest do

test "plan: generates a cache key" do
{_query, _cast_params, _dump_params, key} = plan(from(Post, []))
assert key == [:all, {:from, {"posts", Post, 111065921, "my_prefix"}, []}]
assert key == [:all, {:from, {"posts", Post, 23210922, "my_prefix"}, []}]

query =
from(
Expand All @@ -525,7 +525,7 @@ defmodule Ecto.Query.PlannerTest do
{:limit, {true, 1}},
{:where, [{:and, {:is_nil, [], [nil]}}, {:or, {:is_nil, [], [nil]}}]},
{:join, [{:inner, {"comments", Comment, 38292156, "world"}, true, ["join hint"]}]},
{:from, {"posts", Post, 111065921, "hello"}, ["hint"]},
{:from, {"posts", Post, 23210922, "hello"}, ["hint"]},
{:select, 1}]
end

Expand Down