migration optimization
[akkoma] / priv / repo / migrations / 20190414125034_migrate_old_bookmarks.exs
1 defmodule Pleroma.Repo.Migrations.MigrateOldBookmarks do
2 use Ecto.Migration
3 import Ecto.Query
4 alias Pleroma.Activity
5 alias Pleroma.Bookmark
6 alias Pleroma.User
7 alias Pleroma.Repo
8
9 def up do
10 query =
11 from(u in User,
12 where: u.local == true,
13 where: fragment("array_length(?, 1)", u.old_bookmarks) > 0,
14 select: %{id: u.id, old_bookmarks: u.old_bookmarks}
15 )
16
17 Repo.transaction(fn ->
18 Repo.stream(query)
19 |> Enum.each(fn user ->
20 Enum.each(user.old_bookmarks, fn id ->
21 activity = Activity.get_create_by_object_ap_id(id)
22 {:ok, _} = Bookmark.create(user.id, activity.id)
23 end)
24 end)
25 end)
26 end
27
28 def down do
29 execute("TRUNCATE TABLE bookmarks")
30 end
31 end