From 36a1063c7e29f4706beb2cbe27e1f01ec22ed117 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Tue, 22 Feb 2022 14:23:11 +0200 Subject: [PATCH] entc/integration/migrate: add FULLTEXT usage example --- dialect/sql/schema/mysql.go | 6 +++--- entc/integration/migrate/entv2/migrate/schema.go | 10 ++++++++++ entc/integration/migrate/entv2/schema/user.go | 8 ++++---- entc/integration/migrate/migrate_test.go | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dialect/sql/schema/mysql.go b/dialect/sql/schema/mysql.go index 00b606f05..f7a3f8a56 100644 --- a/dialect/sql/schema/mysql.go +++ b/dialect/sql/schema/mysql.go @@ -962,11 +962,11 @@ func indexType(idx *Index, d string) (string, bool) { if ant == nil { return "", false } - if ant.Type != "" { - return idx.Annotation.Type, true - } if ant.Types != nil && ant.Types[d] != "" { return ant.Types[d], true } + if ant.Type != "" { + return ant.Type, true + } return "", false } diff --git a/entc/integration/migrate/entv2/migrate/schema.go b/entc/integration/migrate/entv2/migrate/schema.go index deb91d074..ec95e6875 100644 --- a/entc/integration/migrate/entv2/migrate/schema.go +++ b/entc/integration/migrate/entv2/migrate/schema.go @@ -170,6 +170,16 @@ var ( Desc: true, }, }, + { + Name: "user_nickname", + Unique: false, + Columns: []*schema.Column{UsersColumns[6]}, + Annotation: &entsql.IndexAnnotation{ + Types: map[string]string{ + "mysql": "FULLTEXT", + }, + }, + }, }, } // FriendsColumns holds the columns for the "friends" table. diff --git a/entc/integration/migrate/entv2/schema/user.go b/entc/integration/migrate/entv2/schema/user.go index 088fb5a53..4362e432b 100644 --- a/entc/integration/migrate/entv2/schema/user.go +++ b/entc/integration/migrate/entv2/schema/user.go @@ -7,9 +7,8 @@ package schema import ( "time" - "entgo.io/ent/dialect" - "entgo.io/ent" + "entgo.io/ent/dialect" "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" @@ -128,11 +127,12 @@ func (User) Indexes() []ent.Index { Unique(), index.Fields("age"). Annotations(entsql.Desc()), + // Enable FULLTEXT search on "nickname" + // field only in MySQL. index.Fields("nickname"). Annotations( entsql.IndexTypes(map[string]string{ - dialect.MySQL: "FULLTEXT", - dialect.Postgres: "GIN", + dialect.MySQL: "FULLTEXT", }), ), } diff --git a/entc/integration/migrate/migrate_test.go b/entc/integration/migrate/migrate_test.go index a8baea10e..ea269a8a6 100644 --- a/entc/integration/migrate/migrate_test.go +++ b/entc/integration/migrate/migrate_test.go @@ -51,6 +51,7 @@ func TestMySQL(t *testing.T) { if version == "8" { CheckConstraint(t, clientv2) } + NicknameSearch(t, clientv2) }) } } @@ -347,6 +348,21 @@ func CheckConstraint(t *testing.T, client *entv2.Client) { require.Error(t, err) } +func NicknameSearch(t *testing.T, client *entv2.Client) { + ctx := context.Background() + names := client.User.Query(). + Where(func(s *sql.Selector) { + s.Where(sql.P(func(b *sql.Builder) { + b.WriteString("MATCH(").Ident(user.FieldNickname).WriteString(") AGAINST(").Arg("nick_bar | nick_foo").WriteString(")") + })) + }). + Unique(true). + Order(entv2.Asc(user.FieldNickname)). + Select(user.FieldNickname). + StringsX(ctx) + require.Equal(t, []string{"nick_bar", "nick_foo"}, names) +} + func EqualFold(t *testing.T, client *entv2.Client) { ctx := context.Background() t.Log("testing equal-fold on sql specific dialects")