Skip to content

Commit

Permalink
Migrate on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Oct 17, 2019
1 parent 0d8b044 commit 1ddf9cb
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 16 deletions.
14 changes: 0 additions & 14 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

{uri, _} = System.cmd("pg_tmp", ["-t"])

[username, host, port, database] =
Regex.scan(~r/(\w+)@([\w\d\.]+)\:(\d+)\/(\w+)/i, uri, capture: :all_but_first) |> List.flatten()

config :stripe_mock, StripeMock.Repo,
username: username,
password: "",
database: database,
hostname: host,
port: port,
pool_size: 2,
migration_primary_key: [name: :id, type: :binary_id]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
3 changes: 2 additions & 1 deletion lib/stripe_mock/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule StripeMock.Application do
def start(_type, _args) do
children = [
StripeMockWeb.Endpoint,
StripeMock.Repo
StripeMock.Repo,
StripeMock.Migrator
]

opts = [strategy: :one_for_one, name: StripeMock.Supervisor]
Expand Down
73 changes: 73 additions & 0 deletions lib/stripe_mock/migrator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
defmodule StripeMock.Migrator do
use GenServer

def start_link(default) do
GenServer.start_link(__MODULE__, default, name: __MODULE__)
end

def done? do
GenServer.call(__MODULE__, :done?)
end

@impl true
def init(_arg) do
migrate()

{:ok, nil}
end

@impl true
def handle_call(:done?, _from, state) do
{:reply, true, state}
end

@start_apps [
:crypto,
:ssl,
:postgrex,
:ecto,
:ecto_sql
]

@repos [StripeMock.Repo]

def migrate(_argv \\ nil) do
# Start apps necessary for executing migrations
IO.puts("Starting dependencies")
Enum.each(@start_apps, &Application.ensure_all_started/1)

Application.load(:basic_space)

# Start the Repo(s) for app
# Switch pool_size to 2 for ecto > 3.0
IO.puts("Starting repos")
Enum.each(@repos, & &1.start_link(pool_size: 2))

# Run migrations
Enum.each(@repos, &run_migrations_for/1)

# Done
IO.puts("Success!")
end

defp run_migrations_for(repo) do
app = Keyword.get(repo.config, :otp_app)
migrations_path = priv_path_for(repo, "migrations")
IO.puts("Running migrations for #{app}, from #{migrations_path}")
Ecto.Migrator.run(repo, migrations_path, :up, all: true)
end

defp priv_path_for(repo, filename) do
app = Keyword.get(repo.config, :otp_app)

repo_underscore =
repo
|> Module.split()
|> List.last()
|> Macro.underscore()

priv_dir = "#{:code.priv_dir(app)}"

Path.join([priv_dir, repo_underscore, filename])
end
end
22 changes: 21 additions & 1 deletion lib/stripe_mock/repo.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
defmodule StripeMock.Repo do
use Ecto.Repo, otp_app: :stripe_mock, adapter: Ecto.Adapters.Postgres
import Ecto.Changeset
alias Ecto.Changeset
alias StripeMock.API

@impl true
def init(_type, config) do
{uri, _} = System.cmd("pg_tmp", ["-t"])

[[username, host, port, database]] =
Regex.scan(~r/(\w+)@([\w\d\.]+)\:(\d+)\/(\w+)/i, uri, capture: :all_but_first)

config =
Keyword.merge(config,
username: username,
password: "",
database: database,
hostname: host,
port: port,
pool_size: 2,
migration_primary_key: [name: :id, type: :binary_id]
)

{:ok, config}
end

def fetch(schema, id) do
with true <- valid_id?(id),
%{} = object <- get(schema, id) do
Expand Down
11 changes: 11 additions & 0 deletions lib/stripe_mock_web/plug/ensure_migrator_finished.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule StripeMockWeb.Plug.EnsureMigratorFinished do
def init(arg) do
arg
end

def call(conn, _arg) do
true = StripeMock.Migrator.done?()

conn
end
end
1 change: 1 addition & 0 deletions lib/stripe_mock_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule StripeMockWeb.Router do
pipeline :api do
plug :accepts, ["json"]
plug StripeMockWeb.Plug.Auth
plug StripeMockWeb.Plug.EnsureMigratorFinished
end

scope "/v1", StripeMockWeb do
Expand Down

0 comments on commit 1ddf9cb

Please sign in to comment.