Hi there,
I am playing around with Elixir 1.12 (rc 1) and specifically Mix.install
. I would like to be able to use Ecto from a simple one-off script (running migrations and using it), because I feel this will be very useful for data exploration tasks.
So far Ecto complains for good reason, because the app I need to refer to in the Repo declaration (via otp_app: sample
) does not exist (the error is ** (ArgumentError) unknown application: :sample
).
Did anyone figure out how to use Ecto in that setup ?
Thanks !
– Thibaut
In case that’s useful to others here, here is the current code:
# mix run sql.exs --no-mix-exs
# TODO: use MIX_INSTALL_DIR
# https://hexdocs.pm/mix/1.12.0-rc.1/Mix.html#install/2
Mix.install([
{:ecto_sql, "~> 3.6.1"},
{:postgrex, ">= 0.0.0"}
])
defmodule Sample.Repo do
use Ecto.Repo,
otp_app: :sample, # this app is not defined!
adapter: Ecto.Adapters.Postgres
def init(_context, config) do
config = Keyword.put(config, :url, "postgres://localhost:5432/sample")
{:ok, config}
end
end
defmodule Sample.Migration do
use Ecto.Migration
def up do
create table("cars") do
add :vin, :string
add :brand, :string
add :model, :integer
add :price, :decimal
add :production_year, :integer
timestamps()
end
end
def down do
drop table("cars")
end
end
require Logger
Logger.info "Connecting to db..."
{:ok, repo} = Sample.Repo.start_link()
Logger.info "Applying migrations..."
Ecto.Migrator.run(Sample.Repo, [{0, Sample.Migration}], :up)