Skip to content

Commit 7f25bff

Browse files
authored
Optimize regular expression compilation cache (#314)
* Optimize regular expression compilation cache * Format code
1 parent 1ffb5a7 commit 7f25bff

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

migrator.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package postgres
33
import (
44
"database/sql"
55
"fmt"
6-
"github.com/jackc/pgx/v5"
76
"regexp"
87
"strings"
98

9+
"github.com/jackc/pgx/v5"
1010
"gorm.io/gorm"
1111
"gorm.io/gorm/clause"
1212
"gorm.io/gorm/migrator"
@@ -68,6 +68,11 @@ var typeAliasMap = map[string][]string{
6868
"time with time zone": {"timetz"},
6969
}
7070

71+
var (
72+
autoIncrementValuePattern = regexp.MustCompile(`^nextval\('"?[^']+seq"?'::regclass\)$`)
73+
defaultValueValuePattern = regexp.MustCompile(`^(.*?)(?:::.*)?$`)
74+
)
75+
7176
type Migrator struct {
7277
migrator.Migrator
7378
}
@@ -501,7 +506,6 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
501506
column.LengthValue = typeLenValue
502507
}
503508

504-
autoIncrementValuePattern := regexp.MustCompile(`^nextval\('"?[^']+seq"?'::regclass\)$`)
505509
if autoIncrementValuePattern.MatchString(column.DefaultValueValue.String) || (identityIncrement.Valid && identityIncrement.String != "") {
506510
column.AutoIncrementValue = sql.NullBool{Bool: true, Valid: true}
507511
column.DefaultValueValue = sql.NullString{}
@@ -816,6 +820,6 @@ func (m Migrator) RenameColumn(dst interface{}, oldName, field string) error {
816820
}
817821

818822
func parseDefaultValueValue(defaultValue string) string {
819-
value := regexp.MustCompile(`^(.*?)(?:::.*)?$`).ReplaceAllString(defaultValue, "$1")
823+
value := defaultValueValuePattern.ReplaceAllString(defaultValue, "$1")
820824
return strings.Trim(value, "'")
821825
}

0 commit comments

Comments
 (0)