Skip to content

Commit

Permalink
internal/controller: prioritize custom config when merging
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao committed Jan 15, 2025
1 parent 9d7fe13 commit 8975f43
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
3 changes: 1 addition & 2 deletions internal/controller/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ func (d *migrationData) render(w io.Writer) error {
}
// Merge the config block if it is set
if d.Config != nil {
mergeBlocks(d.Config.Body(), f.Body())
f = d.Config
mergeBlocks(f.Body(), d.Config.Body())
}
env := searchBlock(f.Body(), hclwrite.NewBlock("env", []string{d.EnvName}))
if env == nil {
Expand Down
17 changes: 9 additions & 8 deletions internal/controller/atlasmigration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,20 +1131,21 @@ env "kubernetes" {
}
var fileContent bytes.Buffer
require.NoError(t, migrate.render(&fileContent))
require.EqualValues(t, `atlas {
require.EqualValues(t, `env "kubernetes" {
migration {
dir = "atlas://my-remote-dir?tag=my-remote-tag"
}
dev = "sqlite://dev/?mode=memory"
url = "sqlite://file2/?mode=memory"
}
atlas {
cloud {
token = "my-token"
url = "https://atlasgo.io/"
project = "my-project"
}
}
env "kubernetes" {
url = "sqlite://file2/?mode=memory"
dev = "sqlite://dev/?mode=memory"
migration {
dir = "atlas://my-remote-dir?tag=my-remote-tag"
}
}`, fileContent.String())
`, fileContent.String())
}

func TestCustomAtlasHCL_BaselineTemplate(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,7 @@ func (d *managedData) render(w io.Writer) error {
}
// Merge config into the atlas.hcl file.
if d.Config != nil {
mergeBlocks(d.Config.Body(), f.Body())
f = d.Config
mergeBlocks(f.Body(), d.Config.Body())
}
if d.EnvName == "" {
return errors.New("env name is not set")
Expand Down
47 changes: 9 additions & 38 deletions internal/controller/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,17 @@ env "kubernetes" {
}
}
}
`),
`),
}
err := data.render(&buf)
require.NoError(t, err)
expected := `
env "kubernetes" {
expected := `env "kubernetes" {
schema {
src = "file://schema.sql"
}
url = "mysql://root:password@localhost:3306/test"
dev = "mysql://root:password@localhost:3306/dev"
schemas = ["foo", "bar"]
diff {
concurrent_index {
create = true
Expand All @@ -573,42 +578,8 @@ env "kubernetes" {
error = true
}
}
dev = "mysql://root:password@localhost:3306/dev"
schemas = ["foo", "bar"]
url = "mysql://root:password@localhost:3306/test"
schema {
src = "file://schema.sql"
}
}
`
require.EqualValues(t, expected, buf.String())
}

func TestCustomAtlasHCL_UnnamedBlock(t *testing.T) {
var buf bytes.Buffer
data := &managedData{
EnvName: defaultEnvName,
URL: must(url.Parse("mysql://root:password@localhost:3306/test")),
Desired: must(url.Parse("file://schema.sql")),
Schemas: []string{"foo", "bar"},
Config: mustParseHCL(`
env {
name = atlas.env
dev = "mysql://root:password@localhost:3306/dev"
}`),
}
err := data.render(&buf)
require.NoError(t, err)
expected := `
env {
name = atlas.env
dev = "mysql://root:password@localhost:3306/dev"
schemas = ["foo", "bar"]
url = "mysql://root:password@localhost:3306/test"
schema {
src = "file://schema.sql"
}
}`
`
require.EqualValues(t, expected, buf.String())
}

Expand Down

0 comments on commit 8975f43

Please sign in to comment.