From fea09c78939f3a1b4acca726896d225ce74fd075 Mon Sep 17 00:00:00 2001 From: Thomas Weustenfeld Date: Thu, 16 Jul 2026 22:43:52 -0400 Subject: [PATCH 1/3] fix: prevent LiveDashboard from showing stale migration status under concurrent access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two related bugs in DataMigration.LiveDashboard.Page: 1. list_data_migrations/1 accumulated entries into a list across calls instead of keying by {repo, folder, id}, so a stale entry cached by one LiveView session could shadow a fresh one computed by another concurrent session on the same node. 2. compile_file/2 used Code.require_file/2, which is idempotent VM-wide (not per-process): once any process on the node has required a given file path, every later call — including from an unrelated LiveView session — returns nil instead of the compiled module list. The caller treated nil as a compile failure and silently dropped the migration from the list. Switched to Code.compile_file/2, which always recompiles and returns the module list, and suppressed the resulting "redefining module" diagnostic. Found while investigating a production report of the dashboard reverting a migration's status to "down" after a page reload, even though the migration had actually run successfully. Co-Authored-By: Claude Sonnet 5 --- lib/data_migration/live_dashboard/page.ex | 57 +++++++++----- .../99999999999999_noop.exs | 7 ++ .../99999999999999_noop.exs | 7 ++ .../99999999999999_noop.exs | 7 ++ .../99999999999999_noop.exs | 7 ++ .../live_dashboard/page_test.exs | 76 +++++++++++++++++++ 6 files changed, 140 insertions(+), 21 deletions(-) create mode 100644 priv/my_xql_repo/data_migrations_myxql/99999999999999_noop.exs create mode 100644 priv/pg_repo/data_migrations_pg/99999999999999_noop.exs create mode 100644 priv/sq_lite_repo/data_migrations_sqlite/99999999999999_noop.exs create mode 100644 priv/tds_repo/data_migrations_tds/99999999999999_noop.exs diff --git a/lib/data_migration/live_dashboard/page.ex b/lib/data_migration/live_dashboard/page.ex index 33401d0..8164d23 100644 --- a/lib/data_migration/live_dashboard/page.ex +++ b/lib/data_migration/live_dashboard/page.ex @@ -410,26 +410,34 @@ defmodule DataMigration.LiveDashboard.Page do |> stream_insert(:logs, {message, level})} end - defp maybe_recompile([]), do: [] + defp maybe_recompile(migrations) when map_size(migrations) == 0, do: migrations if Mix.env() == :dev do defp maybe_recompile(migrations) do - Enum.each(migrations, fn migration -> + Enum.each(migrations, fn {_key, migration} -> Code.unrequire_files([migration.file]) :code.soft_purge(migration.module) end) - [] + %{} end else defp maybe_recompile(migrations), do: migrations end defp compile_file(file, folder) do - # Silence "redefining module ..." logs + # `Code.require_file/2` is idempotent VM-wide (not per-process): once + # any process on this node has required a given path, every later call + # — including from a completely unrelated LiveView session — gets `nil` + # back instead of the compiled module list. Downstream code treats + # `nil` as "this file failed to compile" and silently drops the + # migration, so a second concurrent viewer of this page can see an + # incomplete list. `Code.compile_file/2` always recompiles and always + # returns the module list; silence the resulting "redefining module" + # diagnostic instead. {result, _} = Code.with_diagnostics(fn -> - Code.require_file(file, folder) + Code.compile_file(file, folder) end) result @@ -462,11 +470,18 @@ defmodule DataMigration.LiveDashboard.Page do end) end + # Keyed by {repo, folder, id} so a repeat call always *replaces* an + # entry rather than appending beside it. `:persistent_term` is + # process-independent (shared by every LiveView session on this node), + # so without this a stale entry from an earlier call — including one + # computed before the most recent migrate up/down — sits in the cache + # forever alongside the fresh one, and callers reading through + # `find_migration/4` or the table's row count can see it. @cache_key :data_migration_list defp list_data_migrations(locations) do - existing = @cache_key |> :persistent_term.get([]) |> maybe_recompile() + existing = @cache_key |> :persistent_term.get(%{}) |> maybe_recompile() - migrations = + migrations_by_key = Enum.reduce(locations, existing, fn {repo, folders}, acc -> Enum.reduce(List.wrap(folders), acc, fn folder, data_migration_acc -> abs_dir = Ecto.Migrator.migrations_path(repo, folder) @@ -481,23 +496,23 @@ defmodule DataMigration.LiveDashboard.Page do {status, id, _name}, acc -> Map.put(acc, id, status) end) - data_migrations = - [abs_dir, "*.exs"] - |> Path.join() - |> Path.wildcard() - |> Enum.flat_map(fn file -> - case compile_file(file, abs_dir) do - nil -> [] - compiled -> to_migration(file, rel_path, repo, compiled, statuses) - end - end) - - data_migrations ++ data_migration_acc + [abs_dir, "*.exs"] + |> Path.join() + |> Path.wildcard() + |> Enum.flat_map(fn file -> + case compile_file(file, abs_dir) do + nil -> [] + compiled -> to_migration(file, rel_path, repo, compiled, statuses) + end + end) + |> Enum.reduce(data_migration_acc, fn migration, acc -> + Map.put(acc, {migration.repo, migration.folder, migration.id}, migration) + end) end) end) - :persistent_term.put(@cache_key, migrations) - migrations + :persistent_term.put(@cache_key, migrations_by_key) + Map.values(migrations_by_key) end # Skipped because this is a compile-controlled list of files not from user input diff --git a/priv/my_xql_repo/data_migrations_myxql/99999999999999_noop.exs b/priv/my_xql_repo/data_migrations_myxql/99999999999999_noop.exs new file mode 100644 index 0000000..8df7815 --- /dev/null +++ b/priv/my_xql_repo/data_migrations_myxql/99999999999999_noop.exs @@ -0,0 +1,7 @@ +defmodule DataMigration.Test.Fixtures.Noop do + @moduledoc false + use Ecto.Migration + + def up, do: :ok + def down, do: :ok +end diff --git a/priv/pg_repo/data_migrations_pg/99999999999999_noop.exs b/priv/pg_repo/data_migrations_pg/99999999999999_noop.exs new file mode 100644 index 0000000..8df7815 --- /dev/null +++ b/priv/pg_repo/data_migrations_pg/99999999999999_noop.exs @@ -0,0 +1,7 @@ +defmodule DataMigration.Test.Fixtures.Noop do + @moduledoc false + use Ecto.Migration + + def up, do: :ok + def down, do: :ok +end diff --git a/priv/sq_lite_repo/data_migrations_sqlite/99999999999999_noop.exs b/priv/sq_lite_repo/data_migrations_sqlite/99999999999999_noop.exs new file mode 100644 index 0000000..8df7815 --- /dev/null +++ b/priv/sq_lite_repo/data_migrations_sqlite/99999999999999_noop.exs @@ -0,0 +1,7 @@ +defmodule DataMigration.Test.Fixtures.Noop do + @moduledoc false + use Ecto.Migration + + def up, do: :ok + def down, do: :ok +end diff --git a/priv/tds_repo/data_migrations_tds/99999999999999_noop.exs b/priv/tds_repo/data_migrations_tds/99999999999999_noop.exs new file mode 100644 index 0000000..8df7815 --- /dev/null +++ b/priv/tds_repo/data_migrations_tds/99999999999999_noop.exs @@ -0,0 +1,7 @@ +defmodule DataMigration.Test.Fixtures.Noop do + @moduledoc false + use Ecto.Migration + + def up, do: :ok + def down, do: :ok +end diff --git a/test/data_migration/live_dashboard/page_test.exs b/test/data_migration/live_dashboard/page_test.exs index 4749ae5..be0a05a 100644 --- a/test/data_migration/live_dashboard/page_test.exs +++ b/test/data_migration/live_dashboard/page_test.exs @@ -54,4 +54,80 @@ defmodule DataMigration.LiveDashboard.PageTest do assert {:ok, _view, _html} = live(conn, route) end end + + describe "list_data_migrations/1 (regression)" do + setup do + :persistent_term.erase(:data_migration_list) + :ok + end + + test "a fresh mount reflects the true status after a migration ran, with no duplicate rows", + %{route: route, conn: conn, repo: repo} do + # `route` is the short config key (e.g. "data_migrations_sqlite"). + # `folder`, below, is what the library itself stores on each + # migration (the path relative to the app dir) — that's what the UI + # actually sends back via `phx-value-folder`, not the short key. + config_folder = Application.get_env(:data_migration, :mounted_at) + + folder = + repo + |> Ecto.Migrator.migrations_path(config_folder) + |> String.split(Application.app_dir(:data_migration) <> "/", parts: 2) + |> List.last() + + repo_param = inspect(repo) + + {:ok, view, html} = live(conn, route) + assert html =~ "down" + + html = + render_click(view, "navigate", %{ + "id" => "99999999999999", + "repo" => repo_param, + "folder" => folder + }) + + assert html =~ "down" + + render_click(view, "are-you-sure-up", %{}) + + html = + render_click(view, "migrate-up", %{ + "id" => "99999999999999", + "repo" => repo_param, + "folder" => folder + }) + + assert html =~ "up" + + # Navigate back to the list and re-open the same migration — each of + # these calls `handle_params` -> `list_data_migrations/1` again, same + # as a real page reload would. It must keep reflecting the true + # status, and the list must not have accumulated a stale duplicate + # row for this migration (a second, unrelated LiveView session on + # this node calling `list_data_migrations/1` concurrently — e.g. the + # "mounts successfully" test above, sharing the same async run — + # could otherwise clobber the process-independent `:persistent_term` + # cache with a stale, pre-migration entry). + list_html = render_click(view, "navigate", %{"action" => "list"}) + + # Exactly one row for this migration in the list — a stale cached + # duplicate would show up as a second `phx-value-id` for the same id. + assert list_html + |> String.split(~s(phx-value-id="99999999999999")) + |> length() == 2 + + show_html = + render_click(view, "navigate", %{ + "id" => "99999999999999", + "repo" => repo_param, + "folder" => folder + }) + + assert show_html =~ ~s(data-status="up") + refute show_html =~ ~s(data-status="down") + + Ecto.Migrator.down(repo, 99_999_999_999_999, DataMigration.Test.Fixtures.Noop) + end + end end From 4e2e4e1456bad17e9ef5fde9fec1bdae5663a224 Mon Sep 17 00:00:00 2001 From: Thomas Weustenfeld Date: Thu, 16 Jul 2026 22:56:56 -0400 Subject: [PATCH 2/3] fix(test): drop manual Migrator.down teardown in regression test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calling Ecto.Migrator.down/3 directly from the test process (as opposed to via the LiveView process under test) crashed under the MyXQL adapter in CI with a DBConnection.ConnectionError / CaseClauseError inside the SQL Sandbox connection state machine — a MyXQL+Sandbox interaction quirk, not a bug in the fix itself. The call was redundant anyway: the SQL Sandbox already rolls back all writes, including migration state, at the end of every test. Co-Authored-By: Claude Sonnet 5 --- test/data_migration/live_dashboard/page_test.exs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/data_migration/live_dashboard/page_test.exs b/test/data_migration/live_dashboard/page_test.exs index be0a05a..022316e 100644 --- a/test/data_migration/live_dashboard/page_test.exs +++ b/test/data_migration/live_dashboard/page_test.exs @@ -126,8 +126,6 @@ defmodule DataMigration.LiveDashboard.PageTest do assert show_html =~ ~s(data-status="up") refute show_html =~ ~s(data-status="down") - - Ecto.Migrator.down(repo, 99_999_999_999_999, DataMigration.Test.Fixtures.Noop) end end end From 04f6b5460d7a245d6f13d88e67b0564f45cf2b78 Mon Sep 17 00:00:00 2001 From: Thomas Weustenfeld Date: Thu, 16 Jul 2026 23:09:41 -0400 Subject: [PATCH 3/3] style: trim comments, move rationale to PR description Co-Authored-By: Claude Sonnet 5 --- lib/data_migration/live_dashboard/page.ex | 22 +++++-------------- .../live_dashboard/page_test.exs | 21 +++++------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/lib/data_migration/live_dashboard/page.ex b/lib/data_migration/live_dashboard/page.ex index 8164d23..3eb6159 100644 --- a/lib/data_migration/live_dashboard/page.ex +++ b/lib/data_migration/live_dashboard/page.ex @@ -426,15 +426,9 @@ defmodule DataMigration.LiveDashboard.Page do end defp compile_file(file, folder) do - # `Code.require_file/2` is idempotent VM-wide (not per-process): once - # any process on this node has required a given path, every later call - # — including from a completely unrelated LiveView session — gets `nil` - # back instead of the compiled module list. Downstream code treats - # `nil` as "this file failed to compile" and silently drops the - # migration, so a second concurrent viewer of this page can see an - # incomplete list. `Code.compile_file/2` always recompiles and always - # returns the module list; silence the resulting "redefining module" - # diagnostic instead. + # Code.require_file/2 is idempotent VM-wide, not per-process, so a + # second caller gets nil back instead of the module list. Use + # compile_file/2 instead and silence the "redefining module" diagnostic. {result, _} = Code.with_diagnostics(fn -> Code.compile_file(file, folder) @@ -470,13 +464,9 @@ defmodule DataMigration.LiveDashboard.Page do end) end - # Keyed by {repo, folder, id} so a repeat call always *replaces* an - # entry rather than appending beside it. `:persistent_term` is - # process-independent (shared by every LiveView session on this node), - # so without this a stale entry from an earlier call — including one - # computed before the most recent migrate up/down — sits in the cache - # forever alongside the fresh one, and callers reading through - # `find_migration/4` or the table's row count can see it. + # Keyed by {repo, folder, id} so a repeat call replaces a stale cached + # entry instead of appending beside it (the persistent_term cache is + # shared across all LiveView sessions on the node). @cache_key :data_migration_list defp list_data_migrations(locations) do existing = @cache_key |> :persistent_term.get(%{}) |> maybe_recompile() diff --git a/test/data_migration/live_dashboard/page_test.exs b/test/data_migration/live_dashboard/page_test.exs index 022316e..fb9f167 100644 --- a/test/data_migration/live_dashboard/page_test.exs +++ b/test/data_migration/live_dashboard/page_test.exs @@ -63,10 +63,8 @@ defmodule DataMigration.LiveDashboard.PageTest do test "a fresh mount reflects the true status after a migration ran, with no duplicate rows", %{route: route, conn: conn, repo: repo} do - # `route` is the short config key (e.g. "data_migrations_sqlite"). - # `folder`, below, is what the library itself stores on each - # migration (the path relative to the app dir) — that's what the UI - # actually sends back via `phx-value-folder`, not the short key. + # `folder` (unlike `route`) must match what the UI sends via + # phx-value-folder: the path relative to the app dir, not the config key. config_folder = Application.get_env(:data_migration, :mounted_at) folder = @@ -100,19 +98,12 @@ defmodule DataMigration.LiveDashboard.PageTest do assert html =~ "up" - # Navigate back to the list and re-open the same migration — each of - # these calls `handle_params` -> `list_data_migrations/1` again, same - # as a real page reload would. It must keep reflecting the true - # status, and the list must not have accumulated a stale duplicate - # row for this migration (a second, unrelated LiveView session on - # this node calling `list_data_migrations/1` concurrently — e.g. the - # "mounts successfully" test above, sharing the same async run — - # could otherwise clobber the process-independent `:persistent_term` - # cache with a stale, pre-migration entry). + # Simulates a reload: re-enters list_data_migrations/1, which a + # concurrent session (e.g. the "mounts successfully" test above, + # same async run) could otherwise have clobbered with a stale entry. list_html = render_click(view, "navigate", %{"action" => "list"}) - # Exactly one row for this migration in the list — a stale cached - # duplicate would show up as a second `phx-value-id` for the same id. + # A stale cached duplicate would show up as a second phx-value-id. assert list_html |> String.split(~s(phx-value-id="99999999999999")) |> length() == 2