Skip to content

Commit 03a7f7f

Browse files
authored
Merge pull request #464 from ydb-platform/is-table-exists
* Added `sugar.IsTableExists()` helper for check existence of table
2 parents a449c53 + 6f85e57 commit 03a7f7f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Added `meta.ConsumedUnits(metadata.MD)` method for getting consumed units from metadata
77
* Added `NestedCall` field to retry trace start infos for alarm on nested calls
88
* Added `topicoptions.WithWriterTrace` option for attach tracer into separated writer
9+
* Added `sugar.IsTableExists()` helper for check existence of table
910

1011
## v3.39.0
1112
* Removed message level partitioning from experimental topic API. It is unavailable on server side yet.

sugar/table_exists.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sugar
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"path"
7+
8+
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
9+
)
10+
11+
func IsTableExists(ctx context.Context, c scheme.Client, absTablePath string) (exists bool, _ error) {
12+
directory, tableName := path.Split(absTablePath)
13+
d, err := c.ListDirectory(ctx, directory)
14+
if err != nil {
15+
return false, err
16+
}
17+
for _, e := range d.Children {
18+
if e.Name != tableName {
19+
continue
20+
}
21+
if e.Type != scheme.EntryTable {
22+
return false, fmt.Errorf(
23+
"entry '%s' in path '%s' is not a table: %s",
24+
tableName, directory, e.Type.String(),
25+
)
26+
}
27+
return true, nil
28+
}
29+
return false, nil
30+
}

0 commit comments

Comments
 (0)