Skip to content

Commit 7a645e6

Browse files
committed
refactor: enhance database validation error reporting
1 parent 5cc7c53 commit 7a645e6

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

option/option.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package option
22

33
import (
44
"regexp"
5+
"sort"
56
"strings"
67

7-
"github.com/cloudberry-contrib/cbcopy/utils"
88
"github.com/apache/cloudberry-go-libs/gplog"
9+
"github.com/cloudberry-contrib/cbcopy/utils"
910

1011
"github.com/pkg/errors"
1112
"github.com/spf13/pflag"
@@ -288,12 +289,21 @@ func validateTables(title string, tableList []string) ([]*DbTable, error) {
288289
}
289290

290291
if len(dbs) > 1 {
291-
return nil, errors.Errorf(`All %s should belong to the same database.`, title)
292+
return nil, formatDatabaseError(title, dbs)
292293
}
293294

294295
return result, nil
295296
}
296297

298+
func formatDatabaseError(subject string, dbs map[string]bool) error {
299+
dbNames := make([]string, 0)
300+
for db := range dbs {
301+
dbNames = append(dbNames, db)
302+
}
303+
sort.Strings(dbNames)
304+
return errors.Errorf(`All %s should belong to the same database. Found databases: %s`, subject, strings.Join(dbNames, ", "))
305+
}
306+
297307
func validateSchemas(schemas []string) ([]*DbSchema, error) {
298308
if len(schemas) == 0 {
299309
return nil, nil
@@ -313,7 +323,7 @@ func validateSchemas(schemas []string) ([]*DbSchema, error) {
313323
}
314324

315325
if len(dbs) > 1 {
316-
return nil, errors.Errorf(`All schemas should belong to the same database.`)
326+
return nil, formatDatabaseError("schemas", dbs)
317327
}
318328

319329
return result, nil

0 commit comments

Comments
 (0)