toranb
November 1, 2023, 11:41pm
1
I’m building out a single file elixir script to run examples and I’ve got it working with Ecto with the exception of create and migrate. Does anyone know how you could run these in a single file (without a mix file)
“setup”: [“ecto.create”, “ecto.migrate”]
The functions used by the mix task are available here so you’d be able put something together with them. Take a look at the functions specifically. Sorry, I don’t have an example off the top of my head!
1 Like
Underjord has a couple of really good blogs on dynamic repos and migrations:
Ecto is the database library we know and love from the Elixir ecosystem. It is used by default in Phoenix, thehigh-profileweb framework. Ecto has a bunch of cool features and ideas. But this post is about a corner full of nuts, bolts andvery little...
In the first part I covered the basics of getting started with Dynamic repositories with Ecto. Using that post we can create one or more repos at runtime, create the necessary database, run migrations to get it ready and then direct queries to it....
1 Like
For migrating I do these things in Livebook:
# up
Ecto.Migrator.run(Repo, [{0, SomeMigrationModule}], :up, all: true, log: false)
# down
Ecto.Migrator.run(Repo, [{0, SomeMigrationModule}], :down, all: true, log: false)
The SomeMigrationModule
is use
ing the Ecto.Migration
module, like you always do.
So yes, Ecto.Migrator
is your friend. I’ve never bothered to automate the creation of the schema, so can’t help there.
Edit: the creation mix task seems to be defined in the ecto package itself. Maybe get some inspiration here: https://github.com/elixir-ecto/ecto/blob/f2a0def734dcee354acb223116550e9ae659fb0c/lib/mix/tasks/ecto.create.ex#L53
toranb
November 2, 2023, 11:11am
5
Thanks everyone! I got a complete and working single file elixir app with Ecto that creates the database, runs migrations and a seed file
def down do
execute "DROP EXTENSION vector"
end
end
case :file.read_file_info("priv") do
{:ok, _} ->
:ok
_ ->
PhoenixDemo.Repo.__adapter__.storage_up(PhoenixDemo.Repo.config)
Ecto.Migrator.run(PhoenixDemo.Repo, [{0, PhoenixDemo.Repo.Migrations.CreateEverything}], :up, all: true, log: false)
Mix.Task.run("run", ["data.exs", "--no-mix-exs"])
end
path = Path.join(["priv", "static", "uploads"])
File.mkdir_p(path)
Process.sleep(:infinity)
1 Like