From a15e081183f7a63b014eb2d1231bf319564103c8 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Sun, 13 Feb 2022 09:32:32 +0200 Subject: [PATCH] sql/mysql: marshal inspected auto_increment --- sql/mysql/sqlspec.go | 3 +++ sql/mysql/sqlspec_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/sql/mysql/sqlspec.go b/sql/mysql/sqlspec.go index ee39f96b0de..cc15d6f682c 100644 --- a/sql/mysql/sqlspec.go +++ b/sql/mysql/sqlspec.go @@ -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 } diff --git a/sql/mysql/sqlspec_test.go b/sql/mysql/sqlspec_test.go index 8e9d9b8a255..2cb9b443710 100644 --- a/sql/mysql/sqlspec_test.go +++ b/sql/mysql/sqlspec_test.go @@ -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(