memEx/lib/memex/repo/migrator.ex

26 lines
586 B
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule Memex.Repo.Migrator do
2022-01-22 20:44:38 -05:00
@moduledoc """
Genserver to automatically perform all migration on app start
"""
2021-09-04 16:19:23 -04:00
use GenServer
require Logger
2023-02-04 17:36:27 -05:00
def start_link(_opts) do
2021-09-04 16:19:23 -04:00
GenServer.start_link(__MODULE__, [], [])
end
2023-02-04 17:36:27 -05:00
def init(_opts) do
2023-02-04 20:48:17 -05:00
{:ok, if(automigrate_enabled?(), do: migrate!())}
2021-09-04 16:19:23 -04:00
end
def migrate! do
2023-02-04 20:48:17 -05:00
path = Application.app_dir(:lokal, "priv/repo/migrations")
Ecto.Migrator.run(Lokal.Repo, path, :up, all: true)
end
defp automigrate_enabled? do
Application.get_env(:lokal, Lokal.Application, automigrate: false)[:automigrate]
2021-09-04 16:19:23 -04:00
end
end