Merge pull request 'Correct og:description tag in static-fe' (#373) from sfr/akkoma...
[akkoma] / lib / pleroma / release_tasks.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.ReleaseTasks do
6 @repo Pleroma.Repo
7
8 def run(args) do
9 [task | args] = String.split(args)
10
11 case task do
12 "migrate" -> migrate(args)
13 "create" -> create()
14 "rollback" -> rollback(args)
15 task -> mix_task(task, args)
16 end
17 end
18
19 defp mix_task(task, args) do
20 Application.load(:pleroma)
21 {:ok, modules} = :application.get_key(:pleroma, :modules)
22
23 module =
24 Enum.find(modules, fn module ->
25 module = Module.split(module)
26
27 match?(["Mix", "Tasks", "Pleroma" | _], module) and
28 task_match?(module, task)
29 end)
30
31 if module do
32 module.run(args)
33 else
34 IO.puts("The task #{task} does not exist")
35 end
36 end
37
38 defp task_match?(["Mix", "Tasks", "Pleroma" | module_path], task) do
39 module_path
40 |> Enum.join(".")
41 |> String.downcase()
42 |> String.equivalent?(String.downcase(task))
43 end
44
45 def migrate(args) do
46 Mix.Tasks.Pleroma.Ecto.Migrate.run(args)
47 end
48
49 def rollback(args) do
50 Mix.Tasks.Pleroma.Ecto.Rollback.run(args)
51 end
52
53 def create do
54 Application.load(:pleroma)
55
56 case @repo.__adapter__.storage_up(@repo.config) do
57 :ok ->
58 IO.puts("The database for #{inspect(@repo)} has been created")
59
60 {:error, :already_up} ->
61 IO.puts("The database for #{inspect(@repo)} has already been created")
62
63 {:error, term} when is_binary(term) ->
64 IO.puts(
65 :stderr,
66 "The database for #{inspect(@repo)} couldn't be created: #{inspect(term)}"
67 )
68 end
69 end
70 end