Skip to content

Commit

Permalink
cmd/atlas/internal/cmdext: detect wrong dev/url usage in hcl loader (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Dec 18, 2024
1 parent a8f7717 commit 45b905e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
12 changes: 9 additions & 3 deletions cmd/atlas/internal/cmdext/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,20 @@ func stateReaderHCL(_ context.Context, config *StateReaderConfig, paths []string
}
}
}
// In case the dev connection is bound to a specific schema, we require the
// desired schema to contain only one schema. Thus, executing diff will be
// In case the dev or client connection is bound to a specific schema, we require
// the desired schema to contain only one schema. Thus, executing diff will be
// done on the content of these two schema and not the whole realm.
if client.URL.Schema != "" && len(realm.Schemas) > 1 {
switch {
case config.Dev != nil && config.Dev.URL.Schema != "" && len(realm.Schemas) > 1:
return nil, fmt.Errorf(
"cannot use HCL with more than 1 schema when dev-url is limited to schema %q",
config.Dev.URL.Schema,
)
case config.Client != nil && config.Client.URL.Schema != "" && len(realm.Schemas) > 1:
return nil, fmt.Errorf(
"cannot use HCL with more than 1 schema when url is limited to schema %q",
config.Client.URL.Schema,
)
}
var (
normalized bool
Expand Down
32 changes: 30 additions & 2 deletions cmd/atlas/internal/cmdext/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestStateReaderHCL(t *testing.T) {
require.NoError(t, os.Mkdir(p, 0755))

// Write an empty schema file into the directory.
os.WriteFile(p+"/schema.hcl", []byte(`
require.NoError(t, os.WriteFile(p+"/schema.hcl", []byte(`
schema "default" {}
table "t1" {
schema = schema.default
Expand All @@ -105,7 +105,7 @@ table "t1" {
column "name" {
type = text
}
}`), 0644)
}`), 0644))

// Read schema file.
u, err := url.Parse("file://" + p + "/schema.hcl")
Expand Down Expand Up @@ -140,4 +140,32 @@ table "t1" {
require.NoError(t, err)
_, exists := r.Schemas[0].Tables[0].Column("name")
require.False(t, exists, "column 'name' should be excluded")

// Mimic multi-schema file.
// Write an empty schema file into the directory.
require.NoError(t, os.WriteFile(p+"/schema.hcl", []byte(`
schema "main" {}
schema "default" {}
table "t1" {
schema = schema.default
column "id" {
type = int
}
column "name" {
type = text
}
}`), 0644))
sr, err = StateReaderHCL(ctx, &StateReaderConfig{
Dev: dev,
URLs: []*url.URL{u},
})
require.EqualError(t, err, `cannot use HCL with more than 1 schema when dev-url is limited to schema "main"`)
require.Nil(t, sr)

sr, err = StateReaderHCL(ctx, &StateReaderConfig{
Client: dev,
URLs: []*url.URL{u},
})
require.EqualError(t, err, `cannot use HCL with more than 1 schema when url is limited to schema "main"`)
require.Nil(t, sr)
}

0 comments on commit 45b905e

Please sign in to comment.