Skip to content
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: 11 additions & 2 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SchemaCmd struct {
target string
targetProfile string
filePrefix string // TODO: move filePrefix to global flags
dry_run bool
}

// Name returns the name of operation.
Expand Down Expand Up @@ -67,6 +68,7 @@ func (cmd *SchemaCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.target, "target", "Spanner", "Specifies the target DB, defaults to Spanner (accepted values: `Spanner`)")
f.StringVar(&cmd.targetProfile, "target-profile", "", "Flag for specifying connection profile for target database e.g., \"dialect=postgresql\"")
f.StringVar(&cmd.filePrefix, "prefix", "", "File prefix for generated files")
f.BoolVar(&cmd.dry_run, "dry-run", false, "Flag for specifying dry run mode for a command")
}

func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
Expand All @@ -78,7 +80,8 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa
fmt.Printf("FATAL error: %v\n", err)
}
}()

internal.DryRunInit(cmd.dry_run)
fmt.Printf("Dry run mode: %v\n", cmd.dry_run)
sourceProfile, err := profiles.NewSourceProfile(cmd.sourceProfile, cmd.source)
if err != nil {
return subcommands.ExitUsageError
Expand All @@ -98,7 +101,14 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa
if sourceProfile.Ty == profiles.SourceProfileTypeFile && (sourceProfile.File.Format == "" || sourceProfile.File.Format == "dump") {
dumpFilePath = sourceProfile.File.Path
}

if internal.DryRun() {
fmt.Println("Command successfully validated.")
return subcommands.ExitSuccess
}

ioHelper := utils.NewIOStreams(sourceProfile.Driver, dumpFilePath)

if ioHelper.SeekableIn != nil {
defer ioHelper.In.Close()
}
Expand All @@ -112,7 +122,6 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa
}
cmd.filePrefix = dbName + "."
}

schemaConversionStartTime := time.Now()
var conv *internal.Conv
conv, err = conversion.SchemaConv(sourceProfile, targetProfile, &ioHelper)
Expand Down
27 changes: 27 additions & 0 deletions internal/dry_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal

var enabled = false

// DryRun returns true if dry run mode is enabled.
func DryRun() bool {
return enabled
}

// DryRunInit determines whether dry run mode is enabled.
// Generally there should be one call to DryRunInit at startup.
func DryRunInit(b bool) {
enabled = b
}