Skip to content

Commit

Permalink
Revert "Allow literal struct in insert_all + add recent changes to …
Browse files Browse the repository at this point in the history
…docs (#4…" (#4492)

This reverts commit 92f07d8.
  • Loading branch information
josevalim authored Aug 13, 2024
1 parent 551b2eb commit fc179e0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 51 deletions.
30 changes: 3 additions & 27 deletions lib/ecto/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1490,19 +1490,16 @@ defmodule Ecto.Repo do
## Source query
A query can be given instead of a list with entries. This query needs to select
into a map, struct or source containing only keys that are available as writeable
columns in the schema. This will query and insert the values all inside one query,
without another round trip to the application.
into a map containing only keys that are available as writeable columns in the
schema. This will query and insert the values all inside one query, without
another round trip to the application.
## Examples
# List of keyword lists
MyRepo.insert_all(Post, [[title: "My first post"], [title: "My second post"]])
# List of maps
MyRepo.insert_all(Post, [%{title: "My first post"}, %{title: "My second post"}])
# Query selecting into a map
query = from p in Post,
join: c in assoc(p, :comments),
select: %{
Expand All @@ -1511,29 +1508,8 @@ defmodule Ecto.Repo do
interactions: sum(p.likes) + count(c.id)
},
group_by: p.author_id
MyRepo.insert_all(AuthorStats, query)
query = from p in Post, select: map(p, [:author_id, :title])
MyRepo.insert_all(PostAuthors, query)
# Query selecting into a struct
query = from p in Post, select: %Post{author_id: p.author_id, title: p.title}
MyRepo.insert_all(PostAuthors, query)
query = from p in Post, select: struct(p, [:author_id, :title])
MyRepo.insert_all(PostAuthors, query)
# Query selecting into a source
query = from p in Post, select: p
MyRepo.insert_all(PostBackup, query)
query = from p in Post, select: %{p | public: false}
MyRepo.insert_all(PostBackup, query)
query = from p in Post, join: c in assoc(p, :comments), where: p.public, select: c
MyRepo.insert_all(PublicCommentBackup, query)
## Upserts
`c:insert_all/3` provides upserts (update or inserts) via the `:on_conflict`
Expand Down
9 changes: 4 additions & 5 deletions lib/ecto/repo/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ defmodule Ecto.Repo.Schema do
%Ecto.Query.SelectExpr{expr: {:&, _, [_ix]}, fields: fields} ->
Enum.map(fields, &insert_all_select_dump!(&1))

%Ecto.Query.SelectExpr{expr: {:%, _, [_, {:%{}, _, args}]}} ->
Enum.map(args, fn {field, _} -> insert_all_select_dump!(field, dumper) end)

_ ->
raise ArgumentError, """
cannot generate a fields list for insert_all from the given source query:
Expand All @@ -157,8 +154,10 @@ defmodule Ecto.Repo.Schema do
* A single `struct/2` or several `struct/2` expressions combined with `select_merge`
* A source such as `p` in the query `from p in Post`
* A single literal map or several literal maps combined with `select_merge`. If
combining several literal maps, there cannot be any overwrites to keys containing
query interpolations.
combining several literal maps, there cannot be any query interpolations
except in the last `select_merge`. Consider using `Ecto.Query.exclude/2`
to rebuild the select expression from scratch if you need multiple `select_merge`
statements with interpolations
All keys must exist in the schema that is being inserted into
"""
Expand Down
19 changes: 0 additions & 19 deletions test/ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -670,25 +670,6 @@ defmodule Ecto.RepoTest do
assert ["ten"] = params
end

test "takes query selecting on struct" do
threshold = "ten"

query =
from s in MySchema,
where: s.x > ^threshold,
select: %MySchema{
x: s.x,
y: "bar",
z: nil
}

TestRepo.insert_all(MySchema, query)

assert_received {:insert_all, %{source: "my_schema"}, {%Ecto.Query{} = query, params}}
assert [{{:., _, [{:&, [], [0]}, :x]}, _, []}, "bar", nil] = query.select.fields
assert ["ten"] = params
end

test "takes query selecting on struct/2" do
query =
from s in MySchema,
Expand Down

0 comments on commit fc179e0

Please sign in to comment.