Batched, resumable, reversible data migrations for Ecto and Oban.
Schema migrations change your database's structure. Heron is for the other kind of change: rewriting rows. Backfills, cleanups and data fixes that have to touch millions of rows without locking tables, without blocking a deploy, and without starting over when a node restarts.
A Heron migration is a module with a query that selects the rows still needing work and a function that processes one batch of them:
defmodule MyApp.DataMigrations.BackfillDisplayNames do
use Heron.Migration,
description: "Backfill users.display_name from first and last name",
batch_size: 500
alias MyApp.Repo
@impl Heron.Migration
def query(_run), do: from(u in "users", where: is_nil(u.display_name), select: %{id: u.id})
@impl Heron.Migration
def migrate_batch(records, _run) do
ids = Enum.map(records, & &1.id)
from(u in "users", where: u.id in ^ids, update: [set: [display_name: fragment("first_name || ' ' || last_name")]])
|> Repo.update_all([])
:ok
end
endHeron runs it in batches as Oban jobs, stores its progress, and resumes it after a crash or a deploy.
- Batched and throttled. Each batch is its own Oban job, with a pause between batches so normal traffic keeps priority.
- Resumable. Runs survive restarts and deploys, picking up from their cursor. Stopped or failed runs can be resumed.
- Reversible. Optionally implement
reverse_query/1andreverse_batch/2and a migration can be reverted with the same batching machinery. - Ordered by dependencies. A migration can require others to finish first.
- Started automatically, if you want.
Heron.Plugincan start eligible migrations one at a time, optionally only inside a start window (say, off-peak hours). - Safe by default. Dry runs, verification of the first n batches with
verify/1, runaway protection for queries that never drain, and per-migration timeouts. - A dashboard. An optional LiveView page to start, stop, resume, revert and dry-run migrations and to watch their progress.
- Extensible. Notifiers, start windows, a pluggable run log, and wrapper macros for your own migration options.
- Efficient support for very large tables. In some cases, merely querying the table to produce an ordered cursor
(for instance, "give me the next 1,000 records where column x is non-null, ordered by ID") may be
prohibitively expensive. In such a case, you can use
strategy: :backfillon your migration to simply drain records until none match the query—for instance, "backfill all the records where column x is non-null, taking any 1,000 records at a time."
Jump has used a pre-public version of Heron since March 2026 for all its data migrations. It's been an effective replacement for both hand-rolled, one-off data migrations as well as scripts we would run in a production IEx shell. We've run over 500 total migrations, including some that took multiple weeks when running on multi-terabyte tables.
Fly's Safe Ecto Migrations guide shows how to write a safe backfill by hand, as an Ecto migration you run from a release task. Heron is the kind of dedicated system you reach for once backfills become routine. It keeps the guide's techniques (keyset batching, throttling, idempotent queries) and adds run state, resumption, reversal, dependencies, automatic starts and a UI. For schema changes, such as concurrent indexes and constraints, follow that guide; Heron only changes rows.
Oban Pro includes support for cursor-based backfills. This is effectively a subset of Heron's functionality. Both tools support throttled, batched, resumable backfills. Heron adds some nice-to-have functionality:
- Optional automatic starts of data migrations after a deploy (and optionally only within a certain time window)
- Support for cursorless backfills via the
:backfillstrategy (important for very large tables where the cursor query is prohibitively slow) - Dependency chains between data migrations (data migration C depends on B which depends on A)
- Records of each run (the
heron_runstable tracks status, progress, errors, logs, and a history of past runs) - Stopping and resuming migrations on-demand via the UI
- Optional reversibility
Monarch is another library for data migrations run as Oban jobs. Like Heron, it finds your migrations, queues pending ones at boot, and records which have completed, so a deploy can start new migrations on its own. Heron adds:
- Throttling between batches. Monarch runs a whole migration inside one Oban job, with no pause between batches.
- A cursor strategy alongside drain-until-empty (Monarch only drains, rerunning its filter query for every batch)
- Dependency chains between data migrations, with one automatic start at a time (Monarch queues every pending migration at once)
- Records of each run's status, progress, errors and logs (Monarch records only that a migration completed)
- A failed migration waits for you instead of being re-queued on every deploy
- Broader runaway protection (Monarch only catches a query that returns the same batch twice in a row)
- Stopping, resuming and rerunning migrations on demand via the UI
- Dry runs, verification of the first batches, and optional reversibility
- PostgreSQL
- Ecto SQL 3.13+
- Oban 2.21+ (Oban Pro is not required)
- Phoenix LiveView 1.0+, only for the dashboard
def deps do
[
{:heron, "~> 0.1"},
]
endCreate Heron's tables with a (traditional Ecto schema) migration of your own:
# Run `mix ecto.gen.migration add_heron` and save this to the resulting file
defmodule MyApp.Repo.Migrations.AddHeron do
use Ecto.Migration
def up, do: Heron.Tables.up(version: 1)
def down, do: Heron.Tables.down(version: 1)
endThe tables go in public, or in the schema given by prefix: or by the migration's own prefix.
Heron's queries don't name a schema, so a schema other than public must be on the repo's
search_path.
Configure Heron, and add its queue and plugin to Oban. Oban must use the same repo as Heron,
because Heron inserts jobs in the same transaction as its runs; any Oban prefix works.
config :heron,
repo: MyApp.Repo,
otp_app: :my_app,
namespaces: [MyApp.DataMigrations]
config :my_app, Oban,
queues: [heron: 1],
plugins: [Heron.Plugin, {Oban.Plugins.Lifeline, rescue_after: :timer.minutes(30)}]Heron.Plugin resumes runs orphaned by a restart, and starts the next eligible migration whenever
none is running, so a deployed migration runs on its own. Lifeline is required: a job that was
executing when its node died stays executing until Lifeline rescues it, and until then Heron
treats the run as alive. Oban Pro users can use DynamicLifeline instead. A migration that has run
before is left for you to rerun, resume or revert. Pass auto_start: false to start migrations only
by hand.
See Heron.Config for every option, and Heron.Jobs for running Heron's jobs through your own worker modules.
Generate one:
mix heron.gen.migration backfill_display_namesThis creates lib/my_app/data_migrations/YYYYMMDDHHMMSS_backfill_display_names.ex and a test. The
timestamp in the filename identifies the migration.
The two contracts you must guarantee as the migration author are:
query/1returns only rows that still need migratingmigrate_batch/2is idempotent
Once a batch is processed, its rows must no longer match the query. (This makes restarting, resuming
and progress counting work.) Likewise, migrate_batch/2 can be interrupted by a manual stop or the node
going down, and it may be re-run by a manual restart, so batches must be idempotent. If necessary, you can
use batch_transaction?: true to ensure an interrupted batch leaves no partial writes.
See the Heron.Migration docs for strategies, options, reversal, verification and runaway protection.
The most common way to run Heron's data migrations is either automatically at application start (via the
Heron.Plugin Oban configuration) or manually via the web dashboard (see below). However, if you want
to control the migrations via code, you can do so:
Heron.start(MyApp.DataMigrations.BackfillDisplayNames)
Heron.stop_run(run)
Heron.resume(run)
Heron.revert(MyApp.DataMigrations.BackfillDisplayNames)
Heron.dry_run(MyApp.DataMigrations.BackfillDisplayNames)Locally, mix heron.migrate runs pending migrations synchronously, and mix heron.status lists
them.
Automatic starts run one migration at a time, sequentially. Migrations started by hand can run alongside each
other, each batch being an Oban job on the :heron queue. In open-source Oban a queue's limit is
per node, so on several nodes two runs' batches can execute at the same time. If you need batches
serialized across the cluster, run the queue on a single node, or use Oban Pro's global_limit: 1.
import Heron.Web.Router
scope "/admin" do
pipe_through [:browser, :require_admin]
heron_dashboard "/data-migrations",
resolver: MyAppWeb.HeronResolver,
on_mount: [{MyAppWeb.UserAuth, :ensure_admin}]
endA resolver is required: a Heron.Web.Resolver decides who may view the dashboard and who may
start, stop, resume, revert or dry-run migrations, and is checked on every mount and every action.
When the LiveView reconnects, the route's plug pipeline doesn't run again, so put your
authentication in on_mount hooks, which run on every mount.
The dashboard shows the run in progress with its rate and time remaining, every migration's status and progress, and a details panel per migration with its latest run, error, dependencies, log, run history and dry-run sample. You can filter and search, and the filter, search and open migration are kept in the URL, so links can be shared.
It renders inside your app's layout, on your app's background, and uses your app's LiveView
socket. Its styles are bundled and scoped, so it needs no asset setup. It switches to dark colors
when an ancestor element has the dark class or data-theme="dark". With a Content Security
Policy that disallows inline styles, pass csp_nonce_assign_key: with the conn assign that holds
your style nonce, and the dashboard's bundled stylesheet will carry it.
Set config :heron, pubsub: MyApp.PubSub for instant updates; without it, the page polls.
- Notifications. Implement
Heron.Notifierto post to chat or update another system on every lifecycle event. A notifier that crashes never fails a run. - Start windows. Implement
Heron.StartWindowand setstart_window: :off_peakon migrations that should only start automatically at certain times. - Run logs. Replace the default database log with your own
Heron.RunLog. - Your own options. Wrap
use Heron.Migrationin your own macro to add options and callbacks, such as an owner to notify. See "Wrapping" inHeron.Migration. - Generator templates. Put your own templates in
priv/templates/heron.gen.migration/.
Apache License 2.0. See LICENSE.