diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index addb6a9d2..e88942e34 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -24,7 +24,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "golang.org/x/crypto/bcrypt" // Import the postgres database driver. - "github.com/lib/pq" + _ "github.com/lib/pq" ) // Database represents an account database @@ -128,11 +128,8 @@ func (d *Database) CreateAccount( return nil, err } if err := d.profiles.insertProfile(ctx, localpart); err != nil { - if err, ok := err.(*pq.Error); ok { - if err.Code.Class() == "23" { - // 23 => unique_violation => Account already exists - return nil, nil - } + if common.IsUniqueConstraintViolationErr(err) { + return nil, nil } return nil, err } diff --git a/src/github.com/matrix-org/dendrite/common/sql.go b/src/github.com/matrix-org/dendrite/common/sql.go index f3de66f11..7ac9ac140 100644 --- a/src/github.com/matrix-org/dendrite/common/sql.go +++ b/src/github.com/matrix-org/dendrite/common/sql.go @@ -16,6 +16,8 @@ package common import ( "database/sql" + + "github.com/lib/pq" ) // A Transaction is something that can be committed or rolledback. @@ -66,3 +68,9 @@ func TxStmt(transaction *sql.Tx, statement *sql.Stmt) *sql.Stmt { } return statement } + +// IsUniqueConstraintViolationErr returns true if the error is a postgresql unique_violation error +func IsUniqueConstraintViolationErr(err error) bool { + pqErr, ok := err.(*pq.Error) + return ok && pqErr.Code == "23505" +}