これは、 Changeset の構造体に constraint の要素を設定し、DBへの処理を行うタイミングでその束縛に反していないかを確認する、というもののようです。
変更のあったコミット
これをざーっと眺めると、 lib/ecto/changeset.ex の moduledoc が更新されていることがわかります。
constraints と validation の関係が記載されているので、気になるかたは眺めてみてください。
However, constraints can only be checked in a safe way when performing the operation in the database. As consequence, validations are always checked before constraints. Constraints won’t even be checked in case validations failed.
とのことです。
changeset.ex の defstruct に以下が追加されています。
constraints: []
私の手持ちのWebアプリを更新してみました。
更新のコミットは以下。
https://github.com/KazuCocoa/web_qa_vote/commit/ff53f15f68267afefe3569ecab502c3e41d692a4
ポイントは、
- uniqueを確認するためのメソッドが変更される
validate_unique(changeset, :email, [on: Repo, message: "Already anyone use same email."])=>unique_constraint(changeset, :email, [message: "Already anyone use same email."])
Repo.insertやRepo.updateの時点でパターンマッチによりerrorの処理を入れるようにするchangeset.valid?がfailにならない{:error, changeset}のときに、changesetのerrorに、constraintsに指定したmessageが渡されている
DBに書き込みを行うタイミングでuniqueかを判定するタイミングを統一しよう、という話に見えますね。