diff --git a/internal/integration/mysql.hcl b/internal/integration/mysql.hcl index a329c30e..b0711546 100644 --- a/internal/integration/mysql.hcl +++ b/internal/integration/mysql.hcl @@ -17,6 +17,7 @@ database "db" { } model "User" { + comment = "用户表" has_one "account" {} has_many "user_posts" {} has_many "posts" { @@ -24,6 +25,7 @@ database "db" { } column "name" { + comment = "用户名" type = string } column "type" { diff --git a/internal/integration/postgresql.hcl b/internal/integration/postgresql.hcl index 80bda1c8..f9ff0af7 100644 --- a/internal/integration/postgresql.hcl +++ b/internal/integration/postgresql.hcl @@ -17,6 +17,7 @@ database "db" { } model "User" { + comment = "用户表" has_one "account" {} has_many "user_posts" {} has_many "posts" { @@ -24,6 +25,7 @@ database "db" { } column "name" { + comment = "用户名" type = string } column "type" { diff --git a/internal/integration/sqlite.hcl b/internal/integration/sqlite.hcl index c056094a..9490a65c 100644 --- a/internal/integration/sqlite.hcl +++ b/internal/integration/sqlite.hcl @@ -17,6 +17,7 @@ database "db" { } model "User" { + comment = "用户表" has_one "account" {} has_many "user_posts" {} has_many "posts" { @@ -24,6 +25,7 @@ database "db" { } column "name" { + comment = "用户名" type = string } column "type" { diff --git a/schema/hcl.go b/schema/hcl.go index f86642cf..309b0a41 100644 --- a/schema/hcl.go +++ b/schema/hcl.go @@ -54,6 +54,7 @@ var hclModel = &hcl.BodySchema{ {Name: "primary_key"}, {Name: "timestamps"}, {Name: "default_primary_key"}, + {Name: "comment"}, }, Blocks: []hcl.BlockHeaderSchema{ {Type: "column", LabelNames: []string{"name"}}, @@ -73,6 +74,7 @@ var hclColumn = &hcl.BodySchema{ {Name: "null"}, {Name: "default"}, {Name: "unique"}, + {Name: "comment"}, }, Blocks: []hcl.BlockHeaderSchema{}, } @@ -287,6 +289,8 @@ func (db *Database) modelFromBlock(block *hcl.Block, ctx *hcl.EvalContext) (*Mod if !timestamps { addTimestamps = false } + case "comment": + m.Comment = value.AsString() } } @@ -496,6 +500,8 @@ func columnFromBlock(block *hcl.Block, ctx *hcl.EvalContext) (*Column, error) { column.Array = valueAsBool(value) case "null": column.Null = valueAsBool(value) + case "comment": + column.Comment = value.AsString() } } for name, attr := range content.Attributes { diff --git a/schema/model.go b/schema/model.go index dac50a08..9bc732d5 100644 --- a/schema/model.go +++ b/schema/model.go @@ -9,6 +9,7 @@ type Model struct { TableName string TimeZone string Timestamps bool + Comment string Columns []*Column Attributes []*Attribute BelongsTo []*BelongsTo @@ -51,6 +52,7 @@ type Column struct { // sql auto_increment AutoIncrement bool Default interface{} // TODO: support default + Comment string } type Type struct { diff --git a/schema/mysql.go b/schema/mysql.go index 264dfae7..0e0acd2f 100644 --- a/schema/mysql.go +++ b/schema/mysql.go @@ -50,6 +50,9 @@ func (d *Database) CreateMySQLSchema(dbName string) *schema.Schema { } col.SetNull(c.Null) + if c.Comment != "" { + col.SetComment(fmt.Sprintf("'%v'", c.Comment)) + } t.AddColumns(col) columnMap[c.Name] = col } @@ -77,7 +80,9 @@ func (d *Database) CreateMySQLSchema(dbName string) *schema.Schema { } t.AddIndexes(index) } - + if model.Comment != "" { + t.SetComment(fmt.Sprintf("'%v'", model.Comment)) + } public.AddTables(t) } diff --git a/schema/postgresql.go b/schema/postgresql.go index 64e735ab..96d2d544 100644 --- a/schema/postgresql.go +++ b/schema/postgresql.go @@ -104,6 +104,9 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { } col.SetNull(c.Null) + if c.Comment != "" { + col.SetComment(c.Comment) + } t.AddColumns(col) columnMap[c.Name] = col } @@ -131,7 +134,9 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { } t.AddIndexes(index) } - + if model.Comment != "" { + t.SetComment(model.Comment) + } public.AddTables(t) } diff --git a/schema/sqlite.go b/schema/sqlite.go index 10f8005f..cd98f2aa 100644 --- a/schema/sqlite.go +++ b/schema/sqlite.go @@ -80,6 +80,9 @@ func (d *Database) CreateSQLiteSchema(dbName string) *schema.Schema { } col.SetNull(c.Null) + if c.Comment != "" { + col.SetComment(c.Comment) + } t.AddColumns(col) columnMap[c.Name] = col } @@ -108,6 +111,9 @@ func (d *Database) CreateSQLiteSchema(dbName string) *schema.Schema { t.AddIndexes(index) } + if model.Comment != "" { + t.SetComment(model.Comment) + } public.AddTables(t) public.Name = "main" }