Skip to content

Commit

Permalink
address comments + new UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
venkat-iblox committed Oct 4, 2024
1 parent 56064c4 commit 7c91e46
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/migrate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ var (

// goto command flags
flagDirty = pflag.Bool("force-dirty-handling", false, "force the handling of dirty database state")
flagMountPath = pflag.String("cache-dir", "", "path to the mounted volume which is used to copy the migration files")
flagMountPath = pflag.String("cache-dir", "", "path to the cache-dir which is used to copy the migration files")
)
2 changes: 1 addition & 1 deletion internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoU
log.fatal("error: cache-dir must be specified when force-dirty-handling is set")
}

if err = migrater.WithDirtyStateHandler(sourcePtr, destPath, handleDirty); err != nil {
if err = migrater.WithDirtyStateConfig(sourcePtr, destPath, handleDirty); err != nil {
log.fatalErr(err)
}
}
Expand Down
20 changes: 8 additions & 12 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,31 +218,27 @@ func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseNa
return m, nil
}

func (m *Migrate) WithDirtyStateHandler(srcPath, destPath string, isDirty bool) error {
parser := func(path string) (string, string, error) {
var scheme, p string
func (m *Migrate) WithDirtyStateConfig(srcPath, destPath string, isDirty bool) error {
parsePath := func(path string) (string, string, error) {
uri, err := url.Parse(path)
if err != nil {
return "", "", err
}
scheme = uri.Scheme
p = uri.Path
// if no scheme is provided, assume it's a file path
switch scheme {
case "", "file":
scheme := uri.Scheme
if scheme == "" || scheme == "file" {
scheme = "file://"
default:
} else if scheme != "file" {
return "", "", fmt.Errorf("unsupported scheme: %s", scheme)
}
return scheme, p, nil
return scheme, uri.Path, nil
}

sScheme, sPath, err := parser(srcPath)
sScheme, sPath, err := parsePath(srcPath)
if err != nil {
return err
}

dScheme, dPath, err := parser(destPath)
dScheme, dPath, err := parsePath(destPath)
if err != nil {
return err
}
Expand Down
95 changes: 94 additions & 1 deletion migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ func TestCopyFiles(t *testing.T) {
copiedFiles: []string{"1_name.up.sql", "2_name.up.sql", "3_name.up.sql", "4_name.up.sql"},
},
{
emptyDestPath: true,
emptyDestPath: true, // copyFiles should not do anything
},
}

Expand Down Expand Up @@ -1673,6 +1673,99 @@ func TestCopyFiles(t *testing.T) {
}
}

func TestWithDirtyStateConfig(t *testing.T) {
tests := []struct {
name string
srcPath string
destPath string
isDirty bool
wantErr bool
wantConf *dirtyStateConfig
}{
{
name: "Valid file paths",
srcPath: "file:///src/path",
destPath: "file:///dest/path",
isDirty: true,
wantErr: false,
wantConf: &dirtyStateConfig{
srcScheme: "file://",
destScheme: "file://",
srcPath: "/src/path",
destPath: "/dest/path",
enable: true,
},
},
{
name: "Invalid source scheme",
srcPath: "s3:///src/path",
destPath: "file:///dest/path",
isDirty: true,
wantErr: true,
},
{
name: "Invalid destination scheme",
srcPath: "file:///src/path",
destPath: "s3:///dest/path",
isDirty: true,
wantErr: true,
},
{
name: "Empty source scheme",
srcPath: "/src/path",
destPath: "file:///dest/path",
isDirty: true,
wantErr: false,
wantConf: &dirtyStateConfig{
srcScheme: "file://",
destScheme: "file://",
srcPath: "/src/path",
destPath: "/dest/path",
enable: true,
},
},
{
name: "Empty destination scheme",
srcPath: "file:///src/path",
destPath: "/dest/path",
isDirty: true,
wantErr: false,
wantConf: &dirtyStateConfig{
srcScheme: "file://",
destScheme: "file://",
srcPath: "/src/path",
destPath: "/dest/path",
enable: true,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Migrate{}
err := m.WithDirtyStateConfig(tt.srcPath, tt.destPath, tt.isDirty)
if (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !compareDirtyStateConfig(m.dirtyStateConf, tt.wantConf) {
t.Errorf("dirtyStateConf = %v, want %v", m.dirtyStateConf, tt.wantConf)
}
})
}
}

func compareDirtyStateConfig(a, b *dirtyStateConfig) bool {
if a == nil || b == nil {
return a == b
}
return a.srcScheme == b.srcScheme &&
a.srcPath == b.srcPath &&
a.destScheme == b.destScheme &&
a.destPath == b.destPath &&
a.enable == b.enable
}

/*
diff returns an array containing the elements in Array A and not in B
*/
Expand Down

0 comments on commit 7c91e46

Please sign in to comment.