fix: list_tenants -> all_tenants

fix: when checking for roll back-able migrations, only check `Path.basename`
This commit is contained in:
Zach Daniel 2024-06-10 09:08:11 -04:00
parent 6c81699b0d
commit f0779580b3

View file

@ -447,7 +447,7 @@ defmodule AshPostgres.DataLayer do
|> Enum.sort() |> Enum.sort()
|> Enum.reverse() |> Enum.reverse()
|> Enum.filter(fn file -> |> Enum.filter(fn file ->
Enum.any?(current_migrations, &String.starts_with?(file, &1)) Enum.any?(current_migrations, &String.starts_with?(Path.basename(file), &1))
end) end)
|> Enum.take(20) |> Enum.take(20)
|> Enum.map(&String.trim_leading(&1, migrations_path)) |> Enum.map(&String.trim_leading(&1, migrations_path))
@ -484,31 +484,37 @@ defmodule AshPostgres.DataLayer do
Mix.Task.run("ash_postgres.rollback", args ++ ["-r", inspect(repo), "-n", to_string(n)]) Mix.Task.run("ash_postgres.rollback", args ++ ["-r", inspect(repo), "-n", to_string(n)])
Mix.Task.reenable("ash_postgres.rollback") Mix.Task.reenable("ash_postgres.rollback")
first_tenant = repo.list_tenants() |> Enum.at(0) tenant_files =
tenant_migrations_path
|> Path.join("**/*.exs")
|> Path.wildcard()
|> Enum.sort()
|> Enum.reverse()
if first_tenant do if !Enum.empty?(tenant_files) do
current_tenant_migrations = first_tenant = repo.all_tenants() |> Enum.at(0)
Ecto.Query.from(row in "schema_migrations",
select: row.version
)
|> repo.all(prefix: first_tenant)
|> Enum.map(&to_string/1)
tenant_files = if first_tenant do
tenant_migrations_path current_tenant_migrations =
|> Path.join("**/*.exs") Ecto.Query.from(row in "schema_migrations",
|> Path.wildcard() select: row.version
|> Enum.sort() )
|> Enum.reverse() |> repo.all(prefix: first_tenant)
|> Enum.filter(fn file -> |> Enum.map(&to_string/1)
Enum.any?(current_tenant_migrations, &String.starts_with?(file, &1))
end) tenant_files =
|> Enum.take(20) tenant_files
|> Enum.map(&String.trim_leading(&1, tenant_migrations_path)) |> Enum.filter(fn file ->
|> Enum.with_index() Enum.any?(
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end) current_tenant_migrations,
&String.starts_with?(Path.basename(file), &1)
)
end)
|> Enum.take(20)
|> Enum.map(&String.trim_leading(&1, tenant_migrations_path))
|> Enum.with_index()
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
if !Enum.empty?(tenant_files) do
n = n =
Mix.shell().prompt( Mix.shell().prompt(
""" """
@ -565,13 +571,7 @@ defmodule AshPostgres.DataLayer do
[] []
|> AshPostgres.Mix.Helpers.repos!(args) |> AshPostgres.Mix.Helpers.repos!(args)
|> Enum.all?(fn repo -> |> Enum.all?(&(not has_tenant_migrations?(&1)))
[]
|> AshPostgres.Mix.Helpers.tenant_migrations_path(repo)
|> Path.join("**/*.exs")
|> Path.wildcard()
|> Enum.empty?()
end)
|> case do |> case do
true -> true ->
:ok :ok
@ -586,6 +586,14 @@ defmodule AshPostgres.DataLayer do
Mix.Task.run("ash_postgres.drop", args) Mix.Task.run("ash_postgres.drop", args)
end end
defp has_tenant_migrations?(repo) do
[]
|> AshPostgres.Mix.Helpers.tenant_migrations_path(repo)
|> Path.join("**/*.exs")
|> Path.wildcard()
|> Enum.empty?()
end
import Ecto.Query, only: [from: 2, subquery: 1] import Ecto.Query, only: [from: 2, subquery: 1]
@impl true @impl true