Skip to content

Commit 3cb1892

Browse files
tiagopogmichalmuskala
authored andcommitted
Fix pattern matching error when creating uniqueness index (#112)
* Fix pattern matching error when creating uniqueness index * Ensure returning the expected tuple for uniqueness validations * Remove unnecessary string interpolation
1 parent 95f7099 commit 3cb1892

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/mongo_ecto.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,10 @@ defmodule Mongo.Ecto do
650650

651651
query = %WriteQuery{coll: "system.indexes", command: index}
652652

653-
{:ok, _} = Connection.insert(repo, query, opts)
654-
:ok
653+
case Connection.insert(repo, query, opts) do
654+
{:ok, _} -> :ok
655+
{:invalid, [unique: index]} -> raise Connection.format_constraint_error(index)
656+
end
655657
end
656658

657659
def execute_ddl(repo, {:drop, %Index{name: name, table: coll}}, opts) do

lib/mongo_ecto/connection.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ defmodule Mongo.Ecto.Connection do
189189
end
190190
end
191191

192+
def format_constraint_error(index) do
193+
%Mongo.Error{message: "ERROR (11000): could not create unique index \"#{index}\" due to duplicated entry"}
194+
end
195+
192196
defp format_query(%Query{action: :command}, [command]) do
193197
["COMMAND " | inspect(command)]
194198
end

0 commit comments

Comments
 (0)