cannery/lib/cannery/repo/migrator.ex

30 lines
707 B
Elixir
Raw Normal View History

2021-09-04 16:14:51 -04:00
defmodule Cannery.Repo.Migrator do
2022-01-22 21:40:29 -05:00
@moduledoc """
Genserver to automatically run migrations in prod env
"""
2021-09-04 16:14:51 -04:00
use GenServer
require Logger
2022-01-22 17:21:10 -05:00
2022-03-28 23:05:12 -04:00
def start_link(_opts) do
2021-09-04 16:14:51 -04:00
GenServer.start_link(__MODULE__, [], [])
end
2022-01-22 17:21:10 -05:00
2022-03-28 23:05:12 -04:00
def init(_opts) do
2023-02-04 20:48:14 -05:00
{:ok, if(automigrate_enabled?(), do: migrate!())}
2021-09-04 16:14:51 -04:00
end
2022-01-22 17:21:10 -05:00
2021-09-04 16:14:51 -04:00
def migrate! do
2023-02-04 20:48:14 -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:14:51 -04:00
end
2023-02-04 20:48:14 -05:00
defp automigrate_enabled? do
Application.get_env(:cannery, Cannery.Application, automigrate: false)[:automigrate]
end
2022-01-22 17:21:10 -05:00
end