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

     1  // Copyright 2019 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 TestCommentOnIndex(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  		SET DATABASE = d;
    34  		CREATE TABLE t (c INT, INDEX t_c_idx (c));
    35  	`); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	testCases := []struct {
    40  		exec   string
    41  		query  string
    42  		expect gosql.NullString
    43  	}{
    44  		{
    45  			`COMMENT ON INDEX t_c_idx IS 'index_comment'`,
    46  			`SELECT obj_description(oid) from pg_class WHERE relname='t_c_idx';`,
    47  			gosql.NullString{String: `index_comment`, Valid: true},
    48  		},
    49  		{
    50  			`TRUNCATE t`,
    51  			`SELECT obj_description(oid) from pg_class WHERE relname='t_c_idx';`,
    52  			gosql.NullString{String: `index_comment`, Valid: true},
    53  		},
    54  		{
    55  			`COMMENT ON INDEX t_c_idx IS NULL`,
    56  			`SELECT obj_description(oid) from pg_class WHERE relname='t_c_idx';`,
    57  			gosql.NullString{Valid: false},
    58  		},
    59  	}
    60  
    61  	for _, tc := range testCases {
    62  		if _, err := db.Exec(tc.exec); err != nil {
    63  			t.Fatal(err)
    64  		}
    65  
    66  		row := db.QueryRow(tc.query)
    67  		var comment gosql.NullString
    68  		if err := row.Scan(&comment); err != nil {
    69  			t.Fatal(err)
    70  		}
    71  		if tc.expect != comment {
    72  			t.Fatalf("expected comment %v, got %v", tc.expect, comment)
    73  		}
    74  	}
    75  }
    76  
    77  func TestCommentOnIndexWhenDropTable(t *testing.T) {
    78  	defer leaktest.AfterTest(t)()
    79  
    80  	params, _ := tests.CreateTestServerParams()
    81  	s, db, _ := serverutils.StartServer(t, params)
    82  	defer s.Stopper().Stop(context.Background())
    83  
    84  	if _, err := db.Exec(`
    85  		CREATE DATABASE d;
    86  		SET DATABASE = d;
    87  		CREATE TABLE t (c INT, INDEX t_c_idx (c));
    88  	`); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	if _, err := db.Exec(`COMMENT ON INDEX t_c_idx IS 'index_comment'`); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	if _, err := db.Exec(`DROP TABLE t`); err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	row := db.QueryRow(`SELECT comment FROM system.comments LIMIT 1`)
   101  	var comment string
   102  	err := row.Scan(&comment)
   103  	if !errors.Is(err, gosql.ErrNoRows) {
   104  		if err != nil {
   105  			t.Fatal(err)
   106  		}
   107  
   108  		t.Fatal("comment remain")
   109  	}
   110  }
   111  
   112  func TestCommentOnIndexWhenDropIndex(t *testing.T) {
   113  	defer leaktest.AfterTest(t)()
   114  
   115  	params, _ := tests.CreateTestServerParams()
   116  	s, db, _ := serverutils.StartServer(t, params)
   117  	defer s.Stopper().Stop(context.Background())
   118  
   119  	if _, err := db.Exec(`
   120  		CREATE DATABASE d;
   121  		SET DATABASE = d;
   122  		CREATE TABLE t (c INT, INDEX t_c_idx (c));
   123  	`); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	if _, err := db.Exec(`COMMENT ON INDEX t_c_idx IS 'index_comment'`); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	if _, err := db.Exec(`DROP INDEX t_c_idx`); err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	row := db.QueryRow(`SELECT comment FROM system.comments LIMIT 1`)
   136  	var comment string
   137  	err := row.Scan(&comment)
   138  	if !errors.Is(err, gosql.ErrNoRows) {
   139  		if err != nil {
   140  			t.Fatal(err)
   141  		}
   142  
   143  		t.Fatal("comment remain")
   144  	}
   145  }