From 2bfbd0b6cab6f5e0e442cd151914e174e3cfaa4a Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Sat, 22 Jul 2023 01:16:08 -0400 Subject: [PATCH] format mix/* files --- lib/mix/ecto_sql.ex | 10 ++++----- lib/mix/tasks/ecto.dump.ex | 35 +++++++++++++++++++---------- lib/mix/tasks/ecto.gen.migration.ex | 33 +++++++++++++++++---------- lib/mix/tasks/ecto.load.ex | 25 ++++++++++++++------- lib/mix/tasks/ecto.migrate.ex | 11 +++++---- lib/mix/tasks/ecto.migrations.ex | 6 ++--- lib/mix/tasks/ecto.rollback.ex | 9 +++++--- 7 files changed, 82 insertions(+), 47 deletions(-) diff --git a/lib/mix/ecto_sql.ex b/lib/mix/ecto_sql.ex index a2980bed..bf76566d 100644 --- a/lib/mix/ecto_sql.ex +++ b/lib/mix/ecto_sql.ex @@ -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 @@ -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 @@ -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 """ diff --git a/lib/mix/tasks/ecto.dump.ex b/lib/mix/tasks/ecto.dump.ex index aab99f7b..fef03104 100644 --- a/lib/mix/tasks/ecto.dump.ex +++ b/lib/mix/tasks/ecto.dump.ex @@ -60,7 +60,7 @@ 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 @@ -68,14 +68,19 @@ defmodule Mix.Tasks.Ecto.Dump do [] -> 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 @@ -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" diff --git a/lib/mix/tasks/ecto.gen.migration.ex b/lib/mix/tasks/ecto.gen.migration.ex index 70fd9c0b..51d2bfce 100644 --- a/lib/mix/tasks/ecto.gen.migration.ex +++ b/lib/mix/tasks/ecto.gen.migration.ex @@ -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 @@ -105,11 +114,11 @@ 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() %> @@ -117,5 +126,5 @@ defmodule Mix.Tasks.Ecto.Gen.Migration do <%= @change %> end end - """ + """) end diff --git a/lib/mix/tasks/ecto.load.ex b/lib/mix/tasks/ecto.load.ex index 1c50e5bc..94370d2d 100644 --- a/lib/mix/tasks/ecto.load.ex +++ b/lib/mix/tasks/ecto.load.ex @@ -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 @@ -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" diff --git a/lib/mix/tasks/ecto.migrate.ex b/lib/mix/tasks/ecto.migrate.ex index 27352e19..17b564fe 100644 --- a/lib/mix/tasks/ecto.migrate.ex +++ b/lib/mix/tasks/ecto.migrate.ex @@ -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], @@ -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 @@ -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 diff --git a/lib/mix/tasks/ecto.migrations.ex b/lib/mix/tasks/ecto.migrations.ex index 5065ae7f..5ebc5a68 100644 --- a/lib/mix/tasks/ecto.migrations.ex +++ b/lib/mix/tasks/ecto.migrations.ex @@ -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.( """ @@ -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 diff --git a/lib/mix/tasks/ecto.rollback.ex b/lib/mix/tasks/ecto.rollback.ex index c804c0e7..f0acfb4a 100644 --- a/lib/mix/tasks/ecto.rollback.ex +++ b/lib/mix/tasks/ecto.rollback.ex @@ -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 @@ -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