Skip to content

Commit 45b905e

Browse files
authored
cmd/atlas/internal/cmdext: detect wrong dev/url usage in hcl loader (#3271)
1 parent a8f7717 commit 45b905e

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

cmd/atlas/internal/cmdext/reader.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,20 @@ func stateReaderHCL(_ context.Context, config *StateReaderConfig, paths []string
215215
}
216216
}
217217
}
218-
// In case the dev connection is bound to a specific schema, we require the
219-
// desired schema to contain only one schema. Thus, executing diff will be
218+
// In case the dev or client connection is bound to a specific schema, we require
219+
// the desired schema to contain only one schema. Thus, executing diff will be
220220
// done on the content of these two schema and not the whole realm.
221-
if client.URL.Schema != "" && len(realm.Schemas) > 1 {
221+
switch {
222+
case config.Dev != nil && config.Dev.URL.Schema != "" && len(realm.Schemas) > 1:
222223
return nil, fmt.Errorf(
223224
"cannot use HCL with more than 1 schema when dev-url is limited to schema %q",
224225
config.Dev.URL.Schema,
225226
)
227+
case config.Client != nil && config.Client.URL.Schema != "" && len(realm.Schemas) > 1:
228+
return nil, fmt.Errorf(
229+
"cannot use HCL with more than 1 schema when url is limited to schema %q",
230+
config.Client.URL.Schema,
231+
)
226232
}
227233
var (
228234
normalized bool

cmd/atlas/internal/cmdext/reader_test.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestStateReaderHCL(t *testing.T) {
9595
require.NoError(t, os.Mkdir(p, 0755))
9696

9797
// Write an empty schema file into the directory.
98-
os.WriteFile(p+"/schema.hcl", []byte(`
98+
require.NoError(t, os.WriteFile(p+"/schema.hcl", []byte(`
9999
schema "default" {}
100100
table "t1" {
101101
schema = schema.default
@@ -105,7 +105,7 @@ table "t1" {
105105
column "name" {
106106
type = text
107107
}
108-
}`), 0644)
108+
}`), 0644))
109109

110110
// Read schema file.
111111
u, err := url.Parse("file://" + p + "/schema.hcl")
@@ -140,4 +140,32 @@ table "t1" {
140140
require.NoError(t, err)
141141
_, exists := r.Schemas[0].Tables[0].Column("name")
142142
require.False(t, exists, "column 'name' should be excluded")
143+
144+
// Mimic multi-schema file.
145+
// Write an empty schema file into the directory.
146+
require.NoError(t, os.WriteFile(p+"/schema.hcl", []byte(`
147+
schema "main" {}
148+
schema "default" {}
149+
table "t1" {
150+
schema = schema.default
151+
column "id" {
152+
type = int
153+
}
154+
column "name" {
155+
type = text
156+
}
157+
}`), 0644))
158+
sr, err = StateReaderHCL(ctx, &StateReaderConfig{
159+
Dev: dev,
160+
URLs: []*url.URL{u},
161+
})
162+
require.EqualError(t, err, `cannot use HCL with more than 1 schema when dev-url is limited to schema "main"`)
163+
require.Nil(t, sr)
164+
165+
sr, err = StateReaderHCL(ctx, &StateReaderConfig{
166+
Client: dev,
167+
URLs: []*url.URL{u},
168+
})
169+
require.EqualError(t, err, `cannot use HCL with more than 1 schema when url is limited to schema "main"`)
170+
require.Nil(t, sr)
143171
}

0 commit comments

Comments
 (0)