Skip to content

Commit

Permalink
cmd/atlas: support build-tags for entloader (#2050)
Browse files Browse the repository at this point in the history
* cmd/atlas: support `build-tags` for entloader
* chore: cleanup
  • Loading branch information
giautm committed Sep 6, 2023
1 parent ddadbf0 commit 434deda
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cmd/atlas/internal/cmdext/cmdext.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,17 @@ func (EntLoader) tables(u *url.URL) ([]*entschema.Table, error) {
if err != nil {
return nil, err
}
graph, err := entc.LoadGraph(abs, &gen.Config{})
var opts []entc.Option
if tags, ok := u.Query()["build-tags"]; ok {
opts = append(opts, entc.BuildTags(tags...))
}
cfg := &gen.Config{}
for _, opt := range opts {
if err = opt(cfg); err != nil {
return nil, err
}
}
graph, err := entc.LoadGraph(abs, cfg)
if err != nil {
return nil, fmt.Errorf("loading schema: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/atlas/internal/cmdext/cmdext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,21 @@ func TestEntLoader_LoadState(t *testing.T) {
ctx := context.Background()
drv, err := sqlclient.Open(ctx, "sqlite://test?mode=memory&_fk=1")
require.NoError(t, err)
u, err := url.Parse("ent://../migrate/ent/schema")
u, err := url.Parse("ent://./testdata/schema")
require.NoError(t, err)
l, ok := cmdext.States.Loader("ent")
require.True(t, ok)
state, err := l.LoadState(ctx, &cmdext.StateReaderConfig{
Dev: drv,
URLs: []*url.URL{u},
})
require.ErrorContains(t, err, "build constraints exclude all Go files in")
u, err = url.Parse("ent://./testdata/schema?build-tags=foo&build-tags=testdata")
require.NoError(t, err)
state, err = l.LoadState(ctx, &cmdext.StateReaderConfig{
Dev: drv,
URLs: []*url.URL{u},
})
require.NoError(t, err)
realm, err := state.ReadState(ctx)
require.NoError(t, err)
Expand Down
65 changes: 65 additions & 0 deletions cmd/atlas/internal/cmdext/testdata/schema/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build testdata
// +build testdata

// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schema

import (
"time"

"ariga.io/atlas/sql/migrate"

"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
)

// DefaultRevisionSchema is the default schema for storing revisions table.
const DefaultRevisionSchema = "atlas_schema_revisions"

// Revision holds the schema definition for the Revision entity.
type Revision struct {
ent.Schema
}

// Fields of the Revision.
func (Revision) Fields() []ent.Field {
return []ent.Field{
field.String("id").
StorageKey("version").
Immutable(),
field.String("description").
Immutable(),
field.Uint("type").
GoType(migrate.RevisionType(0)).
Default(uint(migrate.RevisionTypeExecute)),
field.Int("applied").
NonNegative().
Default(0),
field.Int("total").
NonNegative().
Default(0),
field.Time("executed_at").
Immutable(),
field.Int64("execution_time").
GoType(time.Duration(0)),
field.Text("error").
Optional(),
field.Text("error_stmt").
Optional(),
field.String("hash"),
field.Strings("partial_hashes").
Optional(),
field.String("operator_version"),
}
}

// Annotations of the Revision.
func (Revision) Annotations() []schema.Annotation {
return []schema.Annotation{
entsql.Annotation{Table: DefaultRevisionSchema},
}
}

0 comments on commit 434deda

Please sign in to comment.