Skip to content

Commit

Permalink
sql: fix comment on constraint for tables in a schema
Browse files Browse the repository at this point in the history
Release note (sql change): Previously if you did comment on constraint
on a table in a schema the command would succeed but the comment
would not actually created. Now the comment is successfully created.
  • Loading branch information
RichardJCai committed Oct 26, 2021
1 parent dbf5853 commit d8893d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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

0 comments on commit d8893d7

Please sign in to comment.