Skip to content

Commit

Permalink
sql: add missing obj_description case
Browse files Browse the repository at this point in the history
Found with the following query:

```sql
COMMENT ON SCHEMA public IS 'hello';

SELECT obj_description(oid, 'pg_namespace')
FROM pg_namespace WHERE nspname = 'public';
```

Release note (sql change): The PostgreSQL compatibility
function `obj_description` now supports retrieving
comments on schemas.
  • Loading branch information
knz committed Sep 18, 2022
1 parent 5051c83 commit f068697
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ go_test(
"comment_on_constraint_test.go",
"comment_on_database_test.go",
"comment_on_index_test.go",
"comment_on_schema_test.go",
"comment_on_table_test.go",
"conn_executor_internal_test.go",
"conn_executor_savepoints_test.go",
Expand Down
109 changes: 109 additions & 0 deletions pkg/sql/comment_on_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2022 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 sql_test

import (
"context"
gosql "database/sql"
"testing"

"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)

func TestCommentOnSchema(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

params, _ := tests.CreateTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.Background())

if _, err := db.Exec(`
CREATE SCHEMA d;
`); err != nil {
t.Fatal(err)
}

testCases := []struct {
exec string
query string
expect gosql.NullString
}{
{
`COMMENT ON SCHEMA d IS 'foo'`,
`SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace WHERE nspname = 'd'`,
gosql.NullString{String: `foo`, Valid: true},
},
{
`ALTER SCHEMA d RENAME TO d2`,
`SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace WHERE nspname = 'd2'`,
gosql.NullString{String: `foo`, Valid: true},
},
{
`COMMENT ON SCHEMA d2 IS NULL`,
`SELECT obj_description(oid, 'pg_namespace') FROM pg_namespace WHERE nspname = 'd2'`,
gosql.NullString{Valid: false},
},
}

for _, tc := range testCases {
if _, err := db.Exec(tc.exec); err != nil {
t.Fatal(err)
}

row := db.QueryRow(tc.query)
var comment gosql.NullString
if err := row.Scan(&comment); err != nil {
t.Fatal(err)
}
if tc.expect != comment {
t.Fatalf("expected comment %v, got %v", tc.expect, comment)
}
}
}

func TestCommentOnSchemaWhenDrop(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

params, _ := tests.CreateTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.Background())

if _, err := db.Exec(`
CREATE SCHEMA d;
`); err != nil {
t.Fatal(err)
}

if _, err := db.Exec(`COMMENT ON SCHEMA d IS 'foo'`); err != nil {
t.Fatal(err)
}

if _, err := db.Exec(`DROP SCHEMA d`); err != nil {
t.Fatal(err)
}

row := db.QueryRow(`SELECT comment FROM system.comments LIMIT 1`)
var comment string
err := row.Scan(&comment)
if !errors.Is(err, gosql.ErrNoRows) {
if err != nil {
t.Fatal(err)
}

t.Fatal("dropped comment remain comment")
}
}
2 changes: 2 additions & 0 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,8 @@ func getCatalogOidForComments(catalogName string) (id int, ok bool) {
return catconstants.PgCatalogDescriptionTableID, true
case "pg_constraint":
return catconstants.PgCatalogConstraintTableID, true
case "pg_namespace":
return catconstants.PgCatalogNamespaceTableID, true
default:
// We currently only support comments on pg_class objects
// (columns, tables) in this context.
Expand Down

0 comments on commit f068697

Please sign in to comment.