github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/comment_on_database_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql_test
    12  
    13  import (
    14  	"context"
    15  	gosql "database/sql"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/sql/tests"
    19  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    20  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    21  	"github.com/cockroachdb/errors"
    22  )
    23  
    24  func TestCommentOnDatabase(t *testing.T) {
    25  	defer leaktest.AfterTest(t)()
    26  
    27  	params, _ := tests.CreateTestServerParams()
    28  	s, db, _ := serverutils.StartServer(t, params)
    29  	defer s.Stopper().Stop(context.Background())
    30  
    31  	if _, err := db.Exec(`
    32  		CREATE DATABASE d;
    33  	`); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	testCases := []struct {
    38  		exec   string
    39  		query  string
    40  		expect gosql.NullString
    41  	}{
    42  		{
    43  			`COMMENT ON DATABASE d IS 'foo'`,
    44  			`SELECT shobj_description(oid, 'pg_database') FROM pg_database WHERE datname = 'd'`,
    45  			gosql.NullString{String: `foo`, Valid: true},
    46  		},
    47  		{
    48  			`ALTER DATABASE d RENAME TO d2`,
    49  			`SELECT shobj_description(oid, 'pg_database') FROM pg_database WHERE datname = 'd2'`,
    50  			gosql.NullString{String: `foo`, Valid: true},
    51  		},
    52  		{
    53  			`COMMENT ON DATABASE d2 IS NULL`,
    54  			`SELECT shobj_description(oid, 'pg_database') FROM pg_database WHERE datname = 'd2'`,
    55  			gosql.NullString{Valid: false},
    56  		},
    57  	}
    58  
    59  	for _, tc := range testCases {
    60  		if _, err := db.Exec(tc.exec); err != nil {
    61  			t.Fatal(err)
    62  		}
    63  
    64  		row := db.QueryRow(tc.query)
    65  		var comment gosql.NullString
    66  		if err := row.Scan(&comment); err != nil {
    67  			t.Fatal(err)
    68  		}
    69  		if tc.expect != comment {
    70  			t.Fatalf("expected comment %v, got %v", tc.expect, comment)
    71  		}
    72  	}
    73  }
    74  
    75  func TestCommentOnDatabaseWhenDrop(t *testing.T) {
    76  	defer leaktest.AfterTest(t)()
    77  
    78  	params, _ := tests.CreateTestServerParams()
    79  	s, db, _ := serverutils.StartServer(t, params)
    80  	defer s.Stopper().Stop(context.Background())
    81  
    82  	if _, err := db.Exec(`
    83  		CREATE DATABASE d;
    84  	`); err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	if _, err := db.Exec(`COMMENT ON DATABASE d IS 'foo'`); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	if _, err := db.Exec(`DROP DATABASE d`); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	row := db.QueryRow(`SELECT comment FROM system.comments LIMIT 1`)
    97  	var comment string
    98  	err := row.Scan(&comment)
    99  	if !errors.Is(err, gosql.ErrNoRows) {
   100  		if err != nil {
   101  			t.Fatal(err)
   102  		}
   103  
   104  		t.Fatal("dropped comment remain comment")
   105  	}
   106  }