Skip to content

Commit

Permalink
format mix/* files
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski committed Jul 22, 2023
1 parent 04f1b1e commit 2bfbd0b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 47 deletions.
10 changes: 5 additions & 5 deletions lib/mix/ecto_sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Mix.EctoSQL do
@doc """
Ensures the given repository's migrations paths exists on the file system.
"""
@spec ensure_migrations_paths(Ecto.Repo.t, Keyword.t) :: [String.t]
@spec ensure_migrations_paths(Ecto.Repo.t(), Keyword.t()) :: [String.t()]
def ensure_migrations_paths(repo, opts) do
paths = Keyword.get_values(opts, :migrations_path)
paths = if paths == [], do: [Path.join(source_repo_priv(repo), "migrations")], else: paths
Expand All @@ -19,9 +19,9 @@ defmodule Mix.EctoSQL do
end

defp raise_missing_migrations(path, repo) do
Mix.raise """
Could not find migrations directory #{inspect path}
for repo #{inspect repo}.
Mix.raise("""
Could not find migrations directory #{inspect(path)}
for repo #{inspect(repo)}.
This may be because you are in a new project and the
migration directory has not been created yet. Creating an
Expand All @@ -30,7 +30,7 @@ defmodule Mix.EctoSQL do
If you expected existing migrations to be found, please
make sure your repository has been properly configured
and the configured path exists.
"""
""")
end

@doc """
Expand Down
35 changes: 23 additions & 12 deletions lib/mix/tasks/ecto.dump.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,27 @@ defmodule Mix.Tasks.Ecto.Dump do

@impl true
def run(args) do
{opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

dump_prefixes =
case Keyword.get_values(opts, :prefix) do
[_ | _] = prefixes -> prefixes
[] -> nil
end

opts = @default_opts
|> Keyword.merge(opts)
|> Keyword.put(:dump_prefixes, dump_prefixes)
opts =
@default_opts
|> Keyword.merge(opts)
|> Keyword.put(:dump_prefixes, dump_prefixes)

Enum.each parse_repo(args), fn repo ->
Enum.each(parse_repo(args), fn repo ->
ensure_repo(repo, args)
ensure_implements(repo.__adapter__(), Ecto.Adapter.Structure,
"dump structure for #{inspect repo}")

ensure_implements(
repo.__adapter__(),
Ecto.Adapter.Structure,
"dump structure for #{inspect(repo)}"
)

migration_repo = repo.config()[:migration_repo] || repo

Expand All @@ -86,16 +91,22 @@ defmodule Mix.Tasks.Ecto.Dump do
case repo.__adapter__().structure_dump(source_repo_priv(repo), config) do
{:ok, location} ->
unless opts[:quiet] do
elapsed = System.convert_time_unit(System.system_time() - start_time, :native, :microsecond)
Mix.shell().info "The structure for #{inspect repo} has been dumped to #{location} in #{format_time(elapsed)}"
elapsed =
System.convert_time_unit(System.system_time() - start_time, :native, :microsecond)

Mix.shell().info(
"The structure for #{inspect(repo)} has been dumped to #{location} in #{format_time(elapsed)}"
)
end

{:error, term} when is_binary(term) ->
Mix.raise "The structure for #{inspect repo} couldn't be dumped: #{term}"
Mix.raise("The structure for #{inspect(repo)} couldn't be dumped: #{term}")

{:error, term} ->
Mix.raise "The structure for #{inspect repo} couldn't be dumped: #{inspect term}"
Mix.raise("The structure for #{inspect(repo)} couldn't be dumped: #{inspect(term)}")
end
end
end
end)
end

defp format_time(microsec) when microsec < 1_000, do: "#{microsec} μs"
Expand Down
33 changes: 21 additions & 12 deletions lib/mix/tasks/ecto.gen.migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,44 @@ defmodule Mix.Tasks.Ecto.Gen.Migration do
def run(args) do
repos = parse_repo(args)

Enum.map repos, fn repo ->
Enum.map(repos, fn repo ->
case OptionParser.parse!(args, strict: @switches, aliases: @aliases) do
{opts, [name]} ->
ensure_repo(repo, args)
path = opts[:migrations_path] || Path.join(source_repo_priv(repo), "migrations")
base_name = "#{underscore(name)}.exs"
file = Path.join(path, "#{timestamp()}_#{base_name}")
unless File.dir?(path), do: create_directory path
unless File.dir?(path), do: create_directory(path)

fuzzy_path = Path.join(path, "*_#{base_name}")

if Path.wildcard(fuzzy_path) != [] do
Mix.raise "migration can't be created, there is already a migration file with name #{name}."
Mix.raise(
"migration can't be created, there is already a migration file with name #{name}."
)
end

# The :change option may be used by other tasks but not the CLI
assigns = [mod: Module.concat([repo, Migrations, camelize(name)]), change: opts[:change]]
create_file file, migration_template(assigns)
assigns = [
mod: Module.concat([repo, Migrations, camelize(name)]),
change: opts[:change]
]

create_file(file, migration_template(assigns))

if open?(file) and Mix.shell().yes?("Do you want to run this migration?") do
Mix.Task.run "ecto.migrate", ["-r", inspect(repo), "--migrations-path", path]
Mix.Task.run("ecto.migrate", ["-r", inspect(repo), "--migrations-path", path])
end

file

{_, _} ->
Mix.raise "expected ecto.gen.migration to receive the migration file name, " <>
"got: #{inspect Enum.join(args, " ")}"
Mix.raise(
"expected ecto.gen.migration to receive the migration file name, " <>
"got: #{inspect(Enum.join(args, " "))}"
)
end
end
end)
end

defp timestamp do
Expand All @@ -105,17 +114,17 @@ defmodule Mix.Tasks.Ecto.Gen.Migration do
defp migration_module do
case Application.get_env(:ecto_sql, :migration_module, Ecto.Migration) do
migration_module when is_atom(migration_module) -> migration_module
other -> Mix.raise "Expected :migration_module to be a module, got: #{inspect(other)}"
other -> Mix.raise("Expected :migration_module to be a module, got: #{inspect(other)}")
end
end

embed_template :migration, """
embed_template(:migration, """
defmodule <%= inspect @mod %> do
use <%= inspect migration_module() %>
def change do
<%= @change %>
end
end
"""
""")
end
25 changes: 17 additions & 8 deletions lib/mix/tasks/ecto.load.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ defmodule Mix.Tasks.Ecto.Load do
"load structure for #{inspect(repo)}"
)

{migration_repo, source} = Ecto.Migration.SchemaMigration.get_repo_and_source(repo, repo.config())
{:ok, loaded?, _} = Ecto.Migrator.with_repo(migration_repo, table_exists_closure(table_exists?, source, opts))
{migration_repo, source} =
Ecto.Migration.SchemaMigration.get_repo_and_source(repo, repo.config())

{:ok, loaded?, _} =
Ecto.Migrator.with_repo(migration_repo, table_exists_closure(table_exists?, source, opts))

for repo <- Enum.uniq([repo, migration_repo]) do
cond do
Expand Down Expand Up @@ -117,20 +120,26 @@ defmodule Mix.Tasks.Ecto.Load do
defp load_structure(repo, opts) do
config = Keyword.merge(repo.config(), opts)
start_time = System.system_time()

case repo.__adapter__().structure_load(source_repo_priv(repo), config) do
{:ok, location} ->
unless opts[:quiet] do
elapsed = System.convert_time_unit(System.system_time() - start_time, :native, :microsecond)
Mix.shell().info "The structure for #{inspect repo} has been loaded from #{location} in #{format_time(elapsed)}"
elapsed =
System.convert_time_unit(System.system_time() - start_time, :native, :microsecond)

Mix.shell().info(
"The structure for #{inspect(repo)} has been loaded from #{location} in #{format_time(elapsed)}"
)
end

{:error, term} when is_binary(term) ->
Mix.raise "The structure for #{inspect repo} couldn't be loaded: #{term}"
Mix.raise("The structure for #{inspect(repo)} couldn't be loaded: #{term}")

{:error, term} ->
Mix.raise "The structure for #{inspect repo} couldn't be loaded: #{inspect term}"
Mix.raise("The structure for #{inspect(repo)} couldn't be loaded: #{inspect(term)}")
end
end

defp format_time(microsec) when microsec < 1_000, do: "#{microsec} μs"
defp format_time(microsec) when microsec < 1_000_000, do: "#{div(microsec, 1_000)} ms"
defp format_time(microsec), do: "#{Float.round(microsec / 1_000_000.0)} s"
Expand Down
11 changes: 7 additions & 4 deletions lib/mix/tasks/ecto.migrate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule Mix.Tasks.Ecto.Migrate do
@impl true
def run(args, migrator \\ &Ecto.Migrator.run/4) do
repos = parse_repo(args)
{opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

opts =
if opts[:to] || opts[:to_exclusive] || opts[:step] || opts[:all],
Expand All @@ -119,7 +119,7 @@ defmodule Mix.Tasks.Ecto.Migrate do

opts =
if opts[:quiet],
do: Keyword.merge(opts, [log: false, log_migrations_sql: false, log_migrator_sql: false]),
do: Keyword.merge(opts, log: false, log_migrations_sql: false, log_migrator_sql: false),
else: opts

# Start ecto_sql explicitly before as we don't need
Expand All @@ -139,8 +139,11 @@ defmodule Mix.Tasks.Ecto.Migrate do
end

case Ecto.Migrator.with_repo(repo, fun, [mode: :temporary] ++ opts) do
{:ok, _migrated, _apps} -> :ok
{:error, error} -> Mix.raise "Could not start repo #{inspect repo}, error: #{inspect error}"
{:ok, _migrated, _apps} ->
:ok

{:error, error} ->
Mix.raise("Could not start repo #{inspect(repo)}, error: #{inspect(error)}")
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/mix/tasks/ecto.migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ defmodule Mix.Tasks.Ecto.Migrations do
@impl true
def run(args, migrations \\ &Ecto.Migrator.migrations/3, puts \\ &IO.puts/1) do
repos = parse_repo(args)
{opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

for repo <- repos do
ensure_repo(repo, args)
paths = ensure_migrations_paths(repo, opts)

case Ecto.Migrator.with_repo(repo, &migrations.(&1, paths, opts), [mode: :temporary]) do
case Ecto.Migrator.with_repo(repo, &migrations.(&1, paths, opts), mode: :temporary) do
{:ok, repo_status, _} ->
puts.(
"""
Expand All @@ -82,7 +82,7 @@ defmodule Mix.Tasks.Ecto.Migrations do
)

{:error, error} ->
Mix.raise "Could not start repo #{inspect repo}, error: #{inspect error}"
Mix.raise("Could not start repo #{inspect(repo)}, error: #{inspect(error)}")
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/mix/tasks/ecto.rollback.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ defmodule Mix.Tasks.Ecto.Rollback do

opts =
if opts[:quiet],
do: Keyword.merge(opts, [log: false, log_migrations_sql: false, log_migrator_sql: false]),
do: Keyword.merge(opts, log: false, log_migrations_sql: false, log_migrator_sql: false),
else: opts

# Start ecto_sql explicitly before as we don't need
Expand All @@ -135,8 +135,11 @@ defmodule Mix.Tasks.Ecto.Rollback do
end

case Ecto.Migrator.with_repo(repo, fun, [mode: :temporary] ++ opts) do
{:ok, _migrated, _apps} -> :ok
{:error, error} -> Mix.raise "Could not start repo #{inspect repo}, error: #{inspect error}"
{:ok, _migrated, _apps} ->
:ok

{:error, error} ->
Mix.raise("Could not start repo #{inspect(repo)}, error: #{inspect(error)}")
end
end

Expand Down

0 comments on commit 2bfbd0b

Please sign in to comment.