github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/database_sql_get_index_columns_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 TestDatabaseSqlGetIndexColumns(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-index-columns", func(t *testing.T) {
    58  		for _, test := range []struct {
    59  			IndexName      string
    60  			IndexedColumns []string
    61  		}{
    62  			{
    63  				IndexName:      "index_series_title",
    64  				IndexedColumns: []string{"title"},
    65  			},
    66  			{
    67  				IndexName:      "index_seasons_aired_date",
    68  				IndexedColumns: []string{"first_aired", "last_aired"},
    69  			},
    70  		} {
    71  			err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    72  				columns := make([]string, 0)
    73  				err = cc.Raw(func(drvConn interface{}) (err error) {
    74  					q, ok := drvConn.(interface {
    75  						GetIndexColumns(context.Context, string, string) ([]string, error)
    76  					})
    77  
    78  					if !ok {
    79  						return errors.New("drvConn does not implement extended API")
    80  					}
    81  
    82  					columns, err = q.GetIndexColumns(ctx, "./seasons", test.IndexName)
    83  					return err
    84  				})
    85  
    86  				if err != nil {
    87  					return err
    88  				}
    89  
    90  				require.ElementsMatch(t,
    91  					test.IndexedColumns,
    92  					columns)
    93  				return nil
    94  			}, retry.WithIdempotent(true))
    95  
    96  			require.NoError(t, err)
    97  		}
    98  	})
    99  }