Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/atlas/internal/cmdext: detect wrong dev/url usage in hcl loader #3271

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
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)
}
Loading