diff options
author | Mark Felder <feld@FreeBSD.org> | 2020-06-17 12:50:06 -0500 |
---|---|---|
committer | Mark Felder <feld@FreeBSD.org> | 2020-06-17 12:50:06 -0500 |
commit | 3462d4b995a995ad523139a865b34a824e02674d (patch) | |
tree | 273a000ee55c9bb6a5344a507025600508227382 /lib/pleroma/repo.ex | |
parent | 96493da7bdab4ff4a51cbebf18df4127ddc47990 (diff) | |
parent | d772361e6209e6b5733e9fe52b3671cd222060b3 (diff) | |
download | pleroma-3462d4b995a995ad523139a865b34a824e02674d.tar.gz |
Merge branch 'develop' into issue/1855
Diffstat (limited to 'lib/pleroma/repo.ex')
-rw-r--r-- | lib/pleroma/repo.ex | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/pleroma/repo.ex b/lib/pleroma/repo.ex index f62138466..6d85d70bc 100644 --- a/lib/pleroma/repo.ex +++ b/lib/pleroma/repo.ex @@ -8,6 +8,7 @@ defmodule Pleroma.Repo do adapter: Ecto.Adapters.Postgres, migration_timestamps: [type: :naive_datetime_usec] + import Ecto.Query require Logger defmodule Instrumenter do @@ -78,6 +79,33 @@ defmodule Pleroma.Repo do :ok end end + + def chunk_stream(query, chunk_size) do + # We don't actually need start and end funcitons of resource streaming, + # but it seems to be the only way to not fetch records one-by-one and + # have individual records be the elements of the stream, instead of + # lists of records + Stream.resource( + fn -> 0 end, + fn + last_id -> + query + |> order_by(asc: :id) + |> where([r], r.id > ^last_id) + |> limit(^chunk_size) + |> all() + |> case do + [] -> + {:halt, last_id} + + records -> + last_id = List.last(records).id + {records, last_id} + end + end, + fn _ -> :ok end + ) + end end defmodule Pleroma.Repo.UnappliedMigrationsError do |