Skip to content

Commit

Permalink
cmd/atlas/internal/cmdext: move external_schema to ent repo (#3333)
Browse files Browse the repository at this point in the history
It relies now on non-oss logic
  • Loading branch information
a8m authored Jan 21, 2025
1 parent 709e005 commit 18b7894
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 166 deletions.
54 changes: 0 additions & 54 deletions cmd/atlas/internal/cmdext/cmdext.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,60 +529,6 @@ func SchemaHCL(_ context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block)
}), nil
}

// SchemaExternal is a data source that reads a SQL schema state from external program.
func SchemaExternal(_ context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error) {
var (
args struct {
Program []string `hcl:"program"`
Dir string `hcl:"working_dir,optional"`
Remain hcl.Body `hcl:",remain"`
}
errorf = blockError("data.external_schema", block)
)
if diags := gohcl.DecodeBody(block.Body, ectx, &args); diags.HasErrors() {
return cty.NilVal, errorf("decoding body: %v", diags)
}
attrs, diags := args.Remain.JustAttributes()
if diags.HasErrors() {
return cty.NilVal, errorf("getting attributes: %v", diags)
}
if len(attrs) > 0 {
return cty.NilVal, errorf("unexpected attributes: %v", attrs)
}
if len(args.Program) == 0 {
return cty.NilVal, errorf("program cannot be empty")
}
cmd := exec.Command(args.Program[0], args.Program[1:]...)
if args.Dir != "" {
cmd.Dir = args.Dir
}
out, err := cmd.Output()
if err != nil {
msg := err.Error()
if err1 := (*exec.ExitError)(nil); errors.As(err, &err1) && len(err1.Stderr) > 0 {
msg = string(err1.Stderr)
}
return cty.NilVal, errorf("running program %v: %v", cmd.Path, msg)
}
// Directory files must have an .sql extension to be read by the executor.
// The "schema" word is added to indicate that unlike data-source errors, load error
// comes from the output of the data-source (SQL representation of the state/schema).
dir, err := FilesAsDir(migrate.NewLocalFile(fmt.Sprintf("%s/schema.sql", block.Labels[1]), out))
if err != nil {
return cty.NilVal, errorf("converting output to migration: %v", err)
}
u, err := url.JoinPath("mem://external_schema", block.Labels[1])
if err != nil {
return cty.NilVal, errorf("build url: %v", err)
}
memLoader.states[u] = StateLoaderFunc(func(ctx context.Context, config *StateReaderConfig) (*StateReadCloser, error) {
return stateSchemaSQL(ctx, config, dir)
})
return cty.ObjectVal(map[string]cty.Value{
"url": cty.StringVal(u),
}), nil
}

func blockError(name string, b *hclsyntax.Block) func(string, ...any) error {
return func(format string, args ...any) error {
return fmt.Errorf("%s.%s: %w", name, b.Labels[1], fmt.Errorf(format, args...))
Expand Down
5 changes: 5 additions & 0 deletions cmd/atlas/internal/cmdext/cmdext_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func StateReaderAtlas(context.Context, *StateReaderConfig) (*StateReadCloser, er
return nil, fmt.Errorf("atlas remote state is not supported by this release. See: https://atlasgo.io/getting-started")
}

// SchemaExternal is a data source that for reading external schemas.
func SchemaExternal(context.Context, *hcl.EvalContext, *hclsyntax.Block) (cty.Value, error) {
return cty.Zero, fmt.Errorf("data.external_schema is no longer supported by this release. See: https://atlasgo.io/getting-started")
}

// EntLoader is a StateLoader for loading ent.Schema's as StateReader's.
type EntLoader struct{}

Expand Down
68 changes: 0 additions & 68 deletions cmd/atlas/internal/cmdext/cmdext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,74 +349,6 @@ schema "a8m" {
require.Equal(t, ``, string(buf))
}

func TestExternalSchema(t *testing.T) {
var (
v struct {
Schema string `spec:"schema"`
}
ctx = context.Background()
state = schemahcl.New(cmdext.SpecOptions...)
)
err := state.EvalBytes([]byte(`
data "external_schema" "a8m" {
program = [
"echo",
"CREATE TABLE t(c int);",
]
}
schema = data.external_schema.a8m.url
`), &v, nil)
require.NoError(t, err)
require.NotEmpty(t, v.Schema)
u, err := url.Parse(v.Schema)
require.NoError(t, err)
loader, ok := cmdext.States.Loader(u.Scheme)
require.True(t, ok)
drv, err := sqlclient.Open(ctx, "sqlite://test?mode=memory&_fk=1")
require.NoError(t, err)
sr, err := loader.LoadState(ctx, &cmdext.StateReaderConfig{
Dev: drv,
URLs: []*url.URL{u},
})
require.NoError(t, err)
realm, err := sr.ReadState(ctx)
require.NoError(t, err)
buf, err := drv.MarshalSpec(realm)
require.NoError(t, err)
require.Equal(t, `table "t" {
schema = schema.main
column "c" {
null = true
type = int
}
}
schema "main" {
}
`, string(buf))

// Read state error.
err = state.EvalBytes([]byte(`
data "external_schema" "a8m" {
program = [
"echo",
"CREATE TABLE t(c int);",
"CREATE UNKNOWN",
]
}
schema = data.external_schema.a8m.url
`), &v, nil)
require.NoError(t, err)
loader, ok = cmdext.States.Loader(u.Scheme)
require.True(t, ok)
sr, err = loader.LoadState(ctx, &cmdext.StateReaderConfig{
Dev: drv,
URLs: []*url.URL{u},
})
require.EqualError(t, err, `read state from "a8m/schema.sql": executing statement: "CREATE UNKNOWN": near "UNKNOWN": syntax error`)
}

func TestExternal(t *testing.T) {
var (
v struct {
Expand Down

This file was deleted.

0 comments on commit 18b7894

Please sign in to comment.