Gitlab: Run benchmark in CI.
[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(bookmarks, 1)") > 0,
14 select: %{id: u.id, bookmarks: fragment("bookmarks")}
15 )
16
17 Repo.stream(query)
18 |> Enum.each(fn %{id: user_id, bookmarks: bookmarks} ->
19 Enum.each(bookmarks, fn ap_id ->
20 activity = Activity.get_create_by_object_ap_id(ap_id)
21 unless is_nil(activity), do: {:ok, _} = Bookmark.create(user_id, activity.id)
22 end)
23 end)
24
25 alter table(:users) do
26 remove(:bookmarks)
27 end
28 end
29
30 def down do
31 alter table(:users) do
32 add :bookmarks, {:array, :string}, null: false, default: []
33 end
34 end
35 end