Skip to content

support table comment and column comment #25

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: main
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
2 changes: 2 additions & 0 deletions internal/integration/mysql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ database "db" {
}

model "User" {
comment = "用户表"
has_one "account" {}
has_many "user_posts" {}
has_many "posts" {
through = "user_posts"
}

column "name" {
comment = "用户名"
type = string
}
column "type" {
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/postgresql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ database "db" {
}

model "User" {
comment = "用户表"
has_one "account" {}
has_many "user_posts" {}
has_many "posts" {
through = "user_posts"
}

column "name" {
comment = "用户名"
type = string
}
column "type" {
Expand Down
2 changes: 2 additions & 0 deletions internal/integration/sqlite.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ database "db" {
}

model "User" {
comment = "用户表"
has_one "account" {}
has_many "user_posts" {}
has_many "posts" {
through = "user_posts"
}

column "name" {
comment = "用户名"
type = string
}
column "type" {
Expand Down
6 changes: 6 additions & 0 deletions schema/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand All @@ -73,6 +74,7 @@ var hclColumn = &hcl.BodySchema{
{Name: "null"},
{Name: "default"},
{Name: "unique"},
{Name: "comment"},
},
Blocks: []hcl.BlockHeaderSchema{},
}
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions schema/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Model struct {
TableName string
TimeZone string
Timestamps bool
Comment string
Columns []*Column
Attributes []*Attribute
BelongsTo []*BelongsTo
Expand Down Expand Up @@ -51,6 +52,7 @@ type Column struct {
// sql auto_increment
AutoIncrement bool
Default interface{} // TODO: support default
Comment string
}

type Type struct {
Expand Down
7 changes: 6 additions & 1 deletion schema/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 6 additions & 1 deletion schema/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions schema/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
}
Expand Down