Skip to content

Commit

Permalink
Unique violation also returned on unique index. 0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jae Bach Hardie committed Mar 16, 2017
1 parent 81c9234 commit 281e743
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/mssqlex/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Mssqlex.Error do
defp get_constraint_violations(reason) do
constraint_checks =
[unique: ~r/Violation of UNIQUE KEY constraint '(\S+?)'./,
unique: ~r/Cannot insert duplicate key row .* with unique index '(\S+?)'/,
foreign_key: ~r/conflicted with the FOREIGN KEY constraint "(\S+?)"./,
check: ~r/conflicted with the CHECK constraint "(\S+?)"./]
extract = fn {key, test}, acc ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Mssqlex.Mixfile do

def project do
[app: :mssqlex,
version: "0.5.0",
version: "0.5.1",
description: "Adapter to Microsoft SQL Server. Using DBConnection and ODBC.",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
Expand Down
13 changes: 13 additions & 0 deletions test/mssqlex/constraints_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ defmodule Mssqlex.ConstraintsTest do
assert error.constraint_violations == [unique: "id_unique"]
end

test "Unique index", %{pid: pid} do
table_name = "constraints_test.dbo.uniq_ix"
Mssqlex.query!(pid, """
CREATE TABLE #{table_name} (id int);
CREATE UNIQUE INDEX id_unique ON #{table_name} (id);
""", [])
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?)", [42])
error = assert_raise Mssqlex.Error, fn ->
Mssqlex.query!(pid, "INSERT INTO #{table_name} VALUES (?)", [42])
end
assert error.constraint_violations == [unique: "id_unique"]
end

test "Foreign Key constraint", %{pid: pid} do
assoc_table_name = "constraints_test.dbo.assoc"
table_name = "constraints_test.dbo.fk"
Expand Down

0 comments on commit 281e743

Please sign in to comment.