Skip to content

Commit

Permalink
target table name default to same as source (as long as hosts/DSNs ar…
Browse files Browse the repository at this point in the history
…e different)
  • Loading branch information
NickDubelman committed May 16, 2024
1 parent 6205c10 commit 28bcf62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
24 changes: 23 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,33 @@ func loadConfig(fileContents string) (Config, error) {
job.PrimaryKeys = []string{job.PrimaryKey}
}

// If host is given, check to see if there is an entry in the credential map
// Impose default credentials on the source and target tables
job.Source = imposeDefaultCredentials(job.Source, config.Defaults)

sourceHasDSN := job.Source.DSN != ""
sourceHasHost := job.Source.Host != ""

for j := range job.Targets {
job.Targets[j] = imposeDefaultCredentials(job.Targets[j], config.Defaults)

targetHasDSN := job.Targets[j].DSN != ""
targetHasHost := job.Targets[j].Host != ""
hasDifferentDSN := job.Source.DSN != job.Targets[j].DSN
hasDifferentHost := job.Source.Host != job.Targets[j].Host

if sourceHasDSN && targetHasDSN && hasDifferentDSN {
// If the source and target both have DSNs and they are different, default target
// table to same as source table
if job.Targets[j].Table == "" {
job.Targets[j].Table = job.Source.Table
}
} else if sourceHasHost && targetHasHost && hasDifferentHost {
// If the source and target both have hosts and they are different, default target
// table to same as source table
if job.Targets[j].Table == "" {
job.Targets[j].Table = job.Source.Table
}
}
}

config.Jobs[jobName] = job // Update the map
Expand Down
23 changes: 21 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func TestLoadConfig(t *testing.T) {
table: users
targets:
- host: host2
table: users
- host: host3
dsn: host3_dsn
table: users
Expand All @@ -124,9 +123,17 @@ func TestLoadConfig(t *testing.T) {
table: pets
- port: 1234
table: pets
posts:
columns: [id, title, body]
source:
dsn: posts_dsn
table: posts
targets:
- dsn: posts_dsn2
`)
require.NoError(t, err)
require.Len(t, cfg.Jobs, 2)
require.Len(t, cfg.Jobs, 3)

usersJobName := "users"
require.Contains(t, cfg.Jobs, usersJobName)
Expand All @@ -136,6 +143,10 @@ func TestLoadConfig(t *testing.T) {
require.Contains(t, cfg.Jobs, petsJobName)
petsJob := cfg.Jobs[petsJobName]

postsJobName := "posts"
require.Contains(t, cfg.Jobs, postsJobName)
postsJob := cfg.Jobs[postsJobName]

assert.Equal(t, "host1:3369", usersJob.Source.Label)
assert.Equal(t, "mysql", usersJob.Source.Driver)
assert.Equal(t, "user1", usersJob.Source.User)
Expand All @@ -146,6 +157,8 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, "host2_dsn", usersJob.Targets[0].Label)
assert.Equal(t, "postgres", usersJob.Targets[0].Driver)
assert.Equal(t, "host2_dsn", usersJob.Targets[0].DSN)
// Make sure target's table defaults to source's table when hosts are different
assert.Equal(t, "users", usersJob.Targets[0].Table)

assert.Equal(t, "host3_label", usersJob.Targets[1].Label)
assert.Equal(t, "sqlite3", usersJob.Targets[1].Driver)
Expand All @@ -169,6 +182,12 @@ func TestLoadConfig(t *testing.T) {

assert.Equal(t, "host4", petsJob.Targets[2].Label)
assert.Equal(t, ":1234", petsJob.Targets[3].Label)

// Make sure target defaults to same table name as source when DSNs are different
assert.Equal(t, "posts_dsn", postsJob.Source.DSN)
assert.Equal(t, "posts", postsJob.Source.Table)
assert.Equal(t, "posts_dsn2", postsJob.Targets[0].DSN)
assert.Equal(t, "posts", postsJob.Targets[0].Table)
})
}

Expand Down

0 comments on commit 28bcf62

Please sign in to comment.