@@ -12,19 +12,28 @@ import (
1212 "github.com/ydb-platform/ydb-go-sdk/v3/table"
1313)
1414
15- // MakePath creates path inside database
16- func MakePath (ctx context.Context , db ydb.Connection , path string ) error {
17- for i := len (db .Name ()) + 1 ; i < len (path ); i ++ {
18- x := strings .IndexByte (path [i :], '/' )
15+ const (
16+ sysTable = ".sys"
17+ )
18+
19+ // MakeRecursive creates path inside database
20+ func MakeRecursive (ctx context.Context , db ydb.Connection , folder string ) error {
21+ folder = path .Join (db .Name (), folder )
22+ for i := len (db .Name ()) + 1 ; i < len (folder ); i ++ {
23+ x := strings .IndexByte (folder [i :], '/' )
1924 if x == - 1 {
20- x = len (path [i :]) - 1
25+ x = len (folder [i :]) - 1
2126 }
2227 i += x
23- sub := path [:i + 1 ]
28+ sub := folder [:i + 1 ]
2429 info , err := db .Scheme ().DescribePath (ctx , sub )
2530 var opErr * errors.OpError
2631 if errors .As (err , & opErr ) && opErr .Reason == errors .StatusSchemeError {
2732 err = db .Scheme ().MakeDirectory (ctx , sub )
33+ if err != nil {
34+ return err
35+ }
36+ info , err = db .Scheme ().DescribePath (ctx , sub )
2837 }
2938 if err != nil {
3039 return err
@@ -41,19 +50,15 @@ func MakePath(ctx context.Context, db ydb.Connection, path string) error {
4150 )
4251 }
4352 }
44-
4553 return nil
4654}
4755
48- // RmPath remove selected directory or table names in database.
56+ // RemoveRecursive remove selected directory or table names in database.
4957// All database entities in prefix path will remove if names list is empty.
5058// Empty prefix means than use root of database.
51- // RmPath method equal bash command `rm -rf ./pathToRemove/{name1,name2,name3}`
52- func RmPath (ctx context.Context , db ydb.Connection , pathToRemove string , names ... string ) error {
53- filter := make (map [string ]struct {}, len (names ))
54- for _ , n := range names {
55- filter [n ] = struct {}{}
56- }
59+ // RemoveRecursive method equal bash command `rm -rf ./path/to/remove`
60+ func RemoveRecursive (ctx context.Context , db ydb.Connection , pathToRemove string ) error {
61+ fullSysTablePath := path .Join (db .Name (), sysTable )
5762 var list func (int , string ) error
5863 list = func (i int , p string ) error {
5964 dir , err := db .Scheme ().ListDirectory (ctx , p )
@@ -65,10 +70,10 @@ func RmPath(ctx context.Context, db ydb.Connection, pathToRemove string, names .
6570 return err
6671 }
6772 for _ , child := range dir .Children {
68- if _ , has := filter [child .Name ]; ! has {
73+ pt := path .Join (p , child .Name )
74+ if pt == fullSysTablePath {
6975 continue
7076 }
71- pt := path .Join (p , child .Name )
7277 switch child .Type {
7378 case scheme .EntryDirectory :
7479 if err = list (i + 1 , pt ); err != nil {
0 commit comments