Skip to content

Optimize regular expression compilation cache #314

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package postgres
import (
"database/sql"
"fmt"
"github.com/jackc/pgx/v5"
"regexp"
"strings"

"github.com/jackc/pgx/v5"

"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
Expand Down Expand Up @@ -68,6 +69,11 @@ var typeAliasMap = map[string][]string{
"time with time zone": {"timetz"},
}

var (
autoIncrementValuePattern = regexp.MustCompile(`^nextval\('"?[^']+seq"?'::regclass\)$`)
defaultValueValuePattern = regexp.MustCompile(`^(.*?)(?:::.*)?$`)
)

type Migrator struct {
migrator.Migrator
}
Expand Down Expand Up @@ -501,7 +507,6 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
column.LengthValue = typeLenValue
}

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

func parseDefaultValueValue(defaultValue string) string {
value := regexp.MustCompile(`^(.*?)(?:::.*)?$`).ReplaceAllString(defaultValue, "$1")
value := defaultValueValuePattern.ReplaceAllString(defaultValue, "$1")
return strings.Trim(value, "'")
}
Loading