Using Req/Finch in an Ecto migration

I have an app where a bug in application code caused some invalid data to be written to a table. I want to run a migration to fix that data, but the process requires the use of Req. However, I’m getting:

** (ArgumentError) unknown registry: Req.Finch
    (elixir 1.17.2) lib/registry.ex:1400: Registry.key_info!/1
    (elixir 1.17.2) lib/registry.ex:590: Registry.lookup/2
    (finch 0.18.0) lib/finch/pool_manager.ex:46: Finch.PoolManager.lookup_pool/2
    (finch 0.18.0) lib/finch/pool_manager.ex:34: Finch.PoolManager.get_pool/3
    (finch 0.18.0) lib/finch.ex:428: Finch.__stream__/5
    (finch 0.18.0) lib/finch.ex:472: anonymous fn/4 in Finch.request/3

After doing some reading around here/Google, this makes sense as Finch is started by my application’s supervisor tree in start()/2. It seems inadvisable to run the migrations after the application starts, so how can I use Req/Finch before the Application starts?

You can start any required applications with Application.ensure_all_started/2 and processes with Supervisor.start_link/2 (or starting them directly).

I have a general feeling that you should probably not be doing requests during migrations, though.

1 Like

I’m not against this, but AFAI understand there are some benefits to doing things in migrations. Namely,

  • Ecto does some magic under the hood to ensure only one instance of the deployed app is running the migration at a time
  • Ecto makes sure that the migration only runs once, since it tracks which migrations have been run

I could replicate the latter by making my own table or versioning the records, but that seems excessive. I will literally run this migration once and never again. New data will never need this migration, since it will be correct from its creation.

Edit: I failed to consider the simplest solution: SSH onto a running instance and just run the migration manually. Thanks, fly ssh console.

1 Like

Indeed, what’s stopping you to manually correct the data in a production iex?