Skip to content

Commit e7738ff

Browse files
authored
Merge pull request #772 from ydb-platform/is-column-table-exists
* Added `sugar.Is{Entry,ColumnTable}Exists` helper
2 parents 9a52113 + 828cf62 commit e7738ff

File tree

6 files changed

+91
-26
lines changed

6 files changed

+91
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added `sugar.Is{Entry,ColumnTable}Exists` helper
2+
13
## v3.48.0
24
* Fixed stopping topic reader by grpc stream shutdown
35
* Fixed `database/sql` driver for get and parse container ydb types

internal/scheme/helpers/check_exists.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,47 @@ func IsDirectoryExists(ctx context.Context, c schemeClient, directory string) (
5555
return false, nil
5656
}
5757

58-
func IsTableExists(ctx context.Context, c schemeClient, absTablePath string) (
58+
func IsEntryExists(ctx context.Context, c schemeClient, absPath string, entryTypes ...scheme.EntryType) (
5959
exists bool, _ error,
6060
) {
61-
if !strings.HasPrefix(absTablePath, c.Database()) {
61+
if !strings.HasPrefix(absPath, c.Database()) {
6262
return false, xerrors.WithStackTrace(fmt.Errorf(
63-
"table path '%s' must be inside database '%s'",
64-
absTablePath, c.Database(),
63+
"entry path '%s' must be inside database '%s'",
64+
absPath, c.Database(),
6565
))
66-
} else if absTablePath == c.Database() {
66+
} else if absPath == c.Database() {
6767
return false, xerrors.WithStackTrace(fmt.Errorf(
68-
"table path '%s' cannot be equals database name '%s'",
69-
absTablePath, c.Database(),
68+
"entry path '%s' cannot be equals database name '%s'",
69+
absPath, c.Database(),
7070
))
7171
}
72-
directory, tableName := path.Split(absTablePath)
72+
directory, entryName := path.Split(absPath)
7373
if exists, err := IsDirectoryExists(ctx, c, strings.TrimRight(directory, "/")); err != nil {
7474
return false, xerrors.WithStackTrace(err)
7575
} else if !exists {
7676
return false, nil
7777
}
7878
d, err := c.ListDirectory(ctx, directory)
7979
if err != nil {
80-
return false, err
80+
return false, xerrors.WithStackTrace(fmt.Errorf(
81+
"list directory '%s' failed: %w",
82+
directory, err,
83+
))
8184
}
8285
for i := range d.Children {
83-
if d.Children[i].Name != tableName {
86+
if d.Children[i].Name != entryName {
8487
continue
8588
}
86-
if d.Children[i].Type != scheme.EntryTable {
87-
return false, xerrors.WithStackTrace(fmt.Errorf(
88-
"entry '%s' in path '%s' is not a table: %s",
89-
tableName, directory, d.Children[i].Type.String(),
90-
))
89+
childrenType := d.Children[i].Type
90+
for _, entryType := range entryTypes {
91+
if childrenType == entryType {
92+
return true, nil
93+
}
9194
}
92-
return true, nil
95+
return false, xerrors.WithStackTrace(fmt.Errorf(
96+
"entry type of '%s' (%s) in path '%s' is not corresponds to %v",
97+
entryName, childrenType, directory, entryTypes,
98+
))
9399
}
94100
return false, nil
95101
}

internal/scheme/helpers/check_exists_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ func TestIsTableExists(t *testing.T) {
215215
},
216216
} {
217217
t.Run("", func(t *testing.T) {
218-
exists, err := IsTableExists(context.Background(), tt.client, tt.checkPath)
218+
exists, err := IsEntryExists(context.Background(),
219+
tt.client, tt.checkPath,
220+
scheme.EntryTable, scheme.EntryColumnTable,
221+
)
219222
if tt.err {
220223
require.Error(t, err)
221224
} else {

internal/xsql/conn.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ func (c *conn) Version(context.Context) (_ string, err error) {
440440

441441
func (c *conn) IsTableExists(ctx context.Context, tableName string) (tableExists bool, err error) {
442442
tableName = c.normalizePath(tableName)
443-
tableExists, err = helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
443+
tableExists, err = helpers.IsEntryExists(ctx,
444+
c.connector.parent.Scheme(), tableName,
445+
scheme.EntryTable, scheme.EntryColumnTable,
446+
)
444447
if err != nil {
445448
return false, xerrors.WithStackTrace(err)
446449
}
@@ -449,7 +452,10 @@ func (c *conn) IsTableExists(ctx context.Context, tableName string) (tableExists
449452

450453
func (c *conn) IsColumnExists(ctx context.Context, tableName, columnName string) (columnExists bool, err error) {
451454
tableName = c.normalizePath(tableName)
452-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
455+
tableExists, err := helpers.IsEntryExists(ctx,
456+
c.connector.parent.Scheme(), tableName,
457+
scheme.EntryTable, scheme.EntryColumnTable,
458+
)
453459
if err != nil {
454460
return false, xerrors.WithStackTrace(err)
455461
}
@@ -478,7 +484,10 @@ func (c *conn) IsColumnExists(ctx context.Context, tableName, columnName string)
478484

479485
func (c *conn) GetColumns(ctx context.Context, tableName string) (columns []string, err error) {
480486
tableName = c.normalizePath(tableName)
481-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
487+
tableExists, err := helpers.IsEntryExists(ctx,
488+
c.connector.parent.Scheme(), tableName,
489+
scheme.EntryTable, scheme.EntryColumnTable,
490+
)
482491
if err != nil {
483492
return nil, xerrors.WithStackTrace(err)
484493
}
@@ -504,7 +513,10 @@ func (c *conn) GetColumns(ctx context.Context, tableName string) (columns []stri
504513

505514
func (c *conn) GetColumnType(ctx context.Context, tableName, columnName string) (dataType string, err error) {
506515
tableName = c.normalizePath(tableName)
507-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
516+
tableExists, err := helpers.IsEntryExists(ctx,
517+
c.connector.parent.Scheme(), tableName,
518+
scheme.EntryTable, scheme.EntryColumnTable,
519+
)
508520
if err != nil {
509521
return "", xerrors.WithStackTrace(err)
510522
}
@@ -541,7 +553,10 @@ func (c *conn) GetColumnType(ctx context.Context, tableName, columnName string)
541553

542554
func (c *conn) GetPrimaryKeys(ctx context.Context, tableName string) (pkCols []string, err error) {
543555
tableName = c.normalizePath(tableName)
544-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
556+
tableExists, err := helpers.IsEntryExists(ctx,
557+
c.connector.parent.Scheme(), tableName,
558+
scheme.EntryTable, scheme.EntryColumnTable,
559+
)
545560
if err != nil {
546561
return nil, xerrors.WithStackTrace(err)
547562
}
@@ -565,7 +580,10 @@ func (c *conn) GetPrimaryKeys(ctx context.Context, tableName string) (pkCols []s
565580

566581
func (c *conn) IsPrimaryKey(ctx context.Context, tableName, columnName string) (ok bool, err error) {
567582
tableName = c.normalizePath(tableName)
568-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
583+
tableExists, err := helpers.IsEntryExists(ctx,
584+
c.connector.parent.Scheme(), tableName,
585+
scheme.EntryTable, scheme.EntryColumnTable,
586+
)
569587
if err != nil {
570588
return false, xerrors.WithStackTrace(err)
571589
}
@@ -701,7 +719,10 @@ func (c *conn) GetAllTables(ctx context.Context, folder string) (tables []string
701719

702720
func (c *conn) GetIndexes(ctx context.Context, tableName string) (indexes []string, err error) {
703721
tableName = c.normalizePath(tableName)
704-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
722+
tableExists, err := helpers.IsEntryExists(ctx,
723+
c.connector.parent.Scheme(), tableName,
724+
scheme.EntryTable, scheme.EntryColumnTable,
725+
)
705726
if err != nil {
706727
return nil, xerrors.WithStackTrace(err)
707728
}
@@ -728,7 +749,10 @@ func (c *conn) GetIndexes(ctx context.Context, tableName string) (indexes []stri
728749

729750
func (c *conn) GetIndexColumns(ctx context.Context, tableName, indexName string) (columns []string, err error) {
730751
tableName = c.normalizePath(tableName)
731-
tableExists, err := helpers.IsTableExists(ctx, c.connector.parent.Scheme(), tableName)
752+
tableExists, err := helpers.IsEntryExists(ctx,
753+
c.connector.parent.Scheme(), tableName,
754+
scheme.EntryTable, scheme.EntryColumnTable,
755+
)
732756
if err != nil {
733757
return nil, xerrors.WithStackTrace(err)
734758
}

scheme/scheme_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package scheme
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestEntryTypePrintf(t *testing.T) {
11+
require.Equal(t, "[Table ColumnTable]", fmt.Sprintf("%v", []EntryType{EntryTable, EntryColumnTable}))
12+
}

sugar/check_exists.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ import (
99
)
1010

1111
func IsTableExists(ctx context.Context, c scheme.Client, absTablePath string) (exists bool, _ error) {
12-
exists, err := helpers.IsTableExists(ctx, c, absTablePath)
12+
exists, err := helpers.IsEntryExists(ctx, c, absTablePath, scheme.EntryTable)
13+
if err != nil {
14+
return exists, xerrors.WithStackTrace(err)
15+
}
16+
return exists, nil
17+
}
18+
19+
func IsColumnTableExists(ctx context.Context, c scheme.Client, absTablePath string) (exists bool, _ error) {
20+
exists, err := helpers.IsEntryExists(ctx, c, absTablePath, scheme.EntryColumnTable)
21+
if err != nil {
22+
return exists, xerrors.WithStackTrace(err)
23+
}
24+
return exists, nil
25+
}
26+
27+
func IsEntryExists(ctx context.Context, c scheme.Client, absPath string, entryTypes ...scheme.EntryType) (
28+
exists bool, _ error,
29+
) {
30+
exists, err := helpers.IsEntryExists(ctx, c, absPath, entryTypes...)
1331
if err != nil {
1432
return exists, xerrors.WithStackTrace(err)
1533
}

0 commit comments

Comments
 (0)