Skip to content

Commit

Permalink
chore: update Mix.Helpers naming
Browse files Browse the repository at this point in the history
improvement: support `mix ash.rollback`
  • Loading branch information
zachdaniel committed Apr 10, 2024
1 parent e17ce61 commit 0cc6401
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 15 deletions.
55 changes: 55 additions & 0 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,61 @@ defmodule AshSqlite.DataLayer do
Mix.Task.run("ash_sqlite.migrate", args)
end

def rollback(args) do
repos = AshSqlite.Mix.Helpers.repos!([], args)

show_for_repo? = Enum.count_until(repos, 2) == 2

for repo <- repos do
for_repo =
if show_for_repo? do
" for repo #{inspect(repo)}"
else
""
end

migrations_path = AshSqlite.Mix.Helpers.migrations_path([], repo)

files =
migrations_path
|> Path.join("**/*.exs")
|> Path.wildcard()
|> Enum.sort()
|> Enum.reverse()
|> Enum.take(20)
|> Enum.map(&String.trim_leading(&1, migrations_path))
|> Enum.with_index()
|> Enum.map(fn {file, index} -> "#{index + 1}: #{file}" end)

n =
Mix.shell().prompt("""
How many migrations should be rolled back#{for_repo}? (default: 0)
Last 20 migration names, with the input you must provide to
rollback up to *and including* that migration:
#{Enum.join(files, "\n")}
Rollback to:
""" |> String.trim_trailing())
|> String.trim()
|> case do
"" ->
0

n ->
try do
String.to_integer(n)
rescue
_ ->
raise "Required an integer value, got: #{n}"

Check warning on line 352 in lib/data_layer.ex

View workflow job for this annotation

GitHub Actions / ash-ci / mix credo --strict

Use `reraise` inside a rescue block to preserve the original stacktrace.
end
end

Mix.Task.run("ash_postgres.rollback", args ++ ["-r", inspect(repo), "-n", to_string(n)])
Mix.Task.reenable("ash_postgres.rollback")
end
end

def codegen(args) do
# TODO: take args that we care about
Mix.Task.run("ash_sqlite.generate_migrations", args)
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule AshSqlite.MixHelpers do
defmodule AshSqlite.Mix.Helpers do
@moduledoc false
def domains!(opts, args) do
apps =
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/tasks/ash_sqlite.create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ defmodule Mix.Tasks.AshSqlite.Create do
def run(args) do
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

repos = AshSqlite.MixHelpers.repos!(opts, args)
repos = AshSqlite.Mix.Helpers.repos!(opts, args)

repo_args =
Enum.flat_map(repos, fn repo ->
["-r", to_string(repo)]
end)

rest_opts = AshSqlite.MixHelpers.delete_arg(args, "--domains")
rest_opts = AshSqlite.Mix.Helpers.delete_arg(args, "--domains")

Mix.Task.reenable("ecto.create")

Expand Down
4 changes: 2 additions & 2 deletions lib/mix/tasks/ash_sqlite.drop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ defmodule Mix.Tasks.AshSqlite.Drop do
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
opts = Keyword.merge(@default_opts, opts)

repos = AshSqlite.MixHelpers.repos!(opts, args)
repos = AshSqlite.Mix.Helpers.repos!(opts, args)

repo_args =
Enum.flat_map(repos, fn repo ->
["-r", to_string(repo)]
end)

rest_opts = AshSqlite.MixHelpers.delete_arg(args, "--domains")
rest_opts = AshSqlite.Mix.Helpers.delete_arg(args, "--domains")

Mix.Task.reenable("ecto.drop")

Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/ash_sqlite.generate_migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule Mix.Tasks.AshSqlite.GenerateMigrations do
]
)

domains = AshSqlite.MixHelpers.domains!(opts, args)
domains = AshSqlite.Mix.Helpers.domains!(opts, args)

