Skip to content

Commit

Permalink
sql/mysql: add support for column-level auto_increment
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Feb 12, 2022
1 parent 54dcde3 commit 038f875
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions schema/schemaspec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (a *Attr) Int() (int, error) {
}
s, err := strconv.Atoi(lit.V)
if err != nil {
return 0, fmt.Errorf("schema: cannot read attribute %q as integer", a.K)
return 0, fmt.Errorf("schema: cannot read attribute %q as int: %w", a.K, err)
}
return s, nil
}
Expand All @@ -131,7 +131,11 @@ func (a *Attr) Bool() (bool, error) {
if !ok {
return false, fmt.Errorf("schema: cannot read attribute %q as literal", a.K)
}
return strconv.ParseBool(lit.V)
b, err := strconv.ParseBool(lit.V)
if err != nil {
return false, fmt.Errorf("schema: cannot read attribute %q as bool: %w", a.K, err)
}
return b, nil
}

// Ref returns the string representation of the Attr. If the value is not a Ref or the value
Expand Down
9 changes: 9 additions & 0 deletions sql/mysql/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ func convertColumn(spec *sqlspec.Column, _ *schema.Table) (*schema.Column, error
}
c.AddAttrs(&OnUpdate{A: exp.X})
}
if attr, ok := spec.Attr("auto_increment"); ok {
b, err := attr.Bool()
if err != nil {
return nil, err
}
if b {
c.AddAttrs(&AutoIncrement{})
}
}
return c, err
}

Expand Down
3 changes: 3 additions & 0 deletions sql/mysql/sqlspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ table "table" {
}
column "price1" {
type = int
auto_increment = false
}
column "price2" {
type = int
auto_increment = true
}
column "account_name" {
type = varchar(32)
Expand Down Expand Up @@ -136,6 +138,7 @@ table "accounts" {
T: TypeInt,
},
},
Attrs: []schema.Attr{&AutoIncrement{}},
},
{
Name: "account_name",
Expand Down

0 comments on commit 038f875

Please sign in to comment.