From 3402d39054b03493f6ad8e797c89445d0923b5e0 Mon Sep 17 00:00:00 2001 From: ali ashraf Date: Mon, 18 Dec 2023 05:38:00 +0500 Subject: [PATCH 1/2] override identifier length for postgres --- postgres.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/postgres.go b/postgres.go index eb93b40..aa5e0b5 100644 --- a/postgres.go +++ b/postgres.go @@ -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}} } @@ -42,7 +47,20 @@ 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{} + } + namingStartegy.IdentifierMaxLength = defaultIdentifierLength + config.NamingStrategy = namingStartegy + return nil +} func (dialector Dialector) Initialize(db *gorm.DB) (err error) { callbackConfig := &callbacks.Config{ From db1dedd61b1a6edb118d0d2e52418020908889f0 Mon Sep 17 00:00:00 2001 From: ali ashraf Date: Fri, 29 Dec 2023 17:19:54 +0500 Subject: [PATCH 2/2] check if identifier length is set by user --- postgres.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/postgres.go b/postgres.go index aa5e0b5..960e067 100644 --- a/postgres.go +++ b/postgres.go @@ -57,7 +57,9 @@ func (dialector Dialector) Apply(config *gorm.Config) error { case nil: namingStartegy = &schema.NamingStrategy{} } - namingStartegy.IdentifierMaxLength = defaultIdentifierLength + if namingStartegy.IdentifierMaxLength <= 0 { + namingStartegy.IdentifierMaxLength = defaultIdentifierLength + } config.NamingStrategy = namingStartegy return nil }