Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / mix / tasks / pleroma / ecto / migrate.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.Migrate do
6 use Mix.Task
7 import Mix.Pleroma
8 require Logger
9
10 @shortdoc "Wrapper on `ecto.migrate` task."
11
12 @aliases [
13 n: :step,
14 v: :to
15 ]
16
17 @switches [
18 all: :boolean,
19 step: :integer,
20 to: :integer,
21 quiet: :boolean,
22 log_sql: :boolean,
23 strict_version_order: :boolean,
24 migrations_path: :string
25 ]
26
27 @moduledoc """
28 Changes `Logger` level to `:info` before start migration.
29 Changes level back when migration ends.
30
31 ## Start migration
32
33 mix pleroma.ecto.migrate [OPTIONS]
34
35 Options:
36 - see https://hexdocs.pm/ecto/2.0.0/Mix.Tasks.Ecto.Migrate.html
37 """
38
39 @impl true
40 def run(args \\ []) do
41 load_pleroma()
42 {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
43
44 opts =
45 if opts[:to] || opts[:step] || opts[:all],
46 do: opts,
47 else: Keyword.put(opts, :all, true)
48
49 opts =
50 if opts[:quiet],
51 do: Keyword.merge(opts, log: false, log_sql: false),
52 else: opts
53
54 path = Mix.Tasks.Pleroma.Ecto.ensure_migrations_path(Pleroma.Repo, opts)
55
56 level = Logger.level()
57 Logger.configure(level: :info)
58
59 {:ok, _, _} = Ecto.Migrator.with_repo(Pleroma.Repo, &Ecto.Migrator.run(&1, path, :up, opts))
60
61 Logger.configure(level: level)
62 end
63 end