Permalink
Browse files

init, add-environment: persist more options if specified on CLI

Both `skeema init` and `skeema add-environment` now persist --connect-options
to the host-level .skeema file, if it was specified on the CLI.

Additionally, `skeema add-environment` now persists --ignore-schema and
--ignore-table to the host-level .skeema file, matching the existing behavior
of `skeema init` for these options.
  • Loading branch information...
evanelias committed Oct 30, 2018
1 parent 2718d22 commit 05d2622a2d32eb11e06127a4302c47d57a4b4dd2
Showing with 38 additions and 15 deletions.
  1. +4 −2 cmd_add_env.go
  2. +4 −8 cmd_init.go
  3. +30 −5 skeema_cmd_test.go
View
@@ -85,8 +85,10 @@ func AddEnvHandler(cfg *mybase.Config) error {
if flavor := inst.Flavor(); flavor != tengo.FlavorUnknown {
dir.OptionFile.SetOptionValue(environment, "flavor", flavor.String())
}
if cfg.OnCLI("user") {
dir.OptionFile.SetOptionValue(environment, "user", cfg.Get("user"))
for _, persistOpt := range []string{"user", "ignore-schema", "ignore-table", "connect-options"} {
if cfg.OnCLI(persistOpt) {
dir.OptionFile.SetOptionValue(environment, persistOpt, cfg.Get(persistOpt))
}
}
// Write the option file
View
@@ -108,17 +108,13 @@ func InitHandler(cfg *mybase.Config) error {
} else {
hostOptionFile.SetOptionValue(environment, "port", strconv.Itoa(inst.Port))
}
if cfg.OnCLI("user") {
hostOptionFile.SetOptionValue(environment, "user", cfg.Get("user"))
}
if flavor := inst.Flavor(); flavor != tengo.FlavorUnknown {
hostOptionFile.SetOptionValue(environment, "flavor", flavor.String())
}
if cfg.OnCLI("ignore-schema") {
hostOptionFile.SetOptionValue(environment, "ignore-schema", cfg.Get("ignore-schema"))
}
if cfg.OnCLI("ignore-table") {
hostOptionFile.SetOptionValue(environment, "ignore-table", cfg.Get("ignore-table"))
for _, persistOpt := range []string{"user", "ignore-schema", "ignore-table", "connect-options"} {
if cfg.OnCLI(persistOpt) {
hostOptionFile.SetOptionValue(environment, persistOpt, cfg.Get(persistOpt))
}
}
if !separateSchemaSubdir {
// schema name is placed outside of any named section/environment since the
View
@@ -49,7 +49,7 @@ func (s SkeemaIntegrationSuite) TestInitHandler(t *testing.T) {
// among other things, changes the default charset and collation for the
// schema in question.
s.sourceSQL(t, "push1.sql")
cfg = s.handleCommand(t, CodeSuccess, ".", "skeema init --dir combined -h %s -P %d --schema product", s.d.Instance.Host, s.d.Instance.Port)
cfg = s.handleCommand(t, CodeSuccess, ".", "skeema init --dir combined -h %s -P %d --schema product ", s.d.Instance.Host, s.d.Instance.Port)
dir, err := fs.ParseDir("combined", cfg)
if err != nil {
t.Fatalf("Unexpected error from ParseDir: %s", err)
@@ -68,14 +68,24 @@ func (s SkeemaIntegrationSuite) TestInitHandler(t *testing.T) {
t.Errorf("Expected %s to have *.sql files, but it does not", dir)
}
// Test successful init without a --dir
// Test successful init without a --dir. Also test persistence of --connect-options.
expectDir := fmt.Sprintf("%s:%d", s.d.Instance.Host, s.d.Instance.Port)
if _, err = os.Stat(expectDir); err == nil {
t.Fatalf("Expected dir %s to not exist yet, but it does", expectDir)
}
s.handleCommand(t, CodeSuccess, ".", "skeema init -h %s -P %d", s.d.Instance.Host, s.d.Instance.Port)
if _, err = os.Stat(expectDir); err != nil {
t.Fatalf("Expected dir %s to exist now, but it does not", expectDir)
cfg = s.handleCommand(t, CodeSuccess, ".", "skeema init -h %s -P %d --connect-options='wait_timeout=3'", s.d.Instance.Host, s.d.Instance.Port)
if dir, err = fs.ParseDir(expectDir, cfg); err != nil {
t.Fatalf("Unexpected error from ParseDir: %s", err)
}
for _, option := range []string{"host", "port", "connect-options"} {
if _, setsOption := dir.OptionFile.OptionValue(option); !setsOption {
t.Errorf("Expected host-level .skeema to contain %s, but it does not", option)
}
}
for _, option := range []string{"schema", "default-character-set", "default-collation"} {
if _, setsOption := dir.OptionFile.OptionValue(option); setsOption {
t.Errorf("Expected host-level .skeema to NOT contain %s, but it does", option)
}
}
// init should fail if a parent dir has an invalid .skeema file
@@ -127,6 +137,7 @@ func (s SkeemaIntegrationSuite) TestAddEnvHandler(t *testing.T) {
file := getOptionFile(t, "mydb", cfg)
origFile.SetOptionValue("staging", "host", "my.staging.invalid")
origFile.SetOptionValue("staging", "port", "3306")
origFile.SetOptionValue("staging", "connect-options", "timeout=10ms")
if !origFile.SameContents(file) {
t.Fatalf("File contents of %s do not match expectation", file.Path())
}
@@ -137,6 +148,7 @@ func (s SkeemaIntegrationSuite) TestAddEnvHandler(t *testing.T) {
origFile.SetOptionValue("ci", "host", "my.ci.invalid")
origFile.SetOptionValue("ci", "port", "3307")
origFile.SetOptionValue("ci", "user", "foobar")
origFile.SetOptionValue("ci", "connect-options", "timeout=10ms")
if !origFile.SameContents(file) {
t.Fatalf("File contents of %s do not match expectation", file.Path())
}
@@ -149,6 +161,19 @@ func (s SkeemaIntegrationSuite) TestAddEnvHandler(t *testing.T) {
if !origFile.SameContents(file) {
t.Fatalf("File contents of %s do not match expectation", file.Path())
}
// valid instance should work properly and even populate flavor. Also confirm
// persistence of ignore-schema and ignore-table.
cfg = s.handleCommand(t, CodeSuccess, "mydb", "skeema add-environment --ignore-schema='^test' --ignore-table='^_' --host %s:%d cloud", s.d.Instance.Host, s.d.Instance.Port)
file = getOptionFile(t, "mydb", cfg)
origFile.SetOptionValue("cloud", "host", s.d.Instance.Host)
origFile.SetOptionValue("cloud", "port", fmt.Sprintf("%d", s.d.Instance.Port))
origFile.SetOptionValue("cloud", "ignore-schema", "^test")
origFile.SetOptionValue("cloud", "ignore-table", "^_")
origFile.SetOptionValue("cloud", "flavor", s.d.Flavor().String())
if !origFile.SameContents(file) {
t.Fatalf("File contents of %s do not match expectation", file.Path())
}
}
func (s SkeemaIntegrationSuite) TestPullHandler(t *testing.T) {

0 comments on commit 05d2622

Please sign in to comment.