diff --git a/lib/pleroma/release_tasks.ex b/lib/pleroma/release_tasks.ex index 66a8b627f..7726bc635 100644 --- a/lib/pleroma/release_tasks.ex +++ b/lib/pleroma/release_tasks.ex @@ -3,18 +3,21 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.ReleaseTasks do + @repo Pleroma.Repo + def run(args) do + Mix.Tasks.Pleroma.Common.start_pleroma() [task | args] = String.split(args) case task do - "migrate" -> migrate(args) + "migrate" -> migrate() + "create" -> create() + "rollback" -> rollback(String.to_integer(Enum.at(args, 0))) task -> mix_task(task, args) end end defp mix_task(task, args) do - # Modules are not loaded before application starts - Mix.Tasks.Pleroma.Common.start_pleroma() {:ok, modules} = :application.get_key(:pleroma, :modules) module = @@ -32,7 +35,30 @@ defp mix_task(task, args) do end end - defp migrate(_args) do - :noop + def migrate do + {:ok, _, _} = Ecto.Migrator.with_repo(@repo, &Ecto.Migrator.run(&1, :up, all: true)) + end + + def rollback(version) do + {:ok, _, _} = Ecto.Migrator.with_repo(@repo, &Ecto.Migrator.run(&1, :down, to: version)) + end + + def create do + case @repo.__adapter__.storage_up(@repo.config) do + :ok -> + IO.puts("The database for #{inspect(@repo)} has been created") + + {:error, :already_up} -> + IO.puts("The database for #{inspect(@repo)} has already been created") + + {:error, term} when is_binary(term) -> + IO.puts(:stderr, "The database for #{inspect(@repo)} couldn't be created: #{term}") + + {:error, term} -> + IO.puts( + :stderr, + "The database for #{inspect(@repo)} couldn't be created: #{inspect(term)}" + ) + end end end diff --git a/rel/pleroma_ctl b/rel/pleroma_ctl index 16526af44..6137f19d3 100755 --- a/rel/pleroma_ctl +++ b/rel/pleroma_ctl @@ -1,5 +1,19 @@ #!/bin/sh # XXX: This should be removed when elixir's releases get custom command support -SCRIPT=$(readlink -f "$0") -SCRIPTPATH=$(dirname "$SCRIPT") -$SCRIPTPATH/pleroma eval 'Pleroma.ReleaseTasks.run("'"$*"'")' +if [ -z "$1" ] || [ "$1" == "help" ]; then + echo "Usage: $(basename "$0") COMMAND [ARGS] + + The known commands are: + + create Create database schema (needs to be executed only once) + migrate Execute database migrations (needs to be done after updates) + rollback Rollback database migrations (needs to be done before downgrading) + + and any mix tasks under Pleroma namespace, for example \`mix pleroma.user COMMAND\` is + equialent to \`$(basename "$0") user COMMAND\` +" +else + SCRIPT=$(readlink -f "$0") + SCRIPTPATH=$(dirname "$SCRIPT") + $SCRIPTPATH/pleroma eval 'Pleroma.ReleaseTasks.run("'"$*"'")' +fi