8000 dialect: support index comment in mysql and postgresql by liasica · Pull Request #4405 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dialect: support index comment in mysql and postgresql #4405

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions dialect/entsql/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,28 @@ type IndexAnnotation struct {
// )
// CREATE INDEX "table_a" ON "table"("a") WHERE (b AND c > 0)
Where string

// Comment defines the comment for the index.
//
// MySQL read more: https://dev.mysql.com/doc/refman/8.4/en/create-index.html
// PostgreSQL read more: https://www.postgresql.org/docs/current/sql-comment.html
//
// In MySQL, the following annotation maps to:
//
// index.Fields("c1").
// Annotation(
// entsql.IndexComment("Comment string"),
// )
// CREATE INDEX `table_c1` ON `table`(`c1`) COMMENT 'Comment string'
//
// In PostgreSQL, the following annotation maps to:
//
// index.Fields("c1").
// Annotation(
// entsql.IndexComment("Comment string"),
// )
// COMMENT ON INDEX "table_c1" IS 'Comment string'
Comment string
}

// Prefix returns a new index annotation with a single string column index.
Expand Down Expand Up @@ -780,6 +802,30 @@ func IndexWhere(pred string) *IndexAnnotation {
return &IndexAnnotation{Where: pred}
}

// IndexComment defines the comment for the index.
//
// MySQL read more: https://dev.mysql.com/doc/refman/8.4/en/create-index.html
// PostgreSQL read more: https://www.postgresql.org/docs/current/sql-comment.html
//
// In MySQL, the following annotation maps to:
//
// index.Fields("c1").
// Annotation(
// entsql.IndexComment("Comment string"),
// )
// CREATE INDEX `table_c1` ON `table`(`c1`) COMMENT 'Comment string'
//
// In PostgreSQL, the following annotation maps to:
//
// index.Fields("c1").
// Annotation(
// entsql.IndexComment("Comment string"),
// )
// COMMENT ON INDEX "table_c1" IS 'Comment string'
func IndexComment(comment string) *IndexAnnotation {
return &IndexAnnotation{Comment: comment}
}

// Name describes the annotation name.
func (IndexAnnotation) Name() string {
return "EntSQLIndexes"
Expand Down Expand Up @@ -848,6 +894,9 @@ func (a IndexAnnotation) Merge(other schema.Annotation) schema.Annotation {
if ant.Where != "" {
a.Where = ant.Where
}
if ant.Comment != "" {
a.Comment = ant.Comment
}
return a
}

Expand Down
3 changes: 3 additions & 0 deletions dialect/sql/schema/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ func (d *MySQL) atIndex(idx1 *Index, t2 *schema.Table, idx2 *schema.Index) error
if t, ok := indexType(idx1, dialect.MySQL); ok {
idx2.AddAttrs(&mysql.IndexType{T: t})
}
if idx1.Annotation != nil && idx1.Annotation.Comment != "" {
idx2.SetComment(idx1.Annotation.Comment)
}
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions dialect/sql/schema/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ func (d *Postgres) atIndex(idx1 *Index, t2 *schema.Table, idx2 *schema.Index) er
if idx1.Annotation != nil && idx1.Annotation.Where != "" {
idx2.AddAttrs(&postgres.IndexPredicate{P: idx1.Annotation.Where})
}
if idx1.Annotation != nil && idx1.Annotation.Comment != "" {
idx2.SetComment(idx1.Annotation.Comment)
}
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions entc/gen/template/migrate/schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ var (
{{- with $ant.Where }}
Where: {{ quote . }},
{{- end }}
{{- with $ant.Comment }}
Comment: {{ quote . }},
{{- end }}
},
{{- end }}
},
Expand Down
Loading
0