-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Introduce Ecto.Adapters.SQL.Stage #2028
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
018cc65
Introduce Ecto.Adapters.SQL.Stage
fishcakez 57d2a19
Split SQL stage start_link to 3 functions
fishcakez cf90836
Update Postgrex/DBConnection/GenStage/Flow
fishcakez 2879f85
Move GenStage API to Ecto.Repo (via Ecto.Adapter.Stage)
fishcakez 56df49d
Update DBConnection/Postgrex and introduce start_consumer
fishcakez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| defmodule Ecto.Integration.StageTest do | ||
| use Ecto.Integration.Case, async: true | ||
|
|
||
| alias Ecto.Integration.TestRepo | ||
| alias Ecto.Integration.Post | ||
| alias Ecto.Integration.Comment | ||
| import Ecto.Query | ||
|
|
||
| test "stream empty" do | ||
| {:ok, stage} = TestRepo.start_producer(Post) | ||
| assert to_list(stage) === [] | ||
|
|
||
| {:ok, stage} = TestRepo.start_producer(from p in Post) | ||
| assert to_list(stage) == [] | ||
| end | ||
|
|
||
| test "stream without schema" do | ||
| %Post{} = TestRepo.insert!(%Post{title: "title1"}) | ||
| %Post{} = TestRepo.insert!(%Post{title: "title2"}) | ||
|
|
||
| query = from(p in "posts", order_by: p.title, select: p.title) | ||
| {:ok, stage} = TestRepo.start_producer(query) | ||
|
|
||
| assert to_list(stage) == ["title1", "title2"] | ||
| end | ||
|
|
||
| test "stream with assoc" do | ||
| p1 = TestRepo.insert!(%Post{title: "1"}) | ||
|
|
||
| %Comment{id: cid1} = TestRepo.insert!(%Comment{text: "1", post_id: p1.id}) | ||
| %Comment{id: cid2} = TestRepo.insert!(%Comment{text: "2", post_id: p1.id}) | ||
|
|
||
| {:ok, stage} = TestRepo.start_producer(Ecto.assoc(p1, :comments)) | ||
| assert [c1, c2] = to_list(stage) | ||
|
|
||
| assert c1.id == cid1 | ||
| assert c2.id == cid2 | ||
| end | ||
|
|
||
| test "stream with preload" do | ||
| p1 = TestRepo.insert!(%Post{title: "1"}) | ||
| p2 = TestRepo.insert!(%Post{title: "2"}) | ||
| TestRepo.insert!(%Post{title: "3"}) | ||
|
|
||
| %Comment{id: cid1} = TestRepo.insert!(%Comment{text: "1", post_id: p1.id}) | ||
| %Comment{id: cid2} = TestRepo.insert!(%Comment{text: "2", post_id: p1.id}) | ||
| %Comment{id: cid3} = TestRepo.insert!(%Comment{text: "3", post_id: p2.id}) | ||
| %Comment{id: cid4} = TestRepo.insert!(%Comment{text: "4", post_id: p2.id}) | ||
|
|
||
| query = from(p in Post, preload: [:comments], select: p) | ||
| {:ok, stage} = TestRepo.start_producer(query, [max_rows: 2]) | ||
|
|
||
| assert [p1, p2, p3] = stage |> to_list() |> sort_by_id() | ||
|
|
||
| assert [%Comment{id: ^cid1}, %Comment{id: ^cid2}] = p1.comments |> sort_by_id | ||
| assert [%Comment{id: ^cid3}, %Comment{id: ^cid4}] = p2.comments |> sort_by_id | ||
| assert [] = p3.comments | ||
| end | ||
|
|
||
| test "insert entries" do | ||
| {:ok, stage} = TestRepo.start_consumer(Post) | ||
|
|
||
| mon = Process.monitor(stage) | ||
|
|
||
| [[title: "hello"], [title: "world"]] | ||
| |> Flow.from_enumerable() | ||
| |> Flow.into_stages([{stage, [cancel: :transient, max_demand: 1]}]) | ||
|
|
||
| assert_receive {:DOWN, ^mon, _, _, :normal} | ||
|
|
||
| assert [%Post{title: "hello"}, %Post{title: "world"}] = TestRepo.all(Post) | ||
| end | ||
|
|
||
| defp to_list(stage) do | ||
| stage | ||
| |> Flow.from_stage() | ||
| |> Enum.to_list() | ||
| end | ||
|
|
||
| defp sort_by_id(values) do | ||
| Enum.sort_by(values, &(&1.id)) | ||
| end | ||
| end | ||
This file contains hidden or 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,32 @@ | ||
| defmodule Ecto.Adapter.Stage do | ||
| @moduledoc """ | ||
| Specifies the adapter `GenStage` API. | ||
| """ | ||
|
|
||
| @type flat_map :: ({integer, list | nil} -> list) | ||
| @type insert_all :: (list -> integer) | ||
| @type options :: Keyword.t | ||
|
|
||
| @doc """ | ||
| Starts and links to a `GenStage` producer. | ||
|
|
||
| The producer executes a query and returns the result to consumer(s). | ||
|
|
||
| See `Ecto.Repo.start_producer/2`. | ||
| """ | ||
| @callback start_producer(repo :: Ecto.Repo.t, Ecto.Adapter.query_meta, query, params :: list(), Ecto.Adapter.process | nil, flat_map, options) :: | ||
| GenServer.on_start when | ||
| query: {:nocache, Ecto.Adapter.prepared} | | ||
| {:cached, (Ecto.Adapter.prepared -> :ok), Ecto.Adapter.cached} | | ||
| {:cache, (Ecto.Adapter.cached -> :ok), Ecto.Adapter.prepared} | ||
|
|
||
| @doc """ | ||
| Starts and links to a `GenStage` consumer. | ||
|
|
||
| The consumers inserts the entries it receives for the schema. | ||
|
|
||
| See `Ecto.Repo.start_consumer/2` | ||
| """ | ||
| @callback start_consumer(repo :: Ecto.Repo.t, insert_all, options) :: | ||
| GenServer.on_start | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a function call when a pipeline is only one function long