From f10e60b702c1b6ca771188d8d71d2eb1dafdf36a Mon Sep 17 00:00:00 2001 From: Sveta Smirnova Date: Mon, 31 Jul 2023 17:37:49 +0300 Subject: [PATCH 1/2] PT-2235 - pt-mongodb-index-check does not support option --version Changed command version to flag version --- src/go/pt-mongodb-index-check/main.go | 14 ++++---------- src/go/pt-mongodb-index-check/main_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 src/go/pt-mongodb-index-check/main_test.go diff --git a/src/go/pt-mongodb-index-check/main.go b/src/go/pt-mongodb-index-check/main.go index 06de6bb78..24cc05e07 100644 --- a/src/go/pt-mongodb-index-check/main.go +++ b/src/go/pt-mongodb-index-check/main.go @@ -25,7 +25,7 @@ type cmdlineArgs struct { CheckDuplicated struct{} `cmd:"" name:"check-duplicates" help:"Check for duplicated indexes."` CheckAll struct{} `cmd:"" name:"check-all" help:"Check for unused and duplicated indexes."` ShowHelp struct{} `cmd:"" default:"1"` - Version struct{} `cmd:"" name:"version"` + Version kong.VersionFlag AllDatabases bool `name:"all-databases" xor:"db" help:"Check in all databases excluding system dbs"` Databases []string `name:"databases" xor:"db" help:"Comma separated list of databases to check"` @@ -55,15 +55,9 @@ var ( func main() { var args cmdlineArgs - kongctx := kong.Parse(&args, kong.UsageOnError()) - - if kongctx.Command() == "version" { - fmt.Println(toolname) - fmt.Printf("Version %s\n", Version) - fmt.Printf("Build: %s using %s\n", Build, GoVersion) - fmt.Printf("Commit: %s\n", Commit) - return - } + kongctx := kong.Parse(&args, kong.UsageOnError(), + kong.Vars{"version": fmt.Sprintf("%s\nVersion %s\nBuild: %s using %s\nCommit: %s", + toolname, Version, Build, GoVersion, Commit)}) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() diff --git a/src/go/pt-mongodb-index-check/main_test.go b/src/go/pt-mongodb-index-check/main_test.go new file mode 100644 index 000000000..d4fcf1ec3 --- /dev/null +++ b/src/go/pt-mongodb-index-check/main_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "os/exec" + "regexp" + "testing" +) + +/* +Option --version +*/ +func TestVersionOption(t *testing.T) { + out, err := exec.Command("../../../bin/"+toolname, "--version").Output() + if err != nil { + t.Errorf("error executing %s --version: %s", toolname, err.Error()) + } + // We are using MustCompile here, because hard-coded RE should not fail + re := regexp.MustCompile(toolname + `\n.*Version v?\d+\.\d+\.\d+\n`) + if !re.Match(out) { + t.Errorf("%s --version returns wrong result:\n%s", toolname, out) + } +} From 7b39eecd6e76bc6150253968fe0edba97f81bfdc Mon Sep 17 00:00:00 2001 From: Sveta Smirnova Date: Mon, 31 Jul 2023 17:45:50 +0300 Subject: [PATCH 2/2] PT-2235 - pt-mongodb-index-check does not support option --version Updated documentation --- docs/pt-mongodb-index-check.rst | 2 + src/go/pt-mongodb-index-check/README.md | 48 ---------------- src/go/pt-mongodb-index-check/README.rst | 72 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 48 deletions(-) delete mode 100644 src/go/pt-mongodb-index-check/README.md create mode 100644 src/go/pt-mongodb-index-check/README.rst diff --git a/docs/pt-mongodb-index-check.rst b/docs/pt-mongodb-index-check.rst index fd1ae31c3..3e31f0802 100644 --- a/docs/pt-mongodb-index-check.rst +++ b/docs/pt-mongodb-index-check.rst @@ -67,4 +67,6 @@ Available flags +----------------------------+----------------------------------------+ | –json | Show output as JSON | +----------------------------+----------------------------------------+ +| –version | Show version information | ++----------------------------+----------------------------------------+ diff --git a/src/go/pt-mongodb-index-check/README.md b/src/go/pt-mongodb-index-check/README.md deleted file mode 100644 index e4dae670e..000000000 --- a/src/go/pt-mongodb-index-check/README.md +++ /dev/null @@ -1,48 +0,0 @@ -# pt-mongodb-index-check - - - -## Introduction - -This tool can perform checks on MongoDB indexes. - -Currently, these checks are available: - -### Duplicated indexes - -Check for indexes that are the prefix of other indexes. For example if we have these 2 indexes - -``` -db.getSiblingDB("testdb").test_col.createIndex({"f1": 1, "f2": -1, "f3": 1, "f4": 1}, {"name": "idx_01"}); -db.getSiblingDB("testdb").test_col.createIndex({"f1": 1, "f2": -1, "f3": 1}, {"name": "idx_02"}); -``` - -The index `idx_02` is the prefix of `idx_01` because it has the same keys in the same order so, `idx_02` can be dropped. - -### Unused indexes. - -This check gets the `$indexstats` for all indexes and reports those having `accesses.ops` = 0. - -## Usage - -Run the program as `pt-mongodb-index-check [flags]` - -#### Available commands - -| Command | Description | -| ---------------- | ---------------------------------- | -| check-duplicated | Run checks for duplicated indexes. | -| check-unused | Run check for unused indexes. | -| check-all | Run all checks | - -#### Available flags - -| Flag | Description | -| ---- | ----------- | -|--all-databases|Check in all databases excluding system dbs.| -|--databases=DATABASES,...|Comma separated list of databases to check.| -|--all-collections|Check in all collections in the selected databases.| -|--collections=COLLECTIONS,...|Comma separated list of collections to check.| -|--mongodb.uri=|Connection URI| -|--json|Show output as JSON| - diff --git a/src/go/pt-mongodb-index-check/README.rst b/src/go/pt-mongodb-index-check/README.rst new file mode 100644 index 000000000..3e31f0802 --- /dev/null +++ b/src/go/pt-mongodb-index-check/README.rst @@ -0,0 +1,72 @@ +.. _pt-mongodb-index-check: + +================================= +:program:`pt-mongodb-index-check` +================================= + +Performs checks on MongoDB indexes. + +Checks available +================ + +Duplicated indexes +~~~~~~~~~~~~~~~~~~ + +Check for indexes that are the prefix of other indexes. For example if we have these 2 indexes + +.. code-block:: javascript + + db.getSiblingDB("testdb").test_col.createIndex({"f1": 1, "f2": -1, "f3": 1, "f4": 1}, {"name": "idx_01"}); + db.getSiblingDB("testdb").test_col.createIndex({"f1": 1, "f2": -1, "f3": 1}, {"name": "idx_02"}); + + +The index ``idx_02`` is the prefix of ``idx_01`` because it has the same +keys in the same order so, ``idx_02`` can be dropped. + +Unused indexes. +~~~~~~~~~~~~~~~ + +This check gets the ``$indexstats`` for all indexes and reports those +having ``accesses.ops`` = 0. + +Usage +===== + +Run the program as ``pt-mongodb-index-check [flags]`` + +Available commands +~~~~~~~~~~~~~~~~~~ + +================ ================================== +Command Description +================ ================================== +check-duplicated Run checks for duplicated indexes. +check-unused Run check for unused indexes. +check-all Run all checks +================ ================================== + +Available flags +~~~~~~~~~~~~~~~ + ++----------------------------+----------------------------------------+ +| Flag | Description | ++============================+========================================+ +| –all-databases | Check in all databases excluding | +| | system dbs. | ++----------------------------+----------------------------------------+ +| –databases=DATABASES,… | Comma separated list of databases to | +| | check. | ++----------------------------+----------------------------------------+ +| –all-collections | Check in all collections in the | +| | selected databases. | ++----------------------------+----------------------------------------+ +| –collections=COLLECTIONS,… | Comma separated list of collections to | +| | check. | ++----------------------------+----------------------------------------+ +| –mongodb.uri= | Connection URI | ++----------------------------+----------------------------------------+ +| –json | Show output as JSON | ++----------------------------+----------------------------------------+ +| –version | Show version information | ++----------------------------+----------------------------------------+ +