dialect/entsql: supports setting SQL column comments (#3191)

* feat: Add column comment in SQL DDL, using EntSQL annotations to achieve it.

* Update annotation.go

* fix lint

* Add table primary key column comment

* entsql.Comment(string)  is unnecessary

* entc/gen: minor changes to entsql.WithComments + add tests

Co-authored-by: chenghonour <wantto@outlook.com>
This commit is contained in:
Ariel Mashraki
2022-12-26 14:48:37 +02:00
committed by GitHub
parent 038cc0cace
commit 136807549d
10 changed files with 94 additions and 21 deletions

View File

@@ -89,6 +89,16 @@ type Annotation struct {
//
Size int64 `json:"size,omitempty"`
// WithComments specifies whether fields' comments should
// be stored in the database schema as column comments.
//
// withCommentsEnabled := true
// entsql.WithComments{
// WithComments: &withCommentsEnabled,
// }
//
WithComments *bool `json:"with_comments,omitempty"`
// Incremental defines the auto-incremental behavior of a column. For example:
//
// incrementalEnabled := true
@@ -205,6 +215,20 @@ func DefaultExprs(exprs map[string]string) *Annotation {
}
}
// WithComments specifies whether fields' comments should
// be stored in the database schema as column comments.
//
// func (T) Annotations() []schema.Annotation {
// return []schema.Annotation{
// entsql.WithComments(true),
// }
// }
func WithComments(b bool) *Annotation {
return &Annotation{
WithComments: &b,
}
}
// Merge implements the schema.Merger interface.
func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
var ant Annotation
@@ -247,6 +271,9 @@ func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
if s := ant.Size; s != 0 {
a.Size = s
}
if b := ant.WithComments; b != nil {
a.WithComments = b
}
if i := ant.Incremental; i != nil {
a.Incremental = i
}

View File

@@ -912,6 +912,9 @@ func (a *Atlas) aColumns(et *Table, at *schema.Table) error {
if c1.Collation != "" {
c2.SetCollation(c1.Collation)
}
if c1.Comment != "" {
c2.SetComment(c1.Comment)
}
if err := a.sqlDialect.atTypeC(c1, c2); err != nil {
return err
}

View File

@@ -295,6 +295,7 @@ type Column struct {
typ string // row column type (used for Rows.Scan).
indexes Indexes // linked indexes.
foreign *ForeignKey // linked foreign-key.
Comment string // column comment.
}
// Expr represents a raw expression. It is used to distinguish between