summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200328193433_populate_user_raw_bio.exs
blob: 5cf0f472524a0b1cf6cc4fc4ce09b65cb04cee36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Repo.Migrations.PopulateUserRawBio do
  use Ecto.Migration
  import Ecto.Query
  alias Pleroma.User
  alias Pleroma.Repo

  def change do
    {:ok, _} = Application.ensure_all_started(:fast_sanitize)

    User.Query.build(%{local: true})
    |> select([u], struct(u, [:id, :ap_id, :bio]))
    |> Repo.stream()
    |> Enum.each(fn %{bio: bio} = user ->
      if bio do
        raw_bio =
          bio
          |> String.replace(~r(<br */?>), "\n")
          |> Pleroma.HTML.strip_tags()

        Ecto.Changeset.cast(user, %{raw_bio: raw_bio}, [:raw_bio])
        |> Repo.update()
      end
    end)
  end
end