Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

override identifier length for postgres #232

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading