sezaru

sezaru

How to migrate multiple migrations files in a single transaction?

I’ve been wondering for some time now if there is a way to instruct ecto to run all, not yet applied, migrations inside a single transaction.

For example, let’s say I have two migrations:

20260406204201_remove_some_stuff.exs

20260406206213_add_some_stuff.exs

Now, let’s say that I push this to prod, ecto will create a transaction to run 20260406204201_remove_some_stuff.exs, commit it, and then create a new transaction for 20260406206213_add_some_stuff.exs.

Now, let’s imagine that for some reason 20260406206213_add_some_stuff.exs migration fails.

Now, I have a system that I can’t rollback to the last working version because 20260406204201_remove_some_stuff.exs removed some fields, so I can’t actually rollback it, and I can’t install the new version because I can’t finish the full migration.

Meaning that I will need to first create a new release with 20260406206213_add_some_stuff.exsfixed and I will have downtime during that period.

So, I was wondering, is there some way to make Ecto run ALL migrations inside the same migration?

If I could do that, then the example above would work just fine, since 20260406204201_remove_some_stuff.exs and 20260406206213_add_some_stuff.exs will run inside the same transaction, so even if 20260406206213_add_some_stuff.exs fails, 20260406204201_remove_some_stuff.exs will not be committed, so it would be safe to rollback to the older, working version.

Most Liked

LostKobrakai

LostKobrakai

Tbh I don’t think even with the request implemented it would help much. If you’re relying on transactional rollback I’d strongly suggest this to be enforced by having all code in a single migration and not be relying on deployment order. Nothing will make sure 20260406204201_remove_some_stuff.exs won’t be rolled out individually besides manual “making sure”, which is not a great state to be in.

If you really need distinct migrations you’re probably better off making individual migrations in a way they do not break the prev. version of your application, so you do not need to rollback anything to stay with a working application.

Asd

Asd

Migration is an operation which converts database from one valid state to another valid state (in both directions). If some migration leads to invalid state, it means that the migration is incorrect. You can work with incorrect migrations, but it is much harder thing to work with than the correct migrations

Calling multiple migrations inside one transaction is possible, you’d just have to merge the code of all of them into a single migration. Check out this guide for more info about it: GitHub - fly-apps/safe-ecto-migrations: Guide to Safe Ecto Migrations · GitHub

Asd

Asd

Performance. DDL transactions lock tables for the duration of the transaction.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement