From b674602f5c48a3c91d35102bf6aae6edcc3a3c95 Mon Sep 17 00:00:00 2001 From: Tomasz Marek Sulima Date: Sat, 26 Oct 2019 03:40:01 +0200 Subject: [PATCH 1/2] Proposed fix for #3148 If config/config.exs does not exists and Elixir version >= 1.9.0 then use "import Config". Otherwise ensure user config is keep and `String.replace/3` works as expected for both cases. --- lib/mix/tasks/ecto.gen.repo.ex | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/mix/tasks/ecto.gen.repo.ex b/lib/mix/tasks/ecto.gen.repo.ex index 9a8127e713..b8c2220f91 100644 --- a/lib/mix/tasks/ecto.gen.repo.ex +++ b/lib/mix/tasks/ecto.gen.repo.ex @@ -50,11 +50,14 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do case File.read(config_path) do {:ok, contents} -> + check = String.contains?(contents, "import Config") + config_first_line = get_first_config_line(check) + new_contents = config_first_line <> "\n\n" <> config_template(opts) Mix.shell.info [:green, "* updating ", :reset, "config/config.exs"] - File.write! "config/config.exs", - String.replace(contents, "use Mix.Config\n", config_template(opts)) + File.write! "config/config.exs", String.replace(contents, config_first_line, new_contents) {:error, _} -> - create_file "config/config.exs", config_template(opts) + config_first_line = "1.9.0" |> Version.match?(config[:elixir]) |> get_first_config_line() + create_file "config/config.exs", config_first_line <> "\n\n" <> config_template(opts) end open?("config/config.exs") @@ -74,6 +77,9 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do """ end + defp get_first_config_line(true), do: "import Config" + defp get_first_config_line(false), do: "use Mix.Config" + embed_template :repo, """ defmodule <%= inspect @mod %> do use Ecto.Repo, @@ -83,8 +89,6 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do """ embed_template :config, """ - use Mix.Config - config <%= inspect @app %>, <%= inspect @mod %>, database: "<%= @app %>_<%= @base %>", username: "user", From 6ec54fb11be7481a1511e54496d6a44dc504a444 Mon Sep 17 00:00:00 2001 From: Tomasz Marek Sulima Date: Sat, 26 Oct 2019 04:03:16 +0200 Subject: [PATCH 2/2] Update changes to pass tests: 1. Moving one newline - String.replace/3 test fail 2. Changed test to ensure check for Config module --- lib/mix/tasks/ecto.gen.repo.ex | 6 +++--- test/mix/tasks/ecto.gen.repo_test.exs | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/ecto.gen.repo.ex b/lib/mix/tasks/ecto.gen.repo.ex index b8c2220f91..c6f927c47d 100644 --- a/lib/mix/tasks/ecto.gen.repo.ex +++ b/lib/mix/tasks/ecto.gen.repo.ex @@ -51,12 +51,12 @@ defmodule Mix.Tasks.Ecto.Gen.Repo do case File.read(config_path) do {:ok, contents} -> check = String.contains?(contents, "import Config") - config_first_line = get_first_config_line(check) - new_contents = config_first_line <> "\n\n" <> config_template(opts) + config_first_line = get_first_config_line(check) <> "\n" + new_contents = config_first_line <> "\n" <> config_template(opts) Mix.shell.info [:green, "* updating ", :reset, "config/config.exs"] File.write! "config/config.exs", String.replace(contents, config_first_line, new_contents) {:error, _} -> - config_first_line = "1.9.0" |> Version.match?(config[:elixir]) |> get_first_config_line() + config_first_line = Config |> Code.ensure_loaded?() |> get_first_config_line() create_file "config/config.exs", config_first_line <> "\n\n" <> config_template(opts) end diff --git a/test/mix/tasks/ecto.gen.repo_test.exs b/test/mix/tasks/ecto.gen.repo_test.exs index db010483ec..e096044766 100644 --- a/test/mix/tasks/ecto.gen.repo_test.exs +++ b/test/mix/tasks/ecto.gen.repo_test.exs @@ -15,8 +15,10 @@ defmodule Mix.Tasks.Ecto.Gen.RepoTest do end """ + first_line = if Code.ensure_loaded?(Config), do: "import Config", else: "use Mix.Config" + assert_file "config/config.exs", """ - use Mix.Config + #{first_line} config :ecto, Repo, database: "ecto_repo",