Skip to content

Commit de90abd

Browse files
committed
improvement: make rollbacks safer by using --to instead of -n
1 parent d174139 commit de90abd

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

lib/data_layer.ex

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -459,38 +459,55 @@ defmodule AshPostgres.DataLayer do
459459
end)
460460
|> Enum.take(20)
461461
|> Enum.map(&String.trim_leading(&1, migrations_path))
462+
|> Enum.map(&String.trim_leading(&1, "/"))
463+
464+
indexed =
465+
files
462466
|> Enum.with_index()
463467
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
464468

465-
n =
469+
to =
466470
Mix.shell().prompt(
467471
"""
468472
How many migrations should be rolled back#{for_repo}? (default: 0)
469473
470474
Last 20 migration names, with the input you must provide to
471475
rollback up to *and including* that migration:
472476
473-
#{Enum.join(files, "\n")}
477+
#{Enum.join(indexed, "\n")}
474478
Rollback to:
475479
"""
476480
|> String.trim_trailing()
477481
)
478482
|> String.trim()
479483
|> case do
480484
"" ->
481-
0
485+
nil
486+
487+
"0" ->
488+
nil
482489

483490
n ->
484491
try do
485-
String.to_integer(n)
492+
files
493+
|> Enum.at(String.to_integer(n) - 1)
486494
rescue
487495
_ ->
488496
reraise "Required an integer value, got: #{n}", __STACKTRACE__
489497
end
498+
|> String.split("_", parts: 2)
499+
|> Enum.at(0)
500+
|> String.to_integer()
490501
end
491502

492-
Mix.Task.run("ash_postgres.rollback", args ++ ["-r", inspect(repo), "-n", to_string(n)])
493-
Mix.Task.reenable("ash_postgres.rollback")
503+
if to do
504+
Mix.Task.run(
505+
"ash_postgres.rollback",
506+
args ++ ["-r", inspect(repo), "--to", to_string(to)]
507+
)
508+
509+
Mix.Task.reenable("ash_postgres.rollback")
510+
end
494511

495512
tenant_files =
496513
tenant_migrations_path
@@ -520,10 +537,14 @@ defmodule AshPostgres.DataLayer do
520537
end)
521538
|> Enum.take(20)
522539
|> Enum.map(&String.trim_leading(&1, tenant_migrations_path))
540+
|> Enum.map(&String.trim_leading(&1, "/"))
541+
542+
indexed =
543+
tenant_files
523544
|> Enum.with_index()
524545
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
525546

526-
n =
547+
to =
527548
Mix.shell().prompt(
528549
"""
529550
@@ -536,7 +557,7 @@ defmodule AshPostgres.DataLayer do
536557
Last 20 migration names, with the input you must provide to
537558
rollback up to *and including* that migration:
538559
539-
#{Enum.join(tenant_files, "\n")}
560+
#{Enum.join(indexed, "\n")}
540561
541562
Rollback to:
542563
"""
@@ -545,23 +566,32 @@ defmodule AshPostgres.DataLayer do
545566
|> String.trim()
546567
|> case do
547568
"" ->
548-
0
569+
nil
570+
571+
"0" ->
572+
nil
549573

550574
n ->
551575
try do
552-
String.to_integer(n)
576+
tenant_files
577+
|> Enum.at(String.to_integer(n) - 1)
553578
rescue
554579
_ ->
555580
reraise "Required an integer value, got: #{n}", __STACKTRACE__
556581
end
582+
|> String.split("_", parts: 2)
583+
|> Enum.at(0)
584+
|> String.to_integer()
557585
end
558586

559-
Mix.Task.run(
560-
"ash_postgres.rollback",
561-
args ++ ["--tenants", "-r", inspect(repo), "-n", to_string(n)]
562-
)
587+
if to do
588+
Mix.Task.run(
589+
"ash_postgres.rollback",
590+
args ++ ["--tenants", "-r", inspect(repo), "--to", to]
591+
)
563592

564-
Mix.Task.reenable("ash_postgres.rollback")
593+
Mix.Task.reenable("ash_postgres.rollback")
594+
end
565595
end
566596
end
567597
end)

0 commit comments

Comments
 (0)