Add migrate/rollback to release tasks

This commit is contained in:
rinpatch 2019-06-09 13:33:44 +03:00
parent b6d2db42a7
commit 2a659b35f1
2 changed files with 48 additions and 8 deletions

View File

@ -3,18 +3,21 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ReleaseTasks do defmodule Pleroma.ReleaseTasks do
@repo Pleroma.Repo
def run(args) do def run(args) do
Mix.Tasks.Pleroma.Common.start_pleroma()
[task | args] = String.split(args) [task | args] = String.split(args)
case task do case task do
"migrate" -> migrate(args) "migrate" -> migrate()
"create" -> create()
"rollback" -> rollback(String.to_integer(Enum.at(args, 0)))
task -> mix_task(task, args) task -> mix_task(task, args)
end end
end end
defp mix_task(task, args) do 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) {:ok, modules} = :application.get_key(:pleroma, :modules)
module = module =
@ -32,7 +35,30 @@ defmodule Pleroma.ReleaseTasks do
end end
end end
defp migrate(_args) do def migrate do
:noop {: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
end end

View File

@ -1,5 +1,19 @@
#!/bin/sh #!/bin/sh
# XXX: This should be removed when elixir's releases get custom command support # XXX: This should be removed when elixir's releases get custom command support
SCRIPT=$(readlink -f "$0") if [ -z "$1" ] || [ "$1" == "help" ]; then
SCRIPTPATH=$(dirname "$SCRIPT") echo "Usage: $(basename "$0") COMMAND [ARGS]
$SCRIPTPATH/pleroma eval 'Pleroma.ReleaseTasks.run("'"$*"'")'
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