github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/database_sql_is_column_exists_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 TestDatabaseSqlIsColumnExists(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  				`
    33  			CREATE TABLE series (
    34  				series_id Uint64,
    35  				title UTF8,
    36  				series_info UTF8,
    37  				release_date Date,
    38  				comment UTF8,
    39  				PRIMARY KEY (
    40  					series_id
    41  				)
    42  			);`,
    43  			)
    44  
    45  			return err
    46  		}, retry.WithIdempotent(true))
    47  
    48  		require.NoError(t, err)
    49  	})
    50  
    51  	t.Run("is-column-exists", func(t *testing.T) {
    52  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    53  			for _, column := range []string{"series_id", "title", "series_info", "release_date", "comment"} {
    54  				exists := true
    55  				err = cc.Raw(func(drvConn interface{}) (err error) {
    56  					q, ok := drvConn.(interface {
    57  						IsColumnExists(context.Context, string, string) (bool, error)
    58  					})
    59  
    60  					if !ok {
    61  						return errors.New("drvConn does not implement extended API")
    62  					}
    63  
    64  					exists, err = q.IsColumnExists(ctx, "series", column)
    65  					return err
    66  				})
    67  
    68  				if err != nil {
    69  					return err
    70  				}
    71  
    72  				require.True(t, exists)
    73  			}
    74  			return nil
    75  		}, retry.WithIdempotent(true))
    76  
    77  		require.NoError(t, err)
    78  	})
    79  }