Skip to content

Commit f077958

Browse files
committed
fix: list_tenants -> all_tenants
fix: when checking for roll back-able migrations, only check `Path.basename`
1 parent 6c81699 commit f077958

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

lib/data_layer.ex

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ defmodule AshPostgres.DataLayer do
447447
|> Enum.sort()
448448
|> Enum.reverse()
449449
|> Enum.filter(fn file ->
450-
Enum.any?(current_migrations, &String.starts_with?(file, &1))
450+
Enum.any?(current_migrations, &String.starts_with?(Path.basename(file), &1))
451451
end)
452452
|> Enum.take(20)
453453
|> Enum.map(&String.trim_leading(&1, migrations_path))
@@ -484,31 +484,37 @@ defmodule AshPostgres.DataLayer do
484484
Mix.Task.run("ash_postgres.rollback", args ++ ["-r", inspect(repo), "-n", to_string(n)])
485485
Mix.Task.reenable("ash_postgres.rollback")
486486

487-
first_tenant = repo.list_tenants() |> Enum.at(0)
487+
tenant_files =
488+
tenant_migrations_path
489+
|> Path.join("**/*.exs")
490+
|> Path.wildcard()
491+
|> Enum.sort()
492+
|> Enum.reverse()
493+
494+
if !Enum.empty?(tenant_files) do
495+
first_tenant = repo.all_tenants() |> Enum.at(0)
496+
497+
if first_tenant do
498+
current_tenant_migrations =
499+
Ecto.Query.from(row in "schema_migrations",
500+
select: row.version
501+
)
502+
|> repo.all(prefix: first_tenant)
503+
|> Enum.map(&to_string/1)
504+
505+
tenant_files =
506+
tenant_files
507+
|> Enum.filter(fn file ->
508+
Enum.any?(
509+
current_tenant_migrations,
510+
&String.starts_with?(Path.basename(file), &1)
511+
)
512+
end)
513+
|> Enum.take(20)
514+
|> Enum.map(&String.trim_leading(&1, tenant_migrations_path))
515+
|> Enum.with_index()
516+
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
488517

489-
if first_tenant do
490-
current_tenant_migrations =
491-
Ecto.Query.from(row in "schema_migrations",
492-
select: row.version
493-
)
494-
|> repo.all(prefix: first_tenant)
495-
|> Enum.map(&to_string/1)
496-
497-
tenant_files =
498-
tenant_migrations_path
499-
|> Path.join("**/*.exs")
500-
|> Path.wildcard()
501-
|> Enum.sort()
502-
|> Enum.reverse()
503-
|> Enum.filter(fn file ->
504-
Enum.any?(current_tenant_migrations, &String.starts_with?(file, &1))
505-
end)
506-
|> Enum.take(20)
507-
|> Enum.map(&String.trim_leading(&1, tenant_migrations_path))
508-
|> Enum.with_index()
509-
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
510-
511-
if !Enum.empty?(tenant_files) do
512518
n =
513519
Mix.shell().prompt(
514520
"""
@@ -565,13 +571,7 @@ defmodule AshPostgres.DataLayer do
565571

566572
[]
567573
|> AshPostgres.Mix.Helpers.repos!(args)
568-
|> Enum.all?(fn repo ->
569-
[]
570-
|> AshPostgres.Mix.Helpers.tenant_migrations_path(repo)
571-
|> Path.join("**/*.exs")
572-
|> Path.wildcard()
573-
|> Enum.empty?()
574-
end)
574+
|> Enum.all?(&(not has_tenant_migrations?(&1)))
575575
|> case do
576576
true ->
577577
:ok
@@ -586,6 +586,14 @@ defmodule AshPostgres.DataLayer do
586586
Mix.Task.run("ash_postgres.drop", args)
587587
end
588588

589+
defp has_tenant_migrations?(repo) do
590+
[]
591+
|> AshPostgres.Mix.Helpers.tenant_migrations_path(repo)
592+
|> Path.join("**/*.exs")
593+
|> Path.wildcard()
594+
|> Enum.empty?()
595+
end
596+
589597
import Ecto.Query, only: [from: 2, subquery: 1]
590598

591599
@impl true

0 commit comments

Comments
 (0)