github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/database_sql_get_indexes_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"database/sql"
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/ydb-platform/ydb-go-sdk/v3"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/retry"
    16  )
    17  
    18  func TestDatabaseSqlGetIndexes(t *testing.T) {
    19  	var (
    20  		scope = newScope(t)
    21  		db    = scope.SQLDriverWithFolder()
    22  	)
    23  
    24  	defer func() {
    25  		_ = db.Close()
    26  	}()
    27  
    28  	t.Run("create-tables", func(t *testing.T) {
    29  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    30  			_, err = cc.ExecContext(
    31  				ydb.WithQueryMode(ctx, ydb.SchemeQueryMode),
    32  				`CREATE TABLE seasons (
    33  				series_id Uint64,
    34  				season_id Uint64,
    35  				title UTF8,
    36  				first_aired Date,
    37  				last_aired Date,
    38  				INDEX index_series_title GLOBAL ON (title),
    39  				INDEX index_seasons_aired_date GLOBAL ON (first_aired, last_aired),
    40  				PRIMARY KEY (
    41  					series_id,
    42  					season_id
    43  				)
    44  			)`,
    45  			)
    46  
    47  			if err != nil {
    48  				return err
    49  			}
    50  
    51  			return nil
    52  		}, retry.WithIdempotent(true))
    53  
    54  		require.NoError(t, err)
    55  	})
    56  
    57  	t.Run("get-indexes", func(t *testing.T) {
    58  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    59  			indexes := make([]string, 0)
    60  			err = cc.Raw(func(drvConn interface{}) (err error) {
    61  				q, ok := drvConn.(interface {
    62  					GetIndexes(context.Context, string) ([]string, error)
    63  				})
    64  
    65  				if !ok {
    66  					return errors.New("drvConn does not implement extended API")
    67  				}
    68  
    69  				indexes, err = q.GetIndexes(ctx, "./seasons")
    70  				return err
    71  			})
    72  
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			require.ElementsMatch(t,
    78  				[]string{"index_series_title", "index_seasons_aired_date"},
    79  				indexes)
    80  			return nil
    81  		}, retry.WithIdempotent(true))
    82  
    83  		require.NoError(t, err)
    84  	})
    85  }