-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54313: sql: set constraint name in pg error for FK violations r=RaduBerinde a=RaduBerinde This change adds infrastructure to set the "constraint name" field of errors on pgwire and uses it to populate the constraint name for foreign key violation errors. Fixes #36494. Release note (sql change): foreign key violation errors now fill in the "constraint name" error message field. 54542: sql: enable partial inverted indexes in randomized tests r=rytaft a=mgartner Now that partial inverted indexes can be created, they can be generated in randomized tests. Release note: None Co-authored-by: Radu Berinde <[email protected]> Co-authored-by: Marcus Gartner <[email protected]>
- Loading branch information
Showing
15 changed files
with
346 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package pgerror | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/cockroachdb/errors/errorspb" | ||
"github.com/gogo/protobuf/proto" | ||
) | ||
|
||
// WithConstraintName decorates the error with a severity. | ||
func WithConstraintName(err error, constraint string) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return &withConstraintName{cause: err, constraint: constraint} | ||
} | ||
|
||
// GetConstraintName attempts to unwrap and find a Severity. | ||
func GetConstraintName(err error) string { | ||
if c := (*withConstraintName)(nil); errors.As(err, &c) { | ||
return c.constraint | ||
} | ||
return "" | ||
} | ||
|
||
type withConstraintName struct { | ||
cause error | ||
constraint string | ||
} | ||
|
||
var _ error = (*withConstraintName)(nil) | ||
var _ errors.SafeDetailer = (*withConstraintName)(nil) | ||
var _ fmt.Formatter = (*withConstraintName)(nil) | ||
var _ errors.SafeFormatter = (*withConstraintName)(nil) | ||
|
||
func (w *withConstraintName) Error() string { return w.cause.Error() } | ||
func (w *withConstraintName) Cause() error { return w.cause } | ||
func (w *withConstraintName) Unwrap() error { return w.cause } | ||
func (w *withConstraintName) SafeDetails() []string { | ||
// The constraint name is considered PII. | ||
return nil | ||
} | ||
|
||
func (w *withConstraintName) Format(s fmt.State, verb rune) { errors.FormatError(w, s, verb) } | ||
|
||
func (w *withConstraintName) SafeFormatError(p errors.Printer) (next error) { | ||
if p.Detail() { | ||
p.Printf("constraint name: %s", w.constraint) | ||
} | ||
return w.cause | ||
} | ||
|
||
func encodeWithConstraintName(_ context.Context, err error) (string, []string, proto.Message) { | ||
w := err.(*withConstraintName) | ||
return "", nil, &errorspb.StringPayload{Msg: w.constraint} | ||
} | ||
|
||
// decodeWithConstraintName is a custom decoder that will be used when decoding | ||
// withConstraintName error objects. | ||
// Note that as the last argument it takes proto.Message (and not | ||
// protoutil.Message which is required by linter) because the latter brings in | ||
// additional dependencies into this package and the former is sufficient here. | ||
func decodeWithConstraintName( | ||
_ context.Context, cause error, _ string, _ []string, payload proto.Message, | ||
) error { | ||
m, ok := payload.(*errorspb.StringPayload) | ||
if !ok { | ||
// If this ever happens, this means some version of the library | ||
// (presumably future) changed the payload type, and we're | ||
// receiving this here. In this case, give up and let | ||
// DecodeError use the opaque type. | ||
return nil | ||
} | ||
return &withConstraintName{cause: cause, constraint: m.Msg} | ||
} | ||
|
||
func init() { | ||
key := errors.GetTypeKey((*withConstraintName)(nil)) | ||
errors.RegisterWrapperEncoder(key, encodeWithConstraintName) | ||
errors.RegisterWrapperDecoder(key, decodeWithConstraintName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package pgerror | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConstraintName(t *testing.T) { | ||
testCases := []struct { | ||
err error | ||
expectedConstraint string | ||
}{ | ||
{WithConstraintName(fmt.Errorf("test"), "fk1"), "fk1"}, | ||
{WithConstraintName(WithConstraintName(fmt.Errorf("test"), "fk1"), "fk2"), "fk2"}, | ||
{WithConstraintName(WithCandidateCode(fmt.Errorf("test"), pgcode.FeatureNotSupported), "fk1"), "fk1"}, | ||
{New(pgcode.Uncategorized, "i am an error"), ""}, | ||
{WithCandidateCode(WithConstraintName(errors.Newf("test"), "fk1"), pgcode.System), "fk1"}, | ||
{fmt.Errorf("something else"), ""}, | ||
{WithConstraintName(fmt.Errorf("test"), "fk\"⌂"), "fk\"⌂"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.err.Error(), func(t *testing.T) { | ||
constraint := GetConstraintName(tc.err) | ||
require.Equal(t, tc.expectedConstraint, constraint) | ||
// Test that the constraint name survives an encode/decode cycle. | ||
enc := errors.EncodeError(context.Background(), tc.err) | ||
err2 := errors.DecodeError(context.Background(), enc) | ||
constraint = GetConstraintName(err2) | ||
require.Equal(t, tc.expectedConstraint, constraint) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.