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

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/ydb-platform/ydb-go-sdk/v3"
    14  )
    15  
    16  func TestDatabaseSqlScheme(t *testing.T) {
    17  	var (
    18  		scope = newScope(t)
    19  		db    = scope.SQLDriverWithFolder()
    20  	)
    21  
    22  	defer func() {
    23  		_ = db.Close()
    24  	}()
    25  
    26  	t.Run("drop-tables", func(t *testing.T) {
    27  		cc, err := db.Conn(scope.Ctx)
    28  		require.NoError(t, err)
    29  
    30  		defer func() {
    31  			_ = cc.Close()
    32  		}()
    33  
    34  		tables := make([]string, 0)
    35  		err = cc.Raw(func(drvConn interface{}) error {
    36  			q, ok := drvConn.(interface {
    37  				GetTables(ctx context.Context, folder string, recursive bool, excludeSysDirs bool) ([]string, error)
    38  			})
    39  			if !ok {
    40  				return fmt.Errorf("drvConn does not implement scheme methods")
    41  			}
    42  
    43  			tables, err = q.GetTables(scope.Ctx, ".", false, true)
    44  			return err
    45  		})
    46  		require.NoError(t, err)
    47  
    48  		for _, pathToTable := range tables {
    49  			_, err = db.ExecContext(ydb.WithQueryMode(scope.Ctx, ydb.SchemeQueryMode),
    50  				fmt.Sprintf("DROP TABLE `%s`", pathToTable))
    51  
    52  			require.NoError(t, err)
    53  		}
    54  	})
    55  
    56  	t.Run("create-tables", func(t *testing.T) {
    57  		_, err := db.ExecContext(
    58  			ydb.WithQueryMode(scope.Ctx, ydb.SchemeQueryMode),
    59  			`
    60  			CREATE TABLE series (
    61  				series_id Uint64,
    62  				title UTF8,
    63  				series_info UTF8,
    64  				release_date Date,
    65  				comment UTF8,
    66  				PRIMARY KEY (
    67  					series_id
    68  				)
    69  			);`,
    70  		)
    71  
    72  		require.NoError(t, err)
    73  	})
    74  
    75  	t.Run("check-table-exists", func(t *testing.T) {
    76  		cc, err := db.Conn(scope.Ctx)
    77  		require.NoError(t, err)
    78  
    79  		defer func() {
    80  			_ = cc.Close()
    81  		}()
    82  
    83  		err = cc.Raw(func(drvConn interface{}) (err error) {
    84  			q, ok := drvConn.(interface {
    85  				IsTableExists(context.Context, string) (bool, error)
    86  			})
    87  			if !ok {
    88  				return fmt.Errorf("drvConn does not implement scheme methods")
    89  			}
    90  
    91  			exists, err := q.IsTableExists(scope.Ctx, "series")
    92  			if err != nil {
    93  				return err
    94  			}
    95  
    96  			require.True(t, exists)
    97  
    98  			if !exists {
    99  				t.Logf("expected: table 'series' exists")
   100  			}
   101  			return nil
   102  		})
   103  		require.NoError(t, err)
   104  	})
   105  
   106  	t.Run("get-columns", func(t *testing.T) {
   107  		cc, err := db.Conn(scope.Ctx)
   108  		require.NoError(t, err)
   109  
   110  		defer func() {
   111  			_ = cc.Close()
   112  		}()
   113  
   114  		err = cc.Raw(func(drvConn interface{}) (err error) {
   115  			q, ok := drvConn.(interface {
   116  				GetColumns(context.Context, string) ([]string, error)
   117  			})
   118  			if !ok {
   119  				return fmt.Errorf("drvConn does not implement scheme methods")
   120  			}
   121  
   122  			columns, err := q.GetColumns(scope.Ctx, "series")
   123  			if err != nil {
   124  				return err
   125  			}
   126  
   127  			require.ElementsMatch(t,
   128  				[]string{"series_id", "title", "series_info", "release_date", "comment"},
   129  				columns)
   130  
   131  			return nil
   132  		})
   133  		require.NoError(t, err)
   134  	})
   135  }