Skip to content

Commit 6b3c2c8

Browse files
committed
improvement: make rollback more reliable by using --to instead of -n
1 parent 0ea1b2c commit 6b3c2c8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

lib/data_layer.ex

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,39 +333,52 @@ defmodule AshSqlite.DataLayer do
333333
|> Enum.reverse()
334334
|> Enum.take(20)
335335
|> Enum.map(&String.trim_leading(&1, migrations_path))
336+
|> Enum.map(&String.trim_leading(&1, "/"))
337+
338+
indexed =
339+
files
336340
|> Enum.with_index()
337341
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)
338342

339-
n =
343+
to =
340344
Mix.shell().prompt(
341345
"""
342346
How many migrations should be rolled back#{for_repo}? (default: 0)
343347
344348
Last 20 migration names, with the input you must provide to
345349
rollback up to *and including* that migration:
346350
347-
#{Enum.join(files, "\n")}
351+
#{Enum.join(indexed, "\n")}
348352
Rollback to:
349353
"""
350354
|> String.trim_trailing()
351355
)
352356
|> String.trim()
353357
|> case do
354358
"" ->
355-
0
359+
nil
360+
361+
"0" ->
362+
nil
356363

357364
n ->
358365
try do
359-
String.to_integer(n)
366+
files
367+
|> Enum.at(String.to_integer(n) - 1)
360368
rescue
361369
_ ->
362370
# credo:disable-for-next-line
363371
raise "Required an integer value, got: #{n}"
364372
end
373+
|> String.split("_", parts: 2)
374+
|> Enum.at(0)
375+
|> String.to_integer()
365376
end
366377

367-
Mix.Task.run("ash_sqlite.rollback", args ++ ["-r", inspect(repo), "-n", to_string(n)])
368-
Mix.Task.reenable("ash_sqlite.rollback")
378+
if to do
379+
Mix.Task.run("ash_sqlite.rollback", args ++ ["-r", inspect(repo), "--to", to_string(to)])
380+
Mix.Task.reenable("ash_sqlite.rollback")
381+
end
369382
end
370383
end
371384

0 commit comments

Comments
 (0)