diff --git a/cmd/atlas/internal/cmdext/reader.go b/cmd/atlas/internal/cmdext/reader.go index 847bdf2a51b..745a09b8d2f 100644 --- a/cmd/atlas/internal/cmdext/reader.go +++ b/cmd/atlas/internal/cmdext/reader.go @@ -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 diff --git a/cmd/atlas/internal/cmdext/reader_test.go b/cmd/atlas/internal/cmdext/reader_test.go index 1f8f7fcbcb4..5f437b9aa11 100644 --- a/cmd/atlas/internal/cmdext/reader_test.go +++ b/cmd/atlas/internal/cmdext/reader_test.go @@ -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 @@ -105,7 +105,7 @@ table "t1" { column "name" { type = text } -}`), 0644) +}`), 0644)) // Read schema file. u, err := url.Parse("file://" + p + "/schema.hcl") @@ -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) }