Merge branch 'develop' into tests/mastodon_api_controller.ex
[akkoma] / lib / mix / tasks / pleroma / ecto / rollback.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-onl
4
5 defmodule Mix.Tasks.Pleroma.Ecto.Rollback do
6 use Mix.Task
7 import Mix.Pleroma
8 require Logger
9 @shortdoc "Wrapper on `ecto.rollback` task"
10
11 @aliases [
12 n: :step,
13 v: :to
14 ]
15
16 @switches [
17 all: :boolean,
18 step: :integer,
19 to: :integer,
20 start: :boolean,
21 quiet: :boolean,
22 log_sql: :boolean,
23 migrations_path: :string
24 ]
25
26 @moduledoc """
27 Changes `Logger` level to `:info` before start rollback.
28 Changes level back when rollback ends.
29
30 ## Start rollback
31
32 mix pleroma.ecto.rollback
33
34 Options:
35 - see https://hexdocs.pm/ecto/2.0.0/Mix.Tasks.Ecto.Rollback.html
36 """
37
38 @impl true
39 def run(args \\ []) do
40 load_pleroma()
41 {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
42
43 opts =
44 if opts[:to] || opts[:step] || opts[:all],
45 do: opts,
46 else: Keyword.put(opts, :step, 1)
47
48 opts =
49 if opts[:quiet],
50 do: Keyword.merge(opts, log: false, log_sql: false),
51 else: opts
52
53 path = Mix.Tasks.Pleroma.Ecto.ensure_migrations_path(Pleroma.Repo, opts)
54
55 level = Logger.level()
56 Logger.configure(level: :info)
57
58 if Pleroma.Config.get(:env) == :test do
59 Logger.info("Rollback succesfully")
60 else
61 {:ok, _, _} =
62 Ecto.Migrator.with_repo(Pleroma.Repo, &Ecto.Migrator.run(&1, path, :down, opts))
63 end
64
65 Logger.configure(level: level)
66 end
67 end