Skip to content

Commit

Permalink
feat: custom type implements autoIncrement (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanlang committed Jul 25, 2022
1 parent bfee27c commit dedd1d2
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion postgres.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
Expand Down Expand Up @@ -192,9 +193,30 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
return "timestamptz"
case schema.Bytes:
return "bytea"
default:
return dialector.getSchemaCustomType(field)
}
}

func (dialector Dialector) getSchemaCustomType(field *schema.Field) string {
sqlType := string(field.DataType)

if field.AutoIncrement && !strings.Contains(strings.ToLower(sqlType), "serial") {
size := field.Size
if field.GORMDataType == schema.Uint {
size++
}
switch {
case size <= 16:
sqlType = "smallserial"
case size <= 32:
sqlType = "serial"
default:
sqlType = "bigserial"
}
}

return string(field.DataType)
return sqlType
}

func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error {
Expand Down

0 comments on commit dedd1d2

Please sign in to comment.