cannery/lib/lokal/repo/migrator.ex

26 lines
586 B
Elixir
Raw Normal View History

2021-09-04 16:19:23 -04:00
defmodule Lokal.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:28:53 -05:00
def start_link(_opts) do
2021-09-04 16:19:23 -04:00
GenServer.start_link(__MODULE__, [], [])
end
2023-02-04 17:28:53 -05:00
def init(_opts) do
2023-02-04 20:47:47 -05:00
{:ok, if(automigrate_enabled?(), do: migrate!())}
2021-09-04 16:19:23 -04:00
end
def migrate! do
path = Application.app_dir(:lokal, "priv/repo/migrations")
Ecto.Migrator.run(Lokal.Repo, path, :up, all: true)
end
2023-02-04 20:47:47 -05:00
defp automigrate_enabled? do
Application.get_env(:lokal, Lokal.Application, automigrate: false)[:automigrate]
end
2021-09-04 16:19:23 -04:00
end