Skip to content

Commit

Permalink
Merge pull request #17 from gjaldon/ecto-2.0
Browse files Browse the repository at this point in the history
[WIP] Ecto 2.0 support
  • Loading branch information
gjaldon committed Nov 6, 2016
2 parents 357584c + 10d3160 commit 01a1f0c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/ecto_enum/postgres.ex
Expand Up @@ -40,7 +40,7 @@ defmodule EctoEnum.Postgres do
def __valid_values__(), do: @valid_values

def create_type() do
types = Enum.join(unquote(list), ", ")
types = Enum.map_join(unquote(list), ", ", &"'#{&1}'")
sql = "CREATE TYPE #{unquote type} AS ENUM (#{types})"
Ecto.Migration.execute sql
end
Expand Down
10 changes: 5 additions & 5 deletions mix.exs
Expand Up @@ -6,7 +6,7 @@ defmodule EctoEnum.Mixfile do
def project do
[app: :ecto_enum,
version: @version,
elixir: "~> 1.0",
elixir: "~> 1.2",
deps: deps,
description: "Ecto extension to support enums in models",
test_paths: test_paths(Mix.env),
Expand All @@ -31,10 +31,10 @@ defmodule EctoEnum.Mixfile do
end

defp deps do
[{:ecto, ">= 0.13.1 and ~> 1.1.0"},
{:postgrex, ">= 0.8.3", optional: true},
{:mariaex, ">= 0.3.0", optional: true},
{:ex_doc, "~> 0.10", only: :docs},
[{:ecto, "~> 2.0"},
{:postgrex, "~> 0.12.0", optional: true},
{:mariaex, "~> 0.7.7", optional: true},
{:ex_doc, "~> 0.11", only: :docs},
{:earmark, "~> 0.1", only: :docs},
{:inch_ex, ">= 0.0.0", only: :docs}]
end
Expand Down
18 changes: 9 additions & 9 deletions mix.lock
@@ -1,11 +1,11 @@
%{"connection": {:hex, :connection, "1.0.2", "f4a06dd3ecae4141aa66f94ce92ea4c4b8753069472814932f1cadbc3078ab80", [:mix], []},
"db_connection": {:hex, :db_connection, "0.2.3", "9718deeb713ceb5213950a0de4f29b98133f4cb73cc9aca65648b5a8a1414c59", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
%{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
"db_connection": {:hex, :db_connection, "1.0.0", "63c03e520d54886a66104d34e32397ba960db6e74b596ce221592c07d6a40d8d", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
"decimal": {:hex, :decimal, "1.2.0", "462960fd71af282e570f7b477f6be56bf8968e68277d4d0b641a635269bf4b0d", [:mix], []},
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
"ecto": {:hex, :ecto, "1.1.8", "0f0348e678fa5a450c266d69816808f97fbd82ade32cf88d4b09bbe8f8c27545", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.5.0 or ~> 0.6.0", [hex: :mariaex, optional: true]}, {:poison, "~> 1.0 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.4", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.11.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}]},
"ex_doc": {:hex, :ex_doc, "0.11.5", "0dc51cb84f8312162a2313d6c71573a9afa332333d8a332bb12540861b9834db", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"inch_ex": {:hex, :inch_ex, "0.3.3", "f53770404bb38385acbadd518ed571ba54d9608148ce7e7180e5826460b32f52", [:mix], [{:poison, "~> 1.2", [hex: :poison, optional: false]}]},
"mariaex": {:hex, :mariaex, "0.6.5", "7f17fe274151554f33ff0c3778f447f05fe81c2fcff7a1e843233b1225546287", [:mix], [{:connection, "~> 1.0.0", [hex: :connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
"poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], []},
"ecto": {:hex, :ecto, "2.0.5", "7f4c79ac41ffba1a4c032b69d7045489f0069c256de606523c65d9f8188e502d", [:mix], [{:db_connection, "~> 1.0-rc.4", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.1.2 or ~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.12.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]},
"ex_doc": {:hex, :ex_doc, "0.12.0", "b774aabfede4af31c0301aece12371cbd25995a21bb3d71d66f5c2fe074c603f", [:mix], [{:earmark, "~> 0.2", [hex: :earmark, optional: false]}]},
"inch_ex": {:hex, :inch_ex, "0.5.5", "b63f57e281467bd3456461525fdbc9e158c8edbe603da6e3e4671befde796a3d", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]},
"mariaex": {:hex, :mariaex, "0.7.8", "77bcc9e898a7f2231022ac0112f2832158d289fa3ef1e0020ceb61d00403134c", [:mix], [{:db_connection, "~> 1.0.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.11.1", "f48af70c0a58b9bfd1aaa456ec4273624554cfb837726b6a7f0701da4a94b2dd", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 0.2", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}}
"postgrex": {:hex, :postgrex, "0.12.1", "2f8b46cb3a44dcd42f42938abedbfffe7e103ba4ce810ccbeee8dcf27ca0fb06", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc.4", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}}
6 changes: 5 additions & 1 deletion test/mysql/ecto_enum_test.exs
Expand Up @@ -15,6 +15,10 @@ defmodule EctoEnumTest do

alias Ecto.Integration.TestRepo

setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(TestRepo)
end

test "accepts int, atom and string on save" do
user = TestRepo.insert!(%User{status: 0})
user = TestRepo.get(User, user.id)
Expand Down Expand Up @@ -48,7 +52,7 @@ defmodule EctoEnumTest do
end

test "raises when input is not in the enum map" do
error = {:status, "is invalid"}
error = {:status, {"is invalid", [type: EctoEnumTest.StatusEnum]}}

changeset = cast(%User{}, %{"status" => "retroactive"}, ~w(status), [])
assert error in changeset.errors
Expand Down
45 changes: 37 additions & 8 deletions test/mysql/test_helper.exs
@@ -1,25 +1,54 @@
ExUnit.start()

alias Ecto.Integration.TestRepo
Code.require_file "ecto_migration.exs", __DIR__

Application.put_env(:ecto, TestRepo,
Application.put_env(:ecto, Ecto.Integration.TestRepo,
adapter: Ecto.Adapters.MySQL,
url: "ecto://root@localhost/ecto_test",
pool: Ecto.Adapters.SQL.Sandbox)

defmodule Ecto.Integration.TestRepo do
use Ecto.Repo, otp_app: :ecto
end

Application.put_env(:ecto, Ecto.Integration.PoolRepo,
adapter: Ecto.Adapters.MySQL,
url: "ecto://root@localhost/ecto_test",
pool_size: 10)

defmodule Ecto.Integration.PoolRepo do
use Ecto.Repo, otp_app: :ecto

def log(_cmd), do: nil
def create_prefix(prefix) do
"create database #{prefix}"
end

def drop_prefix(prefix) do
"drop database #{prefix}"
end
end

# Load up the repository, start it, and run migrations
_ = Ecto.Storage.down(TestRepo)
:ok = Ecto.Storage.up(TestRepo)

{:ok, _pid} = TestRepo.start_link
defmodule Ecto.Integration.Case do
use ExUnit.CaseTemplate

Code.require_file "ecto_migration.exs", __DIR__
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(TestRepo)
end
end

alias Ecto.Integration.TestRepo
alias Ecto.Integration.PoolRepo

{:ok, _} = Ecto.Adapters.MySQL.ensure_all_started(TestRepo, :temporary)

# Load up the repository, start it, and run migrations
_ = Ecto.Adapters.MySQL.storage_down(TestRepo.config)
:ok = Ecto.Adapters.MySQL.storage_up(TestRepo.config)

{:ok, _pid} = TestRepo.start_link
{:ok, _pid} = PoolRepo.start_link
:ok = Ecto.Migrator.up(TestRepo, 0, Ecto.Integration.Migration, log: false)
Ecto.Adapters.SQL.Sandbox.mode(TestRepo, :manual)
Process.flag(:trap_exit, true)

2 changes: 1 addition & 1 deletion test/pg/ecto_enum_test.exs
Expand Up @@ -48,7 +48,7 @@ defmodule EctoEnumTest do
end

test "raises when input is not in the enum map" do
error = {:status, "is invalid"}
error = {:status, {"is invalid", [type: EctoEnumTest.StatusEnum]}}

changeset = cast(%User{}, %{"status" => "retroactive"}, ~w(status), [])
assert error in changeset.errors
Expand Down
7 changes: 4 additions & 3 deletions test/pg/postgres_test.exs
Expand Up @@ -15,7 +15,7 @@ defmodule EctoEnum.PostgresTest do

alias Ecto.Integration.TestRepo

test "accepts int, atom and string on save" do
test "accepts atom and string on save" do
user = TestRepo.insert!(%User{status: :registered})
user = TestRepo.get(User, user.id)
assert user.status == :registered
Expand All @@ -38,7 +38,8 @@ defmodule EctoEnum.PostgresTest do

test "casts binary to atom" do
%{errors: errors} = cast(%User{}, %{"status" => 3}, ~w(status), [])
assert {:status, "is invalid"} in errors
error = {:status, {"is invalid", [type: EctoEnum.PostgresTest.StatusEnum]}}
assert error in errors

%{changes: changes} = cast(%User{}, %{"status" => "active"}, ~w(status), [])
assert changes.status == :active
Expand All @@ -48,7 +49,7 @@ defmodule EctoEnum.PostgresTest do
end

test "raises when input is not in the enum map" do
error = {:status, "is invalid"}
error = {:status, {"is invalid", [type: EctoEnum.PostgresTest.StatusEnum]}}

changeset = cast(%User{}, %{"status" => "retroactive"}, ~w(status), [])
assert error in changeset.errors
Expand Down
6 changes: 4 additions & 2 deletions test/pg/test_helper.exs
@@ -1,3 +1,4 @@
Logger.configure(level: :info)
ExUnit.start()

alias Ecto.Integration.TestRepo
Expand All @@ -14,14 +15,15 @@ defmodule Ecto.Integration.TestRepo do
end

# Load up the repository, start it, and run migrations
_ = Ecto.Storage.down(TestRepo)
:ok = Ecto.Storage.up(TestRepo)
_ = Ecto.Adapters.Postgres.storage_down(TestRepo.config)
:ok = Ecto.Adapters.Postgres.storage_up(TestRepo.config)

{:ok, pid} = TestRepo.start_link()

Code.require_file "ecto_migration.exs", __DIR__

:ok = Ecto.Migrator.up(TestRepo, 0, Ecto.Integration.Migration, log: false)
Ecto.Adapters.SQL.Sandbox.mode(TestRepo, :manual)
Process.flag(:trap_exit, true)

:ok = TestRepo.stop(pid)
Expand Down

0 comments on commit 01a1f0c

Please sign in to comment.