rollback.ex 1.62 KB
Newer Older
Alexander Strizhakov's avatar
Alexander Strizhakov committed
1
# Pleroma: A lightweight social networking server
feld's avatar
feld committed
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
Alexander Strizhakov's avatar
Alexander Strizhakov committed
3
4
5
6
# SPDX-License-Identifier: AGPL-3.0-onl

defmodule Mix.Tasks.Pleroma.Ecto.Rollback do
  use Mix.Task
7
  import Mix.Pleroma
Alexander Strizhakov's avatar
Alexander Strizhakov committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  require Logger
  @shortdoc "Wrapper on `ecto.rollback` task"

  @aliases [
    n: :step,
    v: :to
  ]

  @switches [
    all: :boolean,
    step: :integer,
    to: :integer,
    start: :boolean,
    quiet: :boolean,
    log_sql: :boolean,
    migrations_path: :string
  ]

  @moduledoc """
  Changes `Logger` level to `:info` before start rollback.
  Changes level back when rollback ends.

  ## Start rollback

      mix pleroma.ecto.rollback

  Options:
    - see https://hexdocs.pm/ecto/2.0.0/Mix.Tasks.Ecto.Rollback.html
  """

  @impl true
  def run(args \\ []) do
40
    load_pleroma()
Alexander Strizhakov's avatar
Alexander Strizhakov committed
41
42
    {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

43
44
45
46
    if Application.get_env(:pleroma, Pleroma.Repo)[:ssl] do
      Application.ensure_all_started(:ssl)
    end

Alexander Strizhakov's avatar
Alexander Strizhakov committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    opts =
      if opts[:to] || opts[:step] || opts[:all],
        do: opts,
        else: Keyword.put(opts, :step, 1)

    opts =
      if opts[:quiet],
        do: Keyword.merge(opts, log: false, log_sql: false),
        else: opts

    path = Mix.Tasks.Pleroma.Ecto.ensure_migrations_path(Pleroma.Repo, opts)

    level = Logger.level()
    Logger.configure(level: :info)

    if Pleroma.Config.get(:env) == :test do
      Logger.info("Rollback succesfully")
    else
      {:ok, _, _} =
        Ecto.Migrator.with_repo(Pleroma.Repo, &Ecto.Migrator.run(&1, path, :down, opts))
    end

    Logger.configure(level: level)
  end
end