Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for entire database namespaces #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/verifier/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,19 @@ func (verifier *Verifier) CreateInitialTasks() error {
verifier.logger.Error().Msgf("%s", err)
return err
}
// Check for database names and append their collections
if NamespacesContainsDBName(verifier.srcNamespaces) {
namespaces, dbs := ParseDBNamespaces(verifier.srcNamespaces)

dbNamespaces, err := ListUserCollectionsForDBs(context.Background(), verifier.logger, verifier.srcClient, true /* include views */, dbs)
if err != nil {
verifier.logger.Error().Msgf("Failed to parse database namespaces: %s", err)
return err
}

verifier.srcNamespaces = append(namespaces, dbNamespaces...)
verifier.SetNamespaceMap()
}
}
isPrimary, err := verifier.CheckIsPrimary()
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion internal/verifier/list_namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ func ListAllUserCollections(ctx context.Context, logger *logger.Logger, client *
}
logger.Debug().Msgf("All user databases: %+v", dbNames)

return ListUserCollectionsForDBs(ctx, logger, client, includeViews, dbNames)
}

func ListUserCollectionsForDBs(ctx context.Context, logger *logger.Logger, client *mongo.Client, includeViews bool,
databases []string) ([]string, error) {

collectionNamespaces := []string{}
for _, dbName := range dbNames {
for _, dbName := range databases {
db := client.Database(dbName)
filter := bson.D{{"name", bson.D{{"$nin", bson.A{ExcludedSystemCollRegex}}}}}
if !includeViews {
Expand Down
24 changes: 24 additions & 0 deletions internal/verifier/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,27 @@ func GetLastOpTimeAndSyncShardClusterTime(
t, i := rawOperationTime.Timestamp()
return &primitive.Timestamp{T: t, I: i}, nil
}

func NamespacesContainsDBName(namespaces []string) bool {
for _, namespace := range namespaces {
if strings.Index(namespace, ".") < 0 {
return true
}
}
return false
}

func ParseDBNamespaces(namespaces []string) ([]string, []string) {
databases := []string{}
parsedNamespaces := []string{}
// strip database names from namespaces - db collections will be appended to the namespaces
for _, name := range namespaces {
db, coll := SplitNamespace(name)
if coll == "" {
databases = append(databases, db)
} else {
parsedNamespaces = append(parsedNamespaces, name)
}
}
return parsedNamespaces, databases
}