Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix comment on constraint for tables in a schema #71985

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions pkg/sql/comment_on_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
Expand Down Expand Up @@ -62,8 +61,10 @@ func (n *commentOnConstraintNode) startExec(params runParams) error {
if err != nil {
return err
}
cSchema, ok := schemadesc.GetVirtualSchemaByID(n.tableDesc.GetParentSchemaID())
if !ok {
schema, err := params.p.Descriptors().GetImmutableSchemaByID(
params.ctx, params.extendedEvalCtx.Txn, n.tableDesc.GetParentSchemaID(), tree.SchemaLookupFlags{},
)
if err != nil {
return err
}

Expand All @@ -78,16 +79,16 @@ func (n *commentOnConstraintNode) startExec(params runParams) error {
switch kind := constraint.Kind; kind {
case descpb.ConstraintTypePK:
constraintDesc := constraint.Index
n.oid = hasher.PrimaryKeyConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc)
n.oid = hasher.PrimaryKeyConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc)
case descpb.ConstraintTypeFK:
constraintDesc := constraint.FK
n.oid = hasher.ForeignKeyConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc)
n.oid = hasher.ForeignKeyConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc)
case descpb.ConstraintTypeUnique:
constraintDesc := constraint.Index.ID
n.oid = hasher.UniqueConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc)
n.oid = hasher.UniqueConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc)
case descpb.ConstraintTypeCheck:
constraintDesc := constraint.CheckConstraint
n.oid = hasher.CheckConstraintOid(n.tableDesc.GetParentID(), cSchema.GetName(), n.tableDesc.GetID(), constraintDesc)
n.oid = hasher.CheckConstraintOid(n.tableDesc.GetParentID(), schema.GetName(), n.tableDesc.GetID(), constraintDesc)

}
// Setting the comment to NULL is the
Expand Down
9 changes: 8 additions & 1 deletion pkg/sql/comment_on_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func TestCommentOnConstraint(t *testing.T) {
CREATE DATABASE d;
SET DATABASE = d;
CREATE TABLE t ( a int UNIQUE, b numeric CONSTRAINT positive_price CHECK (b > 0), c int CHECK (b > c), CONSTRAINT pkey PRIMARY KEY (a,c));
CREATE TABLE t2 (a UUID PRIMARY KEY, b int NOT NULL REFERENCES t (a))
CREATE TABLE t2 (a UUID PRIMARY KEY, b int NOT NULL REFERENCES t (a));
CREATE SCHEMA s;
CREATE TABLE s.t ( a int UNIQUE, b numeric CONSTRAINT positive_price CHECK (b > 0), c int CHECK (b > c), CONSTRAINT pkey PRIMARY KEY (a,c));
`); err != nil {
t.Fatal(err)
}
Expand All @@ -48,6 +50,11 @@ func TestCommentOnConstraint(t *testing.T) {
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='t_a_key'`,
gosql.NullString{String: `unique_comment`, Valid: true},
},
{
`COMMENT ON CONSTRAINT t_a_key ON s.t IS 'unique_comment'`,
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='t_a_key'`,
gosql.NullString{String: `unique_comment`, Valid: true},
},
{
`COMMENT ON CONSTRAINT positive_price ON t IS 'check_comment'`,
`SELECT obj_description(oid, 'pg_constraint') FROM pg_constraint WHERE conname='positive_price'`,
Expand Down