github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/database_sql_is_table_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 TestDatabaseSqlIsTableExists(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("drop-if-exists", func(t *testing.T) {
    29  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    30  			exists := true
    31  			err = cc.Raw(func(drvConn interface{}) (err error) {
    32  				q, ok := drvConn.(interface {
    33  					IsTableExists(context.Context, string) (bool, error)
    34  				})
    35  
    36  				if !ok {
    37  					return errors.New("drvConn does not implement extended API")
    38  				}
    39  
    40  				exists, err = q.IsTableExists(ctx, "series")
    41  				return err
    42  			})
    43  			if err != nil {
    44  				return err
    45  			}
    46  
    47  			if exists {
    48  				_, err = cc.ExecContext(
    49  					ydb.WithQueryMode(ctx, ydb.SchemeQueryMode),
    50  					"DROP TABLE `series`")
    51  			}
    52  
    53  			return err
    54  		}, retry.WithIdempotent(true))
    55  
    56  		require.NoError(t, err)
    57  	})
    58  
    59  	t.Run("create-tables", func(t *testing.T) {
    60  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    61  			_, err = cc.ExecContext(
    62  				ydb.WithQueryMode(ctx, ydb.SchemeQueryMode),
    63  				`
    64  			CREATE TABLE series (
    65  				series_id Uint64,
    66  				title UTF8,
    67  				series_info UTF8,
    68  				release_date Date,
    69  				comment UTF8,
    70  				PRIMARY KEY (
    71  					series_id
    72  				)
    73  			);`,
    74  			)
    75  
    76  			return err
    77  		}, retry.WithIdempotent(true))
    78  
    79  		require.NoError(t, err)
    80  	})
    81  
    82  	t.Run("is-table-exists", func(t *testing.T) {
    83  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    84  			exists := false
    85  			err = cc.Raw(func(drvConn interface{}) (err error) {
    86  				q, ok := drvConn.(interface {
    87  					IsTableExists(context.Context, string) (bool, error)
    88  				})
    89  
    90  				if !ok {
    91  					return errors.New("drvConn does not implement extended API")
    92  				}
    93  
    94  				exists, err = q.IsTableExists(ctx, "series")
    95  				return err
    96  			})
    97  
    98  			if err != nil {
    99  				return err
   100  			}
   101  
   102  			require.True(t, exists)
   103  			return nil
   104  		}, retry.WithIdempotent(true))
   105  
   106  		require.NoError(t, err)
   107  	})
   108  }