-
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.
sql: add missing obj_description case
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
Showing
3 changed files
with
112 additions
and
0 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
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") | ||
} | ||
} |
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