Skip to content

Commit

Permalink
support auto_increment for any integer types (fixes #74)
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Sep 15, 2022
1 parent c681bf3 commit dbc82b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion translators/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ func (p *MySQL) buildColumn(c fizz.Column) string {
s = fmt.Sprintf("%s DEFAULT %s", s, d)
}

if c.Primary && (c.ColType == "integer" || strings.ToLower(c.ColType) == "int") {
// there are also tinyint, smallint, mediumint, and bigint
if c.Primary && (c.ColType == "integer" || strings.HasSuffix(strings.ToLower(c.ColType), "int")) {
s = fmt.Sprintf("%s AUTO_INCREMENT", s)
}
return s
Expand Down
27 changes: 27 additions & 0 deletions translators/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ FOREIGN KEY (` + "`user_id`" + `) REFERENCES ` + "`users`" + ` (` + "`id`" + `)
r.Equal(ddl, res)
}

func (p *MySQLSuite) Test_MySQL_CreateTables_WithVariousInt() {
r := p.Require()
ddl := `CREATE TABLE ` + "`manyusers`" + ` (
` + "`id`" + ` bigint NOT NULL AUTO_INCREMENT,
` + `PRIMARY KEY(` + "`id`" + `),
` + "`created_at`" + ` DATETIME NOT NULL,
` + "`updated_at`" + ` DATETIME NOT NULL
) ENGINE=InnoDB;
` + `CREATE TABLE ` + "`someusers`" + ` (
` + "`id`" + ` smallint NOT NULL AUTO_INCREMENT,
` + `PRIMARY KEY(` + "`id`" + `),
` + "`created_at`" + ` DATETIME NOT NULL,
` + "`updated_at`" + ` DATETIME NOT NULL
) ENGINE=InnoDB;`

res, err := fizz.AString(`
create_table("manyusers") {
t.Column("id", "bigint", {"primary": true})
}
create_table("someusers") {
t.Column("id", "smallint", {"primary": true})
}
`, myt)
r.NoError(err)
r.Equal(ddl, res)
}

func (p *MySQLSuite) Test_MySQL_CreateTables_WithCompositePrimaryKey() {
r := p.Require()
ddl := `CREATE TABLE ` + "`user_profiles`" + ` (
Expand Down

0 comments on commit dbc82b3

Please sign in to comment.