Skip to content
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

Add comment support for MySQL Server #142

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ For example for PostgreSQL you could pass `jsonb`and it will be supported, howev
* `default_raw` - The default value defined as a database function.
* `after` - (MySQL Only) Add a column after another column in the table. `example: {"after":"created_at"}`
* `first` - (MySQL Only) Add a column to the first position in the table. `example: {"first": true}`
* `comment` - (MySQL Only) The comment of the column. `example: {"comment": "A comment"}`

#### Composite primary key

Expand Down
3 changes: 2 additions & 1 deletion testdata/migrations/20160808213308_setup_tests.down.fizz
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ drop_table("validatable_cars")
drop_table("not_validatable_cars")
drop_table("callbacks_users")
drop_table("books")
drop_table("cars")
drop_table("cars")
drop_table("user_with_comments")
5 changes: 5 additions & 0 deletions testdata/migrations/20160808213308_setup_tests.up.fizz
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ create_table("taxis") {
t.Column("model", "string")
t.Column("user_id", "int", {null: true})
}

create_table("user_with_comments", {"comment": "Table's comment"}) {
t.Column("id", "int", {"primary": true, "comment": "Identifier"})
t.Column("email", "string", {"size": 20, "comment": "Private email"})
}
6 changes: 6 additions & 0 deletions translators/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (p *MySQL) CreateTable(t fizz.Table) (string, error) {
}

s := fmt.Sprintf("CREATE TABLE %s (\n%s\n) ENGINE=InnoDB;", p.escapeIdentifier(t.Name), strings.Join(cols, ",\n"))
if t.Options["comment"] != nil {
s = fmt.Sprintf("%s COMMENT='%v';", s[:len(s)-1], strings.ReplaceAll(fmt.Sprintf("%v", t.Options["comment"]), "'", "\\'"))
}
sql = append(sql, s)

for _, i := range t.Indexes {
Expand Down Expand Up @@ -228,6 +231,9 @@ func (p *MySQL) buildColumn(c fizz.Column) string {
if c.Primary && (c.ColType == "integer" || strings.HasSuffix(strings.ToLower(c.ColType), "int")) {
s = fmt.Sprintf("%s AUTO_INCREMENT", s)
}
if c.Options["comment"] != nil {
s = fmt.Sprintf("%s COMMENT '%v'", s, strings.ReplaceAll(fmt.Sprintf("%v", c.Options["comment"]), "'", "\\'"))
}
return s
}

Expand Down
8 changes: 6 additions & 2 deletions translators/mysql_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type mysqlTableInfo struct {
Null string `db:"Null"`
Key string `db:"Key"`
Default interface{} `db:"Default"`
Comment string `db:"Comment"`
Extra string `db:"Extra"`
}

Expand All @@ -34,6 +35,9 @@ func (ti mysqlTableInfo) ToColumn() fizz.Column {
d := fmt.Sprintf("%s", ti.Default)
c.Options["default"] = d
}
if len(ti.Comment) > 0 {
c.Options["comment"] = ti.Comment
}
return c
}

Expand Down Expand Up @@ -98,7 +102,7 @@ func (p *mysqlSchema) Build() error {
}

func (p *mysqlSchema) buildTableData(table *fizz.Table, db *sql.DB) error {
prag := fmt.Sprintf("SELECT COLUMN_NAME AS `Field`, COLUMN_TYPE AS `Type`, IS_NULLABLE AS `Null`, COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS `Extra` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s';", table.Name)
prag := fmt.Sprintf("SELECT COLUMN_NAME AS `Field`, COLUMN_TYPE AS `Type`, IS_NULLABLE AS `Null`, COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, COLUMN_COMMENT AS `Comment`, EXTRA AS `Extra` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s';", p.Name, table.Name)

res, err := db.Query(prag)
if err != nil {
Expand All @@ -108,7 +112,7 @@ func (p *mysqlSchema) buildTableData(table *fizz.Table, db *sql.DB) error {

for res.Next() {
ti := mysqlTableInfo{}
err = res.Scan(&ti.Field, &ti.Type, &ti.Null, &ti.Key, &ti.Default, &ti.Extra)
err = res.Scan(&ti.Field, &ti.Type, &ti.Null, &ti.Key, &ti.Default, &ti.Comment, &ti.Extra)
if err != nil {
return err
}
Expand Down
30 changes: 30 additions & 0 deletions translators/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ PRIMARY KEY(` + "`user_id`" + `, ` + "`profile_id`" + `)
r.Equal(ddl, res)
}

func (p *MySQLSuite) Test_MySQL_CreateTables_WithComments() {
r := p.Require()
ddl := `CREATE TABLE ` + "`user_with_comments`" + ` (
` + "`id`" + ` INTEGER NOT NULL AUTO_INCREMENT COMMENT 'Identifier',
PRIMARY KEY(` + "`id`" + `),
` + "`email`" + ` VARCHAR (20) NOT NULL COMMENT 'Private email',
` + "`created_at`" + ` DATETIME NOT NULL,
` + "`updated_at`" + ` DATETIME NOT NULL
) ENGINE=InnoDB COMMENT='Table\'s comment';`

res, err := fizz.AString(`
create_table("user_with_comments", {"comment": "Table's comment"}) {
t.Column("id", "INT", {"primary": true, "comment": "Identifier"})
t.Column("email", "string", {"size":20, "comment": "Private email"})
}
`, myt)
r.NoError(err)
r.Equal(ddl, res)
}

func (p *MySQLSuite) Test_MySQL_DropTable() {
r := p.Require()

Expand Down Expand Up @@ -266,6 +286,16 @@ func (p *MySQLSuite) Test_MySQL_AddColumnFirst() {
r.Equal(ddl, res)
}

func (p *MySQLSuite) Test_MySQL_AddColumnWithComment() {
r := p.Require()
ddl := `ALTER TABLE ` + "`user_with_comments`" + ` ADD COLUMN ` + "`name`" + ` VARCHAR (50) NOT NULL COMMENT 'Username';`

res, err := fizz.AString(`add_column("user_with_comments", "name", "string", {"size": 50, "comment": "Username"})`, myt)
r.NoError(err)

r.Equal(ddl, res)
}

func (p *MySQLSuite) Test_MySQL_DropColumn() {
r := p.Require()
ddl := `ALTER TABLE ` + "`users`" + ` DROP COLUMN ` + "`mycolumn`" + `;`
Expand Down