Skip to content

Commit 4dad6e0

Browse files
authored
New command: check (#901)
#### Description This PR adds a new dummy command called `check`. It will run the preflight checks before `run`/`snapshot`. The actual checks will come in multiple follow-up PRs. When invoking `check` all checks are executed. If you want to run a only a portion of the checks, you will be able to provide flags to filter. ``` $ pgstream check --connectivity ``` ``` $ pgstream check --replication ``` ``` $ pgstream check --access ``` ``` $ pgstream check --connectivity --replication ``` ##### Help ``` $ pgstream check --help Runs pre-migration checks to catch blocking issues before snapshot/run Usage: pgstream check [flags] Examples: pgstream check -c pg2pg.env pgstream check -c pg2pg.yaml --json Flags: -h, --help help for check --json Output the check report in JSON format --postgres-url string Source postgres URL to run checks against --target-url string Target URL to run checks against ``` ##### Example outputs ``` # Run against a config file $ pgstream check -c pg2pg.yaml SUCCESS no checks to run # Run with env-var-driven config $ PGSTREAM_POSTGRES_LISTENER_URL="postgres://user:pw@source:5432/app" pgstream check SUCCESS no checks to run # Run with flags $ pgstream check \ --postgres-url "postgres://user:pw@source:5432/app" \ --target-url "postgres://user:pw@target:5432/app" SUCCESS no checks to run # JSON output (no findings to report yet) $ pgstream check -c pg2pg.yaml --json SUCCESS no checks to run ``` #### Related Issue(s) - Related to #897 #### Type of Change Please select the relevant option(s): - [ ] 🐛 Bug fix (non-breaking change that fixes an issue) - [x] ✨ New feature (non-breaking change that adds functionality) - [ ] 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 📚 Documentation update - [ ] 🔧 Refactoring (no functional changes) - [ ] ⚡ Performance improvement - [ ] 🧪 Test coverage improvement - [ ] 🔨 Build/CI changes - [ ] 🧹 Code cleanup #### Testing - [ ] Unit tests added/updated - [ ] Integration tests added/updated - [x] Manual testing performed - [x] All existing tests pass #### Checklist - [x] Code follows project style guidelines - [x] Self-review completed - [x] Code is well-commented - [x] Documentation updated where necessary #### Additional Notes <!-- Any context or special instructions for reviewers -->
1 parent 6f0a550 commit 4dad6e0

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

cli-definition.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
{
22
"name": "pgstream",
33
"commands": [
4+
{
5+
"name": "check",
6+
"short": "Runs pre-migration checks to catch blocking issues before snapshot/run",
7+
"use": "check",
8+
"example": "\n\tpgstream check -c pg2pg.env\n\tpgstream check -c pg2pg.yaml --json\n\t",
9+
"flags": [
10+
{
11+
"name": "json",
12+
"description": "Output the check report in JSON format",
13+
"default": "false"
14+
},
15+
{
16+
"name": "postgres-url",
17+
"description": "Source postgres URL to run checks against",
18+
"default": ""
19+
},
20+
{
21+
"name": "target-url",
22+
"description": "Target URL to run checks against",
23+
"default": ""
24+
}
25+
],
26+
"subcommands": [],
27+
"args": []
28+
},
429
{
530
"name": "destroy",
631
"short": "It destroys any pgstream setup, removing the replication slot and all the relevant tables/functions/triggers, along with the internal pgstream schema",

cmd/check_cmd.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package cmd
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/pterm/pterm"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
12+
"github.com/xataio/pgstream/cmd/config"
13+
)
14+
15+
var checkCmd = &cobra.Command{
16+
Use: "check",
17+
Short: "Runs pre-migration checks to catch blocking issues before snapshot/run",
18+
PreRunE: checkFlagBinding,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
sp, _ := pterm.DefaultSpinner.WithText("running pgstream checks...").Start()
21+
22+
err := func() error {
23+
streamConfig, err := config.ParseStreamConfig()
24+
if err != nil {
25+
return fmt.Errorf("parsing stream config: %w", err)
26+
}
27+
_ = streamConfig
28+
29+
// TODO: run checks. See https://github.com/xataio/pgstream/issues/897 for the
30+
// list of checks to implement.
31+
32+
sp.Success("no checks to run")
33+
return nil
34+
}()
35+
if err != nil {
36+
sp.Fail(err.Error())
37+
}
38+
39+
return err
40+
},
41+
Example: `
42+
pgstream check -c pg2pg.env
43+
pgstream check -c pg2pg.yaml --json
44+
`,
45+
}
46+
47+
func checkFlagBinding(cmd *cobra.Command, _ []string) error {
48+
// to be able to overwrite configuration with flags when yaml config file is
49+
// provided
50+
viper.BindPFlag("source.postgres.url", cmd.Flags().Lookup("postgres-url"))
51+
viper.BindPFlag("target.postgres.url", cmd.Flags().Lookup("target-url"))
52+
53+
// to be able to overwrite configuration with flags when env config file is
54+
// provided or when no configuration is provided
55+
viper.BindPFlag("PGSTREAM_POSTGRES_LISTENER_URL", cmd.Flags().Lookup("postgres-url"))
56+
viper.BindPFlag("PGSTREAM_POSTGRES_WRITER_TARGET_URL", cmd.Flags().Lookup("target-url"))
57+
return nil
58+
}

cmd/root_cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ func Prepare() *cobra.Command {
115115
validateSchemaCmd.Flags().Bool("json", false, "Output the validation status in JSON format")
116116
validateCmd.AddCommand(validateSchemaCmd)
117117

118+
// check cmd
119+
checkCmd.Flags().String("postgres-url", "", "Source postgres URL to run checks against")
120+
checkCmd.Flags().String("target-url", "", "Target URL to run checks against")
121+
checkCmd.Flags().Bool("json", false, "Output the check report in JSON format")
122+
118123
// Flag binding for root cmd
119124
rootFlagBinding(rootCmd)
120125

@@ -126,6 +131,7 @@ func Prepare() *cobra.Command {
126131
rootCmd.AddCommand(snapshotCmd)
127132
rootCmd.AddCommand(statusCmd)
128133
rootCmd.AddCommand(validateCmd)
134+
rootCmd.AddCommand(checkCmd)
129135
return rootCmd
130136
}
131137

0 commit comments

Comments
 (0)