Skip to content

Commit

Permalink
sql/mysql: marshal inspected auto_increment
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Feb 13, 2022
1 parent 9f669a2 commit a15e081
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sql/mysql/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ func columnSpec(c *schema.Column, t *schema.Table) (*sqlspec.Column, error) {
if o := (OnUpdate{}); sqlx.Has(c.Attrs, &o) {
col.Extra.Attrs = append(col.Extra.Attrs, specutil.RawAttr("on_update", o.A))
}
if sqlx.Has(c.Attrs, &AutoIncrement{}) {
col.Extra.Attrs = append(col.Extra.Attrs, specutil.BoolAttr("auto_increment", true))
}
return col, nil
}

Expand Down
35 changes: 35 additions & 0 deletions sql/mysql/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,41 @@ schema "test" {
require.EqualValues(t, expected, string(buf))
}

func TestMarshalSpec_AutoIncrement(t *testing.T) {
s := &schema.Schema{
Name: "test",
Tables: []*schema.Table{
{
Name: "users",
Columns: []*schema.Column{
{
Name: "id",
Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}},
Attrs: []schema.Attr{
&AutoIncrement{V: 1000},
},
},
},
},
},
}
s.Tables[0].Schema = s
buf, err := MarshalSpec(s, hclState)
require.NoError(t, err)
const expected = `table "users" {
schema = schema.test
column "id" {
null = false
type = bigint
auto_increment = true
}
}
schema "test" {
}
`
require.EqualValues(t, expected, string(buf))
}

func TestMarshalSpec_Check(t *testing.T) {
s := schema.New("test").
AddTables(
Expand Down

0 comments on commit a15e081

Please sign in to comment.