@@ -124,36 +124,9 @@ func RemoveRecursive(ctx context.Context, db dbFoRemoveRecursive, pathToRemove s
124124 if pt == fullSysTablePath {
125125 continue
126126 }
127- switch t := dir .Children [j ].Type ; t {
128- case scheme .EntryDirectory :
129- if err = rmPath (i + 1 , pt ); err != nil {
130- return xerrors .WithStackTrace (
131- fmt .Errorf ("recursive removing directory %q failed: %w" , pt , err ),
132- )
133- }
134-
135- case scheme .EntryTable , scheme .EntryColumnTable :
136- err = db .Table ().Do (ctx , func (ctx context.Context , session table.Session ) (err error ) {
137- return session .DropTable (ctx , pt )
138- }, table .WithIdempotent ())
139- if err != nil {
140- return xerrors .WithStackTrace (
141- fmt .Errorf ("removing table %q failed: %w" , pt , err ),
142- )
143- }
144-
145- case scheme .EntryTopic :
146- err = db .Topic ().Drop (ctx , pt )
147- if err != nil {
148- return xerrors .WithStackTrace (
149- fmt .Errorf ("removing topic %q failed: %w" , pt , err ),
150- )
151- }
152-
153- default :
154- return xerrors .WithStackTrace (
155- fmt .Errorf ("unknown entry type: %s" , t .String ()),
156- )
127+ err = removeEntry (ctx , i , pt , j , db , & dir , rmPath )
128+ if err != nil {
129+ return err
157130 }
158131 }
159132
@@ -183,3 +156,50 @@ func removeWithPrefix(pathToRemove string, db dbFoRemoveRecursive) string {
183156
184157 return pathToRemove
185158}
159+
160+ // removeEntry removes an entry from the database.
161+ // It takes a context, an index, a path, an index, a database, a directory, and a function to recursively remove directories.
162+ // It returns an error if the removal fails.
163+ func removeEntry (
164+ ctx context.Context ,
165+ i int ,
166+ pt string ,
167+ j int ,
168+ db dbFoRemoveRecursive ,
169+ dir * scheme.Directory ,
170+ rmPath func (int , string ) error ,
171+ ) error {
172+ switch t := dir .Children [j ].Type ; t {
173+ case scheme .EntryDirectory :
174+ if err := rmPath (i + 1 , pt ); err != nil {
175+ return xerrors .WithStackTrace (
176+ fmt .Errorf ("recursive removing directory %q failed: %w" , pt , err ),
177+ )
178+ }
179+
180+ case scheme .EntryTable , scheme .EntryColumnTable :
181+ err := db .Table ().Do (ctx , func (ctx context.Context , session table.Session ) (err error ) {
182+ return session .DropTable (ctx , pt )
183+ }, table .WithIdempotent ())
184+ if err != nil {
185+ return xerrors .WithStackTrace (
186+ fmt .Errorf ("removing table %q failed: %w" , pt , err ),
187+ )
188+ }
189+
190+ case scheme .EntryTopic :
191+ err := db .Topic ().Drop (ctx , pt )
192+ if err != nil {
193+ return xerrors .WithStackTrace (
194+ fmt .Errorf ("removing topic %q failed: %w" , pt , err ),
195+ )
196+ }
197+
198+ default :
199+ return xerrors .WithStackTrace (
200+ fmt .Errorf ("unknown entry type: %s" , t .String ()),
201+ )
202+ }
203+
204+ return nil
205+ }
0 commit comments