New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move generators to Ecto 2.0 #1649

Merged
merged 6 commits into from Apr 18, 2016
@@ -424,19 +424,21 @@ defmodule Mix.Tasks.Phoenix.New do
end
defp get_ecto_adapter("sqlite", app, module) 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, [])"}
dev: [database: "db/#{app}_dev.sqlite"],
test: [database: "db/#{app}_test.sqlite", pool: Ecto.Adapters.SQL.Sandbox],
prod: [database: "db/#{app}_prod.sqlite"],
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 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 +452,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
@@ -20,14 +20,16 @@ defmodule <%= application_module %>.ModelCase do
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
import Ecto.Query
import <%= application_module %>.ModelCase
end
end
setup tags do
<%= adapter_config[:test_setup] %>
unless tags[:async] do
<%= adapter_config[:test_restart] %>
<%= adapter_config[:test_async] %>
end
:ok
@@ -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
config :<%= application_name %>,
app_namespace: <%= application_module %>
<% end %>
# General application configuration
config :<%= application_name %>,<%= 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"],
@@ -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
@@ -77,27 +77,17 @@ msgstr[0] ""
msgstr[1] ""
## From Ecto.Changeset.validate_number/3
msgid "must be less than %{count}"
msgid_plural "must be less than %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be less than %{number}"
msgstr ""
msgid "must be greater than %{count}"
msgid_plural "must be greater than %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be greater than %{number}"
msgstr ""
msgid "must be less than or equal to %{count}"
msgid_plural "must be less than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be less than or equal to %{number}"
msgstr ""
msgid "must be greater than or equal to %{count}"
msgid_plural "must be greater than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be greater than or equal to %{number}"
msgstr ""
msgid "must be equal to %{count}"
msgid_plural "must be equal to %{count}"
msgstr[0] ""
msgstr[1] ""<% end %>
msgid "must be equal to %{number}"
msgstr ""<% end %>
@@ -75,27 +75,17 @@ msgstr[0] ""
msgstr[1] ""
## From Ecto.Changeset.validate_number/3
msgid "must be less than %{count}"
msgid_plural "must be less than %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be less than %{number}"
msgstr ""
msgid "must be greater than %{count}"
msgid_plural "must be greater than %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be greater than %{number}"
msgstr ""
msgid "must be less than or equal to %{count}"
msgid_plural "must be less than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be less than or equal to %{number}"
msgstr ""
msgid "must be greater than or equal to %{count}"
msgid_plural "must be greater than or equal to %{count}"
msgstr[0] ""
msgstr[1] ""
msgid "must be greater than or equal to %{number}"
msgstr ""
msgid "must be equal to %{count}"
msgid_plural "must be equal to %{count}"
msgstr[0] ""
msgstr[1] ""<% end %>
msgid "must be equal to %{number}"
msgstr ""<% end %>
@@ -23,7 +23,7 @@ defmodule <%= application_module %>.ChannelCase do
alias <%= application_module %>.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
import Ecto.Query
<% end %>
# The default endpoint for testing
@@ -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
@@ -23,7 +23,7 @@ defmodule <%= application_module %>.ConnCase do
alias <%= application_module %>.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
import Ecto.Query
<% end %>
import <%= application_module %>.Router.Helpers
@@ -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()}
@@ -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 %>
@@ -21,15 +21,20 @@ defmodule <%= application_module %>.ErrorHelpers do
# Because error messages were defined within Ecto, we must
# call the Gettext module passing our Gettext backend. We
# also use the "errors" domain as translations are placed
# in the errors.po file. On your own code and templates,
# this could be written simply as:
# in the errors.po file.
# Ecto will pass the :count keyword if the error message is
# meant to be pluralized.
# On your own code and templates, depending on whether you
# need the message to be pluralized or not, this could be
# written simply as:
#
# dngettext "errors", "1 file", "%{count} files", count
# dgettext "errors", "is invalid"
#
Gettext.dngettext(<%= application_module %>.Gettext, "errors", msg, msg, opts[:count], opts)
end
def translate_error(msg) do
Gettext.dgettext(<%= application_module %>.Gettext, "errors", msg)
if count = opts[:count] do
Gettext.dngettext(<%= application_module %>.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(<%= application_module %>.Gettext, "errors", msg, opts)
end
end
end
@@ -22,7 +22,7 @@ defmodule <%= application_module %>.Web do
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]<%= if adapter_config[:binary_id] do %>
import Ecto.Query<%= if adapter_config[:binary_id] do %>
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id<% end %>
@@ -41,7 +41,7 @@ defmodule <%= application_module %>.Web do
<%= if ecto do %>
alias <%= application_module %>.Repo
import Ecto
import Ecto.Query, only: [from: 1, from: 2]
import Ecto.Query
<% end %>
import <%= application_module %>.Router.Helpers
import <%= application_module %>.Gettext
@@ -76,7 +76,7 @@ defmodule <%= application_module %>.Web do
alias <%= application_module %>.Repo
import Ecto
import Ecto.Query, only: [from: 1, from: 2]<% end %>
import Ecto.Query<% end %>
import <%= application_module %>.Gettext
end
end
@@ -254,12 +254,9 @@ defmodule Mix.Tasks.Phoenix.NewTest do
assert_file "custom_path/config/test.exs", [~r/Ecto.Adapters.MySQL/, ~r/username: "root"/, ~r/password: ""/]
assert_file "custom_path/config/prod.secret.exs", [~r/Ecto.Adapters.MySQL/, ~r/username: "root"/, ~r/password: ""/]
assert_file "custom_path/test/support/conn_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/channel_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/model_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/conn_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
assert_file "custom_path/test/support/channel_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
assert_file "custom_path/test/support/model_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
end
end
@@ -273,12 +270,9 @@ defmodule Mix.Tasks.Phoenix.NewTest do
assert_file "custom_path/config/test.exs", ~r/Tds.Ecto/
assert_file "custom_path/config/prod.secret.exs", ~r/Tds.Ecto/
assert_file "custom_path/test/support/conn_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/channel_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/model_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/conn_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
assert_file "custom_path/test/support/channel_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
assert_file "custom_path/test/support/model_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
end
end
@@ -304,12 +298,9 @@ defmodule Mix.Tasks.Phoenix.NewTest do
assert file =~ ~r/database: "db\/custom_path_prod.sqlite"/
end
assert_file "custom_path/test/support/conn_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/channel_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/model_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/conn_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
assert_file "custom_path/test/support/channel_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
assert_file "custom_path/test/support/model_case.ex", "Ecto.Adapters.SQL.Sandbox.mode"
end
end
@@ -333,9 +324,9 @@ defmodule Mix.Tasks.Phoenix.NewTest do
refute file =~ ~r/Ecto.Adapters.SQL/
end
assert_file "custom_path/test/support/conn_case.ex", ~r/Mongo.Ecto.truncate/
assert_file "custom_path/test/support/model_case.ex", ~r/Mongo.Ecto.truncate/
assert_file "custom_path/test/support/channel_case.ex", ~r/Mongo.Ecto.truncate/
assert_file "custom_path/test/support/conn_case.ex", "Mongo.Ecto.truncate"
assert_file "custom_path/test/support/model_case.ex", "Mongo.Ecto.truncate"
assert_file "custom_path/test/support/channel_case.ex", "Mongo.Ecto.truncate"
assert_file "custom_path/config/config.exs", fn file ->
assert file =~ ~r/binary_id: true/
@@ -355,12 +346,9 @@ defmodule Mix.Tasks.Phoenix.NewTest do
assert_file "custom_path/config/test.exs", [~r/Ecto.Adapters.Postgres/, ~r/username: "postgres"/, ~r/password: "postgres"/, ~r/hostname: "localhost"/]
assert_file "custom_path/config/prod.secret.exs", [~r/Ecto.Adapters.Postgres/, ~r/username: "postgres"/, ~r/password: "postgres"/]
assert_file "custom_path/test/support/conn_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/channel_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/model_case.ex",
~r/Ecto.Adapters.SQL.restart_test_transaction/
assert_file "custom_path/test/support/conn_case.ex", "Ecto.Adapters.SQL.Sandbox.checkout"
assert_file "custom_path/test/support/channel_case.ex", "Ecto.Adapters.SQL.Sandbox.checkout"
assert_file "custom_path/test/support/model_case.ex", "Ecto.Adapters.SQL.Sandbox.checkout"
end
end
@@ -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 %>)
@@ -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 %>)
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.