Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions lib/data_migration/live_dashboard/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule DataMigration.Test.Fixtures.Noop do
@moduledoc false
use Ecto.Migration

def up, do: :ok
def down, do: :ok
end
7 changes: 7 additions & 0 deletions priv/pg_repo/data_migrations_pg/99999999999999_noop.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule DataMigration.Test.Fixtures.Noop do
@moduledoc false
use Ecto.Migration

def up, do: :ok
def down, do: :ok
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule DataMigration.Test.Fixtures.Noop do
@moduledoc false
use Ecto.Migration

def up, do: :ok
def down, do: :ok
end
7 changes: 7 additions & 0 deletions priv/tds_repo/data_migrations_tds/99999999999999_noop.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule DataMigration.Test.Fixtures.Noop do
@moduledoc false
use Ecto.Migration

def up, do: :ok
def down, do: :ok
end
65 changes: 65 additions & 0 deletions test/data_migration/live_dashboard/page_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading