Ensure SchemaConfig is set in sqlQuery's context (#1203)

This commit is contained in:
Marwan Sulaiman
2021-01-24 08:15:23 -05:00
committed by Ariel Mashraki
parent aeb1ccc571
commit bdd8f2f624
6 changed files with 20 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@@ -130,6 +130,8 @@ func NewSchemaConfigContext(parent context.Context, config SchemaConfig) context
{{- $receiver := receiver $builder }}
{{- if $.FeatureEnabled "sql/schemaconfig" }}
t1.Schema({{ $receiver }}.schemaConfig.{{ $.Name }})
ctx = internal.NewSchemaConfigContext(ctx, {{ $receiver }}.schemaConfig)
selector.WithContext(ctx)
{{- end }}
{{- end -}}

View File

@@ -511,6 +511,8 @@ func (gq *GroupQuery) sqlQuery(ctx context.Context) *sql.Selector {
selector.Select(selector.Columns(group.Columns...)...)
}
t1.Schema(gq.schemaConfig.Group)
ctx = internal.NewSchemaConfigContext(ctx, gq.schemaConfig)
selector.WithContext(ctx)
for _, p := range gq.predicates {
p(selector)
}

View File

@@ -478,6 +478,8 @@ func (pq *PetQuery) sqlQuery(ctx context.Context) *sql.Selector {
selector.Select(selector.Columns(pet.Columns...)...)
}
t1.Schema(pq.schemaConfig.Pet)
ctx = internal.NewSchemaConfigContext(ctx, pq.schemaConfig)
selector.WithContext(ctx)
for _, p := range pq.predicates {
p(selector)
}

View File

@@ -580,6 +580,8 @@ func (uq *UserQuery) sqlQuery(ctx context.Context) *sql.Selector {
selector.Select(selector.Columns(user.Columns...)...)
}
t1.Schema(uq.schemaConfig.User)
ctx = internal.NewSchemaConfigContext(ctx, uq.schemaConfig)
selector.WithContext(ctx)
for _, p := range uq.predicates {
p(selector)
}

View File

@@ -12,7 +12,9 @@ import (
"github.com/facebook/ent/dialect"
"github.com/facebook/ent/dialect/sql"
"github.com/facebook/ent/entc/integration/multischema/ent"
"github.com/facebook/ent/entc/integration/multischema/ent/group"
"github.com/facebook/ent/entc/integration/multischema/ent/migrate"
"github.com/facebook/ent/entc/integration/multischema/ent/user"
"github.com/stretchr/testify/require"
_ "github.com/go-sql-driver/mysql"
@@ -42,12 +44,15 @@ func TestMySQL(t *testing.T) {
client.Group.Create().SetName("GitHub"),
client.Group.Create().SetName("GitLab"),
).SaveX(ctx)
client.User.Create().AddPets(pedro).AddGroups(groups...).SaveX(ctx)
usr := client.User.Create().AddPets(pedro).AddGroups(groups...).SaveX(ctx)
// id := client.Group.Query().
// Where(group.HasUsersWith(user.ID(usr.ID))).
// OnlyIDX(ctx)
// require.Equal(t, pedro.ID, id)
id := client.Group.Query().
Where(group.HasUsersWith(user.ID(usr.ID))).
Limit(1).
QueryUsers().
QueryPets().
OnlyIDX(ctx)
require.Equal(t, pedro.ID, id)
}
func setupSchema(t *testing.T, drv *sql.Driver) {