diff --git a/lib/data_migration/live_dashboard/page.ex b/lib/data_migration/live_dashboard/page.ex index 33401d0..3eb6159 100644 --- a/lib/data_migration/live_dashboard/page.ex +++ b/lib/data_migration/live_dashboard/page.ex @@ -410,26 +410,28 @@ 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, 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.require_file(file, folder) + Code.compile_file(file, folder) end) result @@ -462,11 +464,14 @@ defmodule DataMigration.LiveDashboard.Page do end) end + # 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() + 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 +486,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..fb9f167 100644 --- a/test/data_migration/live_dashboard/page_test.exs +++ b/test/data_migration/live_dashboard/page_test.exs @@ -54,4 +54,69 @@ 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 + # `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 = + 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" + + # 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"}) + + # 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 + + 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") + end + end end