revert ae54c06bb4bd2389d6eb1502b842c6b632e12e40
[akkoma] / lib / pleroma / data_migration.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.DataMigration do
6 use Ecto.Schema
7
8 alias Pleroma.DataMigration
9 alias Pleroma.DataMigration.State
10 alias Pleroma.Repo
11
12 import Ecto.Changeset
13 import Ecto.Query
14
15 schema "data_migrations" do
16 field(:name, :string)
17 field(:state, State, default: :pending)
18 field(:feature_lock, :boolean, default: false)
19 field(:params, :map, default: %{})
20 field(:data, :map, default: %{})
21
22 timestamps()
23 end
24
25 def changeset(data_migration, params \\ %{}) do
26 data_migration
27 |> cast(params, [:name, :state, :feature_lock, :params, :data])
28 |> validate_required([:name])
29 |> unique_constraint(:name)
30 end
31
32 def update_one_by_id(id, params \\ %{}) do
33 with {1, _} <-
34 from(dm in DataMigration, where: dm.id == ^id)
35 |> Repo.update_all(set: params) do
36 :ok
37 end
38 end
39
40 def get_by_name(name) do
41 Repo.get_by(DataMigration, name: name)
42 end
43
44 def populate_hashtags_table, do: get_by_name("populate_hashtags_table")
45 end