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: support build-tags for entloader #2050

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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{})
opts := []entc.Option{}
if tags, ok := u.Query()["build-tags"]; ok {
giautm marked this conversation as resolved.
Show resolved Hide resolved
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},
}
}