diff --git a/installer/lib/phoenix_new.ex b/installer/lib/phoenix_new.ex index fa658dc8c0..8a1dc50fca 100644 --- a/installer/lib/phoenix_new.ex +++ b/installer/lib/phoenix_new.ex @@ -426,17 +426,16 @@ defmodule Mix.Tasks.Phoenix.New do {:sqlite_ecto, Sqlite.Ecto, dev: [database: "db/#{app}_dev.sqlite"], test: [database: "db/#{app}_test.sqlite", pool: Ecto.Adapters.SQL.Sandbox], - prod: [database: "db/#{app}_prod.sqlite"], - test_begin: "Ecto.Adapters.SQL.begin_test_transaction(#{module}.Repo)", - test_restart: "Ecto.Adapters.SQL.restart_test_transaction(#{module}.Repo, [])"} + prod: [database: "db/#{app}_prod.sqlite"]} end defp get_ecto_adapter("mongodb", app, module) do {:mongodb_ecto, Mongo.Ecto, dev: [database: "#{app}_dev"], test: [database: "#{app}_test", pool_size: 1], prod: [database: "#{app}_prod"], - test_begin: "", - test_restart: "Mongo.Ecto.truncate(#{module}.Repo, [])", + test_setup_all: "", + test_setup: "", + test_async: "Mongo.Ecto.truncate(#{module}.Repo, [])", binary_id: true, migration: false, sample_binary_id: "111111111111111111111111"} @@ -450,8 +449,9 @@ defmodule Mix.Tasks.Phoenix.New do test: [username: user, password: pass, database: "#{app}_test", hostname: "localhost", pool: Ecto.Adapters.SQL.Sandbox], prod: [username: user, password: pass, database: "#{app}_prod"], - test_begin: "Ecto.Adapters.SQL.begin_test_transaction(#{module}.Repo)", - test_restart: "Ecto.Adapters.SQL.restart_test_transaction(#{module}.Repo, [])"] + test_setup_all: "Ecto.Adapters.SQL.Sandbox.mode(#{module}.Repo, :manual)", + test_setup: ":ok = Ecto.Adapters.SQL.Sandbox.checkout(#{module}.Repo)", + test_async: "Ecto.Adapters.SQL.Sandbox.mode(#{module}.Repo, {:shared, self()})"] end defp kw_to_config(kw) do diff --git a/installer/templates/ecto/model_case.ex b/installer/templates/ecto/model_case.ex index c80801f65f..09bfc79a17 100644 --- a/installer/templates/ecto/model_case.ex +++ b/installer/templates/ecto/model_case.ex @@ -26,8 +26,10 @@ defmodule <%= application_module %>.ModelCase do end setup tags do + <%= adapter_config[:test_setup] %> + unless tags[:async] do - <%= adapter_config[:test_restart] %> + <%= adapter_config[:test_async] %> end :ok diff --git a/installer/templates/new/config/config.exs b/installer/templates/new/config/config.exs index 9bed619131..7109202e29 100644 --- a/installer/templates/new/config/config.exs +++ b/installer/templates/new/config/config.exs @@ -4,11 +4,12 @@ # This configuration file is loaded before any dependency and # is restricted to this project. use Mix.Config -<%= if namespaced? do %> -# Configures the namespace used by Phoenix generators + +# General application configuration config :<%= application_name %>, - app_namespace: <%= application_module %> -<% end %> +<%= if namespaced? do %> app_namespace: <%= application_module %>,<% end %> + ecto_repos: [<%= application_module %>.Repo] + # Configures the endpoint config :<%= application_name %>, <%= application_module %>.Endpoint, url: [host: "localhost"], diff --git a/installer/templates/new/mix.exs b/installer/templates/new/mix.exs index 54d707c5cc..5a5bf7a6ea 100644 --- a/installer/templates/new/mix.exs +++ b/installer/templates/new/mix.exs @@ -35,13 +35,13 @@ defmodule <%= application_module %>.Mixfile do # Type `mix help deps` for examples and options. defp deps do [<%= phoenix_dep %>,<%= if ecto do %> - {:phoenix_ecto, "~> 2.0"}, + {:phoenix_ecto, "~> 3.0-rc"}, {<%= inspect adapter_app %>, ">= 0.0.0"},<% end %><%= if html do %> - {:phoenix_html, "~> 2.3"}, + {:phoenix_html, "~> 2.5"}, {:phoenix_live_reload, "~> 1.0", only: :dev},<% end %> # TODO move to hex release {:phoenix_pubsub, github: "phoenixframework/phoenix_pubsub"}, - {:gettext, "~> 0.9"}, + {:gettext, "~> 0.11"}, {:cowboy, "~> 1.0"}] end<%= if ecto do %> @@ -53,6 +53,7 @@ defmodule <%= application_module %>.Mixfile do # See the documentation for `Mix` for more info on aliases. defp aliases do ["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], - "ecto.reset": ["ecto.drop", "ecto.setup"]] + "ecto.reset": ["ecto.drop", "ecto.setup"], + "test": ["ecto.create", "ecto.migrate", "test"]] end<% end %> end diff --git a/installer/templates/new/test/support/channel_case.ex b/installer/templates/new/test/support/channel_case.ex index 25c12062a3..f993533d17 100644 --- a/installer/templates/new/test/support/channel_case.ex +++ b/installer/templates/new/test/support/channel_case.ex @@ -32,8 +32,10 @@ defmodule <%= application_module %>.ChannelCase do end setup tags do -<%= if ecto do %> unless tags[:async] do - <%= adapter_config[:test_restart] %> +<%= if ecto do %> <%= adapter_config[:test_setup] %> + + unless tags[:async] do + <%= adapter_config[:test_async] %> end <% end %> :ok diff --git a/installer/templates/new/test/support/conn_case.ex b/installer/templates/new/test/support/conn_case.ex index d11e601d00..a86854fe91 100644 --- a/installer/templates/new/test/support/conn_case.ex +++ b/installer/templates/new/test/support/conn_case.ex @@ -33,8 +33,10 @@ defmodule <%= application_module %>.ConnCase do end setup tags do -<%= if ecto do %> unless tags[:async] do - <%= adapter_config[:test_restart] %> +<%= if ecto do %> <%= adapter_config[:test_setup] %> + + unless tags[:async] do + <%= adapter_config[:test_async] %> end <% end %> {:ok, conn: Phoenix.ConnTest.build_conn()} diff --git a/installer/templates/new/test/test_helper.exs b/installer/templates/new/test/test_helper.exs index 7ded20b1fd..647f3fa56b 100644 --- a/installer/templates/new/test/test_helper.exs +++ b/installer/templates/new/test/test_helper.exs @@ -1,6 +1,4 @@ ExUnit.start <%= if ecto do %> -Mix.Task.run "ecto.create", ~w(-r <%= application_module %>.Repo --quiet) -Mix.Task.run "ecto.migrate", ~w(-r <%= application_module %>.Repo --quiet) -<%= adapter_config[:test_begin] %> +<%= adapter_config[:test_setup_all] %> <% end %> diff --git a/priv/templates/phoenix.gen.html/controller.ex b/priv/templates/phoenix.gen.html/controller.ex index d8cfe6ee62..61b7d4d1e4 100644 --- a/priv/templates/phoenix.gen.html/controller.ex +++ b/priv/templates/phoenix.gen.html/controller.ex @@ -3,8 +3,6 @@ defmodule <%= module %>Controller do alias <%= module %> - plug :scrub_params, <%= inspect singular %> when action in [:create, :update] - def index(conn, _params) do <%= plural %> = Repo.all(<%= alias %>) render(conn, "index.html", <%= plural %>: <%= plural %>) diff --git a/priv/templates/phoenix.gen.json/controller.ex b/priv/templates/phoenix.gen.json/controller.ex index 12afd4b0d6..69b16178ba 100644 --- a/priv/templates/phoenix.gen.json/controller.ex +++ b/priv/templates/phoenix.gen.json/controller.ex @@ -3,8 +3,6 @@ defmodule <%= module %>Controller do alias <%= module %> - plug :scrub_params, <%= inspect singular %> when action in [:create, :update] - def index(conn, _params) do <%= plural %> = Repo.all(<%= alias %>) render(conn, "index.json", <%= plural %>: <%= plural %>) diff --git a/priv/templates/phoenix.gen.model/model.ex b/priv/templates/phoenix.gen.model/model.ex index 05cbb2c631..b47ba97dae 100644 --- a/priv/templates/phoenix.gen.model/model.ex +++ b/priv/templates/phoenix.gen.model/model.ex @@ -8,18 +8,19 @@ defmodule <%= module %> do timestamps end - @required_fields ~w(<%= Enum.map_join(attrs, " ", &elem(&1, 0)) %>) - @optional_fields ~w() + @required_fields [<%= Enum.map_join(attrs, ", ", &inspect(elem(&1, 0))) %>] + @optional_fields [] @doc """ - Creates a changeset based on the `model` and `params`. + Creates a changeset based on the `struct` and `params`. If no params are provided, an invalid changeset is returned with no validation performed. """ - def changeset(model, params \\ :empty) do - model - |> cast(params, @required_fields, @optional_fields) + def changeset(struct, params \\ %{}) do + struct + |> cast(params, @required_fields ++ @optional_fields) + |> validate_required(@required_fields) <%= for k <- uniques do %> |> unique_constraint(<%= inspect k %>) <% end %> end end