dialect/entsql: add support for index-type annotation

This commit is contained in:
Ariel Mashraki
2022-02-22 13:41:11 +02:00
committed by Ariel Mashraki
parent d80f7cc41a
commit fe2511fc8d
5 changed files with 97 additions and 0 deletions

View File

@@ -224,6 +224,30 @@ type IndexAnnotation struct {
// CREATE INDEX `table_c1_c2_c3` ON `table`(`c1` DESC, `c2` DESC, `c3`)
//
DescColumns map[string]bool
// Type defines the type of the index.
// In MySQL, the following annotation maps to:
//
// index.Fields("c1").
// Annotation(
// entsql.IndexType("FULLTEXT"),
// )
//
// CREATE FULLTEXT INDEX `table_c1` ON `table`(`c1`)
//
Type string
// Types is like the Type option but allows mapping an index-type per dialect.
//
// index.Fields("c1").
// Annotation(
// entsql.IndexTypes(map[string]string{
// dialect.MySQL: "FULLTEXT",
// dialect.Postgres: "GIN",
// }),
// )
//
Types map[string]string
}
// Prefix returns a new index annotation with a single string column index.
@@ -293,6 +317,34 @@ func DescColumns(names ...string) *IndexAnnotation {
return ant
}
// Type defines the type of the index.
// In MySQL, the following annotation maps to:
//
// index.Fields("c1").
// Annotation(
// entsql.IndexType("FULLTEXT"),
// )
//
// CREATE FULLTEXT INDEX `table_c1` ON `table`(`c1`)
//
func IndexType(t string) *IndexAnnotation {
return &IndexAnnotation{Type: t}
}
// Types is like the Type option but allows mapping an index-type per dialect.
//
// index.Fields("c1").
// Annotations(
// entsql.IndexTypes(map[string]string{
// dialect.MySQL: "FULLTEXT",
// dialect.Postgres: "GIN",
// }),
// )
//
func IndexTypes(types map[string]string) *IndexAnnotation {
return &IndexAnnotation{Types: types}
}
// Name describes the annotation name.
func (IndexAnnotation) Name() string {
return "EntSQLIndexes"
@@ -333,6 +385,12 @@ func (a IndexAnnotation) Merge(other schema.Annotation) schema.Annotation {
a.DescColumns[column] = desc
}
}
if ant.Type != "" {
a.Type = ant.Type
}
if ant.Types != nil {
a.Types = ant.Types
}
return a
}

View File

@@ -951,5 +951,22 @@ func (d *MySQL) atIndex(idx1 *Index, t2 *schema.Table, idx2 *schema.Index) error
}
idx2.AddParts(part)
}
if t, ok := indexType(idx1, dialect.MySQL); ok {
idx2.AddAttrs(&mysql.IndexType{T: t})
}
return nil
}
func indexType(idx *Index, d string) (string, bool) {
ant := idx.Annotation
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
}
return "", false
}

View File

@@ -779,5 +779,8 @@ func (d *Postgres) atIndex(idx1 *Index, t2 *schema.Table, idx2 *schema.Index) er
}
idx2.AddParts(&schema.IndexPart{C: c2})
}
if t, ok := indexType(idx1, dialect.Postgres); ok {
idx2.AddAttrs(&postgres.IndexType{T: t})
}
return nil
}

View File

@@ -130,6 +130,16 @@ var (
{{- end }}
},
{{- end }}
{{- with $ant.Type }}
Type: "{{ . }}",
{{- end }}
{{- with $keys := keys $ant.Types }}
Types: map[string]string{
{{- range $k := $keys }}
"{{ $k }}": "{{ index $ant.Types $k }}",
{{- end }}
},
{{- end }}
},
{{- end }}
},

View File

@@ -7,6 +7,8 @@ package schema
import (
"time"
"entgo.io/ent/dialect"
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
@@ -126,6 +128,13 @@ func (User) Indexes() []ent.Index {
Unique(),
index.Fields("age").
Annotations(entsql.Desc()),
index.Fields("nickname").
Annotations(
entsql.IndexTypes(map[string]string{
dialect.MySQL: "FULLTEXT",
dialect.Postgres: "GIN",
}),
),
}
}