Rodeoclash

Rodeoclash

Manging migrations outside of Ecto

Has anyone had success with getting Ecto to use externally generated migration files? I have a directory of SQL files that I would like to migrate for my unit tests but loading each migration and executing it is causing problems with Postgres prepared statements due to multiple commands being in each SQL migration. My code to load the migrations is:

  def up do
    Path.wildcard("priv/repo/migrations/hasura/**/up.sql")
    |> Enum.each(fn sql_file ->
      sql = File.read!(sql_file)
      execute("BEGIN; #{sql} COMMIT;")
    end)
  end

However, this gives me the error: cannot insert multiple commands into a prepared statement.

I’ve played around with the Ecto config (setting prepare: :unnamed) and wrapping each of the migration files in a BEGIN / COMMIT block (i.e. a transaction) without any luck.

I’ve resorted to using Mox to mock my Repo which works ok but I would like to test against the actual database.

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Ah! yes 100%. So what Ecto does is that for each test it starts a transaction, and then all of your test data is executed within that transaction. Then at the end of the test, it just calls ROLLBACK on that transaction instead of COMMIT. So this way it doesn’t need to do any big delete of any data, because no data is ever committed.

In that cause I’d put all of that in a priv/repo/structure.sql file and then use the mix ecto.load task. So for a total reset you would mix do ecto.drop, ecto.create, ecto.load, run priv/repo/seeds.exs to drop, create, load your sql file, and run an extra seeds.

For your tests I’d do the same thing in MIX_ENV=test but just don’t run seeds.exs. From there, the normal Ecto Sandbox stuff should handle everything.

EDIT: I’d also add an ecto.migrate command after ecto.load to run any new migrations you later add, if you plan to add any.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @Rodeoclash migrations already happen in a transaction so you shouldn’t need to add your own begin and commit. Have you tried simply doing execute(sql) ?

Rodeoclash

Rodeoclash

Thanks for hashing that out with me, I’ll do a test run tomorrow but I suspect this has been solved!

I appreciate the work you’ve done with Absinth too by the way - I’ve used it in a bunch of various projects in the esports space :slightly_smiling_face:

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement