From 2a659b35f16cacb39ea6dae2aefd3572f4be6783 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Sun, 9 Jun 2019 13:33:44 +0300 Subject: [PATCH] Add migrate/rollback to release tasks --- lib/pleroma/release_tasks.ex | 36 +++++++++++++++++++++++++++++++----- rel/pleroma_ctl | 20 +++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) 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