Skip to content

Commit 913f936

Browse files
committed
chore: fix tests & don't do normal rollbacks with --tenant option
1 parent 89ca1fe commit 913f936

File tree

5 files changed

+69
-84
lines changed

5 files changed

+69
-84
lines changed

lib/data_layer.ex

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -453,72 +453,74 @@ defmodule AshPostgres.DataLayer do
453453
migrations_path = AshPostgres.Mix.Helpers.migrations_path([], repo)
454454
tenant_migrations_path = AshPostgres.Mix.Helpers.tenant_migrations_path([], repo)
455455

456-
current_migrations =
457-
Ecto.Query.from(row in "schema_migrations",
458-
select: row.version
459-
)
460-
|> repo.all()
461-
|> Enum.map(&to_string/1)
456+
if "--tenants" not in args do
457+
current_migrations =
458+
Ecto.Query.from(row in "schema_migrations",
459+
select: row.version
460+
)
461+
|> repo.all()
462+
|> Enum.map(&to_string/1)
463+
464+
files =
465+
migrations_path
466+
|> Path.join("**/*.exs")
467+
|> Path.wildcard()
468+
|> Enum.sort()
469+
|> Enum.reverse()
470+
|> Enum.filter(fn file ->
471+
Enum.any?(current_migrations, &String.starts_with?(Path.basename(file), &1))
472+
end)
473+
|> Enum.take(20)
474+
|> Enum.map(&String.trim_leading(&1, migrations_path))
475+
|> Enum.map(&String.trim_leading(&1, "/"))
476+
477+
indexed =
478+
files
479+
|> Enum.with_index()
480+
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
481+
482+
to =
483+
Mix.shell().prompt(
484+
"""
485+
How many migrations should be rolled back#{for_repo}? (default: 0)
486+
487+
Last 20 migration names, with the input you must provide to
488+
rollback up to *and including* that migration:
489+
490+
#{Enum.join(indexed, "\n")}
491+
Rollback to:
492+
"""
493+
|> String.trim_trailing()
494+
)
495+
|> String.trim()
496+
|> case do
497+
"" ->
498+
nil
462499

463-
files =
464-
migrations_path
465-
|> Path.join("**/*.exs")
466-
|> Path.wildcard()
467-
|> Enum.sort()
468-
|> Enum.reverse()
469-
|> Enum.filter(fn file ->
470-
Enum.any?(current_migrations, &String.starts_with?(Path.basename(file), &1))
471-
end)
472-
|> Enum.take(20)
473-
|> Enum.map(&String.trim_leading(&1, migrations_path))
474-
|> Enum.map(&String.trim_leading(&1, "/"))
475-
476-
indexed =
477-
files
478-
|> Enum.with_index()
479-
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
480-
481-
to =
482-
Mix.shell().prompt(
483-
"""
484-
How many migrations should be rolled back#{for_repo}? (default: 0)
485-
486-
Last 20 migration names, with the input you must provide to
487-
rollback up to *and including* that migration:
488-
489-
#{Enum.join(indexed, "\n")}
490-
Rollback to:
491-
"""
492-
|> String.trim_trailing()
493-
)
494-
|> String.trim()
495-
|> case do
496-
"" ->
497-
nil
498-
499-
"0" ->
500-
nil
501-
502-
n ->
503-
try do
504-
files
505-
|> Enum.at(String.to_integer(n) - 1)
506-
rescue
507-
_ ->
508-
reraise "Required an integer value, got: #{n}", __STACKTRACE__
509-
end
510-
|> String.split("_", parts: 2)
511-
|> Enum.at(0)
512-
|> String.to_integer()
513-
end
500+
"0" ->
501+
nil
514502

515-
if to do
516-
Mix.Task.run(
517-
"ash_postgres.rollback",
518-
args ++ ["-r", inspect(repo), "--to", to_string(to)]
519-
)
503+
n ->
504+
try do
505+
files
506+
|> Enum.at(String.to_integer(n) - 1)
507+
rescue
508+
_ ->
509+
reraise "Required an integer value, got: #{n}", __STACKTRACE__
510+
end
511+
|> String.split("_", parts: 2)
512+
|> Enum.at(0)
513+
|> String.to_integer()
514+
end
520515

521-
Mix.Task.reenable("ash_postgres.rollback")
516+
if to do
517+
Mix.Task.run(
518+
"ash_postgres.rollback",
519+
args ++ ["-r", inspect(repo), "--to", to_string(to)]
520+
)
521+
522+
Mix.Task.reenable("ash_postgres.rollback")
523+
end
522524
end
523525

524526
tenant_files =
@@ -599,7 +601,7 @@ defmodule AshPostgres.DataLayer do
599601
if to do
600602
Mix.Task.run(
601603
"ash_postgres.rollback",
602-
args ++ ["--tenants", "-r", inspect(repo), "--to", to]
604+
args ++ ["--tenants", "-r", inspect(repo), "--to", to_string(to)]
603605
)
604606

605607
Mix.Task.reenable("ash_postgres.rollback")

lib/migration_generator/migration_generator.ex

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,6 @@ defmodule AshPostgres.MigrationGenerator do
514514
end
515515
end
516516

517-
if Mix.env() == :test do
518-
defp with_repo_not_in_test(repo, fun) do
519-
fun.(repo)
520-
end
521-
else
522-
defp with_repo_not_in_test(repo, fun) do
523-
Ecto.Migrator.with_repo(repo, fun)
524-
end
525-
end
526-
527517
defp require_name!(opts) do
528518
if !opts.name && !opts.dry_run && !opts.check && !opts.snapshots_only && !opts.dev &&
529519
!opts.auto_name do
@@ -548,7 +538,7 @@ defmodule AshPostgres.MigrationGenerator do
548538
end)
549539

550540
if tenant? do
551-
with_repo_not_in_test(repo, fn repo ->
541+
Ecto.Migrator.with_repo(repo, fn repo ->
552542
for prefix <- repo.all_tenants() do
553543
{repo, query, opts} = Ecto.Migration.SchemaMigration.versions(repo, [], prefix)
554544

@@ -583,7 +573,7 @@ defmodule AshPostgres.MigrationGenerator do
583573
end
584574
end)
585575
else
586-
with_repo_not_in_test(repo, fn repo ->
576+
Ecto.Migrator.with_repo(repo, fn repo ->
587577
{repo, query, opts} = Ecto.Migration.SchemaMigration.versions(repo, [], nil)
588578

589579
repo.transaction(fn ->

priv/resource_snapshots/test_repo/authors/20251018130654_dev.json.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

priv/resource_snapshots/test_repo/authors/20251018130654_dev.json renamed to priv/resource_snapshots/test_repo/authors/20251020024026.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"custom_indexes": [],
121121
"custom_statements": [],
122122
"has_create_action": true,
123-
"hash": "55CFEDA4CB07DD89D2807B080A6F0E14A0370D7EA0C48E67C36ACAC568137D95",
123+
"hash": "DEDCEFDB71DE94818B8D624B0BFE438AD7AEDC6279FD05B83C11C216E7A0F991",
124124
"identities": [],
125125
"multitenancy": {
126126
"attribute": null,

priv/test_repo/migrations/20251018130654_migrate_resources64_dev.exs renamed to priv/test_repo/migrations/20251020024026_migrate_resources64.exs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
2-
#
3-
# SPDX-License-Identifier: MIT
4-
51
defmodule AshPostgres.TestRepo.Migrations.MigrateResources64 do
62
@moduledoc """
73
Updates resources based on their most recent snapshots.

0 commit comments

Comments
 (0)