09f02c22326d1a1d32f6a32bec25a528efc32f15
[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.stream(query)
18 |> Enum.each(fn user ->
19 Enum.each(user.old_bookmarks, fn id ->
20 activity = Activity.get_create_by_object_ap_id(id)
21 {:ok, _} = Bookmark.create(user.id, activity.id)
22 end)
23 end)
24 end
25
26 def down do
27 execute("TRUNCATE TABLE bookmarks")
28 end
29 end