opts =
opts
Expand Down
8 changes: 4 additions & 4 deletions lib/mix/tasks/ash_sqlite.migrate.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mix.Tasks.AshSqlite.Migrate do
use Mix.Task

import AshSqlite.MixHelpers,
import AshSqlite.Mix.Helpers,
only: [migrations_path: 2]

@shortdoc "Runs the repository migrations for all repositories in the provided (or congigured) domains"
Expand Down Expand Up @@ -90,7 +90,7 @@ defmodule Mix.Tasks.AshSqlite.Migrate do
def run(args) do
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

repos = AshSqlite.MixHelpers.repos!(opts, args)
repos = AshSqlite.Mix.Helpers.repos!(opts, args)

repo_args =
Enum.flat_map(repos, fn repo ->
Expand All @@ -99,8 +99,8 @@ defmodule Mix.Tasks.AshSqlite.Migrate do

rest_opts =
args
|> AshSqlite.MixHelpers.delete_arg("--domains")
|> AshSqlite.MixHelpers.delete_arg("--migrations-path")
|> AshSqlite.Mix.Helpers.delete_arg("--domains")
|> AshSqlite.Mix.Helpers.delete_arg("--migrations-path")

Mix.Task.reenable("ecto.migrate")

Expand Down
8 changes: 4 additions & 4 deletions lib/mix/tasks/ash_sqlite.rollback.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mix.Tasks.AshSqlite.Rollback do
use Mix.Task

import AshSqlite.MixHelpers,
import AshSqlite.Mix.Helpers,
only: [migrations_path: 2]

@shortdoc "Rolls back the repository migrations for all repositories in the provided (or configured) domains"
Expand Down Expand Up @@ -55,7 +55,7 @@ defmodule Mix.Tasks.AshSqlite.Rollback do
aliases: [n: :step, v: :to]
)

repos = AshSqlite.MixHelpers.repos!(opts, args)
repos = AshSqlite.Mix.Helpers.repos!(opts, args)

repo_args =
Enum.flat_map(repos, fn repo ->
Expand All @@ -64,8 +64,8 @@ defmodule Mix.Tasks.AshSqlite.Rollback do

rest_opts =
args
|> AshSqlite.MixHelpers.delete_arg("--domains")
|> AshSqlite.MixHelpers.delete_arg("--migrations-path")
|> AshSqlite.Mix.Helpers.delete_arg("--domains")
|> AshSqlite.Mix.Helpers.delete_arg("--migrations-path")

Mix.Task.reenable("ecto.rollback")

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"ash": {:hex, :ash, "3.0.0-rc.6", "78d9bc068a0c632e4fe2db8a8802f772c65329c8bc15877ceb6eb2ac83e1fa8b", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.8", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.1.7 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.6", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3e0ccc857572d10972868886aff46f9b1d11c90f8b357f85f2887e71f702e916"},
"ash_sql": {:hex, :ash_sql, "0.1.1-rc.2", "281e036180ea069c24239ea051fd6551708c21a0690b099acb326d3d7005302e", [:mix], [{:ash, "~> 3.0.0-rc.0", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "76a21857b8d823ee47732c20746830732be9a005c72b11db6bd8e203e459a11c"},
"ash_sql": {:hex, :ash_sql, "0.1.1-rc.2", "7e5b313b0a15da109e0f86c7a0e4c4a045668f22f8eed2cdf7d9d04976bd183e", [:mix], [{:ash, "~> 3.0.0-rc.0", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "ea394738d9fd6edccffb5068501b0168de9acc146ed7765652d00fa290afa642"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"cc_precompiler": {:hex, :cc_precompiler, "0.1.10", "47c9c08d8869cf09b41da36538f62bc1abd3e19e41701c2cea2675b53c704258", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "f6e046254e53cd6b41c6bacd70ae728011aa82b2742a80d6e2214855c6e06b22"},
"comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"},
Expand Down

0 comments on commit 0cc6401

Please sign in to comment.