Skip to content

Commit

Permalink
Call them exclusion constraints, not exclude constraints (#1384)
Browse files Browse the repository at this point in the history
This matches the PostgreSQL docs
  • Loading branch information
nathanl authored and josevalim committed Apr 20, 2016
1 parent 4f8dc03 commit d7ecccd
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 146,7 @@ Finally, Ecto now allows putting existing records in changesets, and the proper
* [Migration] Support `:on_update` for `Ecto.Migrate.references`
* [Migration] Use pool of 1 connection for `mix ecto.migrate/rollback`
* [Mix] Automatically reenable migration and repository management tasks after execution
* [Postgres] Add migration and changeset support for PostgreSQL exclusion constraints. Example: `create constraint(:sizes, :cannot_overlap, exclude: ~s|gist (int4range("min", "max", '[]') WITH &&)|)` and `exclude_constraint(changeset, :sizes, name: :cannot_overlap, message: "must not overlap")`
* [Postgres] Add migration and changeset support for PostgreSQL exclusion constraints. Example: `create constraint(:sizes, :cannot_overlap, exclude: ~s|gist (int4range("min", "max", '[]') WITH &&)|)` and `exclusion_constraint(changeset, :sizes, name: :cannot_overlap, message: "must not overlap")`
* [Postgres] Add migration and changeset support for PostgreSQL check constraints. Example: `create constraint(:products, "positive_price", check: "price > 0")` and `check_constraint(changeset, :price, name: :positive_price, message: "must be greater than zero")`
* [Query] Allow the `:on` field to be specified with association joins
* [Query] Support expressions in map keys in `select` in queries. Example: `from p in Post, select: %{p.title => p.visitors}`
Expand Down
6 changes: 3 additions & 3 deletions integration_test/pg/constraints_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 36,7 @@ defmodule Ecto.Integration.ConstraintsTest do
up(PoolRepo, 20040906120000, ConstraintMigration, log: false)
end

test "creating, using, and dropping an exclude constraint" do
test "creating, using, and dropping an exclusion constraint" do
changeset = Ecto.Changeset.change(%Constraint{}, from: 0, to: 10)
{:ok, _} = PoolRepo.insert(changeset)

Expand All @@ -56,14 56,14 @@ defmodule Ecto.Integration.ConstraintsTest do
exception =
assert_raise Ecto.ConstraintError, message, fn ->
overlapping_changeset
|> Ecto.Changeset.exclude_constraint(:from)
|> Ecto.Changeset.exclusion_constraint(:from)
|> PoolRepo.insert()
end
assert exception.message =~ "exclude: cannot_overlap"

{:error, changeset} =
overlapping_changeset
|> Ecto.Changeset.exclude_constraint(:from, name: :cannot_overlap)
|> Ecto.Changeset.exclusion_constraint(:from, name: :cannot_overlap)
|> PoolRepo.insert()
assert changeset.errors == [from: {"violates an exclusion constraint", []}]
assert changeset.data.__meta__.state == :built
Expand Down
2 changes: 1 addition & 1 deletion lib/ecto/adapters/mysql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 520,7 @@ if Code.ensure_loaded?(Mariaex) do
def execute_ddl({:create, %Constraint{check: check}}) when is_binary(check),
do: error!(nil, "MySQL adapter does not support check constraints")
def execute_ddl({:create, %Constraint{exclude: exclude}}) when is_binary(exclude),
do: error!(nil, "MySQL adapter does not support exclude constraints")
do: error!(nil, "MySQL adapter does not support exclusion constraints")

def execute_ddl({:drop, %Index{}=index}) do
assemble(["DROP INDEX",
Expand Down
8 changes: 4 additions & 4 deletions lib/ecto/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1706,10 1706,10 @@ defmodule Ecto.Changeset do
end

@doc """
Checks for a exclude constraint in the given field.
Checks for a exclusion constraint in the given field.
The exclude constraint works by relying on the database to check
if the exclude constraint has been violated or not and, if so,
The exclusion constraint works by relying on the database to check
if the exclusion constraint has been violated or not and, if so,
Ecto converts it into a changeset error.
## Options
Expand All @@ -1721,7 1721,7 @@ defmodule Ecto.Changeset do
explicitly for complex cases
"""
def exclude_constraint(changeset, field, opts \\ []) do
def exclusion_constraint(changeset, field, opts \\ []) do
constraint = opts[:name] || "#{get_source(changeset)}_#{field}_exclusion"
message = message(opts, "violates an exclusion constraint")
add_constraint(changeset, :exclude, to_string(constraint), field, {message, []})
Expand Down
2 changes: 1 addition & 1 deletion lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 703,7 @@ defmodule Ecto.Migration do
end

@doc ~S"""
Defines a constraint (either a check constraint or an exclude constraint) to be evaluated by the database when a row is inserted or updated.
Defines a constraint (either a check constraint or an exclusion constraint) to be evaluated by the database when a row is inserted or updated.
## Examples
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/adapters/mysql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 652,7 @@ defmodule Ecto.Adapters.MySQLTest do
assert SQL.execute_ddl(create)
end

assert_raise ArgumentError, "MySQL adapter does not support exclude constraints", fn ->
assert_raise ArgumentError, "MySQL adapter does not support exclusion constraints", fn ->
create = {:create, constraint(:products, "bar", exclude: "price")}
assert SQL.execute_ddl(create)
end
Expand Down
6 changes: 3 additions & 3 deletions test/ecto/changeset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 969,13 @@ defmodule Ecto.ChangesetTest do
end
end

test "exclude_constraint/3" do
changeset = change(%Post{}) |> exclude_constraint(:title)
test "exclusion_constraint/3" do
changeset = change(%Post{}) |> exclusion_constraint(:title)
assert changeset.constraints ==
[%{type: :exclude, field: :title, constraint: "posts_title_exclusion",
error: {"violates an exclusion constraint", []}}]

changeset = change(%Post{}) |> exclude_constraint(:title, name: :whatever, message: "is invalid")
changeset = change(%Post{}) |> exclusion_constraint(:title, name: :whatever, message: "is invalid")
assert changeset.constraints ==
[%{type: :exclude, field: :title, constraint: "whatever",
error: {"is invalid", []}}]
Expand Down
2 changes: 1 addition & 1 deletion test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 167,7 @@ defmodule Ecto.MigrationTest do
assert {:create, %Constraint{}} = last_command()
end

test "forward: creates an exclude constraint" do
test "forward: creates an exclusion constraint" do
create constraint(:posts, :price, exclude: "price")
flush
assert {:create, %Constraint{}} = last_command()
Expand Down

0 comments on commit d7ecccd

Please sign in to comment.