From e5ea9f6171db5a79ac486c97f74126c5f42242e2 Mon Sep 17 00:00:00 2001 From: Peter Mueller <6015288+petermueller@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:20:32 -0400 Subject: [PATCH] add `Ecto.Adapters.SQL.Constraint` --- lib/ecto/adapters/sql.ex | 1 + lib/ecto/adapters/sql/constraint.ex | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/ecto/adapters/sql/constraint.ex diff --git a/lib/ecto/adapters/sql.ex b/lib/ecto/adapters/sql.ex index f30a1952..ffc11584 100644 --- a/lib/ecto/adapters/sql.ex +++ b/lib/ecto/adapters/sql.ex @@ -644,6 +644,7 @@ defmodule Ecto.Adapters.SQL do sql_call(adapter_meta, :query_many, [sql], params, opts) end + @doc false def to_constraints(adapter_meta, opts, err, err_opts) do %{constraint_handler: constraint_handler} = adapter_meta constraint_handler = Keyword.get(opts, :constraint_handler) || constraint_handler diff --git a/lib/ecto/adapters/sql/constraint.ex b/lib/ecto/adapters/sql/constraint.ex new file mode 100644 index 00000000..6e52524b --- /dev/null +++ b/lib/ecto/adapters/sql/constraint.ex @@ -0,0 +1,19 @@ +defmodule Ecto.Adapters.SQL.Constraint do + @moduledoc """ + Specifies the constraint handling API + """ + + @doc """ + Receives the exception returned by `c:Ecto.Adapters.SQL.Connection.query/4`. + + The constraints are in the keyword list and must return the + constraint type, like `:unique`, and the constraint name as + a string, for example: + + [unique: "posts_title_index"] + + Must return an empty list if the error does not come + from any constraint. + """ + @callback to_constraints(exception :: Exception.t(), options :: Keyword.t()) :: Keyword.t() +end