Skip to content

Commit

Permalink
override identifier length for postgres (#232)
Browse files Browse the repository at this point in the history
* override identifier length for postgres

* check if identifier length is set by user
  • Loading branch information
alidevhere committed Jan 29, 2024
1 parent 68a877c commit e6d2435
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Config struct {
Conn gorm.ConnPool
}

var (
timeZoneMatcher = regexp.MustCompile("(time_zone|TimeZone)=(.*?)($|&| )")
defaultIdentifierLength = 63 //maximum identifier length for postgres
)

func Open(dsn string) gorm.Dialector {
return &Dialector{&Config{DSN: dsn}}
}
Expand All @@ -42,7 +47,22 @@ func (dialector Dialector) Name() string {
return "postgres"
}

var timeZoneMatcher = regexp.MustCompile("(time_zone|TimeZone)=(.*?)($|&| )")
func (dialector Dialector) Apply(config *gorm.Config) error {
var namingStartegy *schema.NamingStrategy
switch v := config.NamingStrategy.(type) {
case *schema.NamingStrategy:
namingStartegy = v
case schema.NamingStrategy:
namingStartegy = &v
case nil:
namingStartegy = &schema.NamingStrategy{}
}
if namingStartegy.IdentifierMaxLength <= 0 {
namingStartegy.IdentifierMaxLength = defaultIdentifierLength
}
config.NamingStrategy = namingStartegy
return nil
}

func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
callbackConfig := &callbacks.Config{
Expand Down

0 comments on commit e6d2435

Please sign in to comment.