Skip to content

Commit a15e081

Browse files
committed
sql/mysql: marshal inspected auto_increment
1 parent 9f669a2 commit a15e081

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

sql/mysql/sqlspec.go

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ func columnSpec(c *schema.Column, t *schema.Table) (*sqlspec.Column, error) {
229229
if o := (OnUpdate{}); sqlx.Has(c.Attrs, &o) {
230230
col.Extra.Attrs = append(col.Extra.Attrs, specutil.RawAttr("on_update", o.A))
231231
}
232+
if sqlx.Has(c.Attrs, &AutoIncrement{}) {
233+
col.Extra.Attrs = append(col.Extra.Attrs, specutil.BoolAttr("auto_increment", true))
234+
}
232235
return col, nil
233236
}
234237

sql/mysql/sqlspec_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,41 @@ schema "test" {
466466
require.EqualValues(t, expected, string(buf))
467467
}
468468

469+
func TestMarshalSpec_AutoIncrement(t *testing.T) {
470+
s := &schema.Schema{
471+
Name: "test",
472+
Tables: []*schema.Table{
473+
{
474+
Name: "users",
475+
Columns: []*schema.Column{
476+
{
477+
Name: "id",
478+
Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}},
479+
Attrs: []schema.Attr{
480+
&AutoIncrement{V: 1000},
481+
},
482+
},
483+
},
484+
},
485+
},
486+
}
487+
s.Tables[0].Schema = s
488+
buf, err := MarshalSpec(s, hclState)
489+
require.NoError(t, err)
490+
const expected = `table "users" {
491+
schema = schema.test
492+
column "id" {
493+
null = false
494+
type = bigint
495+
auto_increment = true
496+
}
497+
}
498+
schema "test" {
499+
}
500+
`
501+
require.EqualValues(t, expected, string(buf))
502+
}
503+
469504
func TestMarshalSpec_Check(t *testing.T) {
470505
s := schema.New("test").
471506
AddTables(

0 commit comments

Comments
 (0)