fix: failed to migrate
This commit is contained in:
+51
-7
@@ -24,11 +24,17 @@ type Dialector struct {
|
||||
type Config struct {
|
||||
DriverName string
|
||||
DSN string
|
||||
WithoutQuotingCheck bool
|
||||
PreferSimpleProtocol bool
|
||||
WithoutReturning bool
|
||||
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}}
|
||||
}
|
||||
@@ -41,17 +47,42 @@ func (dialector Dialector) Name() string {
|
||||
return "postgres"
|
||||
}
|
||||
|
||||
var timeZoneMatcher = regexp.MustCompile("(time_zone|TimeZone)=(.*?)($|&| )")
|
||||
func (dialector Dialector) Apply(config *gorm.Config) error {
|
||||
if config.NamingStrategy == nil {
|
||||
config.NamingStrategy = schema.NamingStrategy{
|
||||
IdentifierMaxLength: defaultIdentifierLength,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
switch v := config.NamingStrategy.(type) {
|
||||
case *schema.NamingStrategy:
|
||||
if v.IdentifierMaxLength <= 0 {
|
||||
v.IdentifierMaxLength = defaultIdentifierLength
|
||||
}
|
||||
case schema.NamingStrategy:
|
||||
if v.IdentifierMaxLength <= 0 {
|
||||
v.IdentifierMaxLength = defaultIdentifierLength
|
||||
config.NamingStrategy = v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
|
||||
callbackConfig := &callbacks.Config{
|
||||
CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT"},
|
||||
UpdateClauses: []string{"UPDATE", "SET", "FROM", "WHERE"},
|
||||
DeleteClauses: []string{"DELETE", "FROM", "WHERE"},
|
||||
}
|
||||
// register callbacks
|
||||
if !dialector.WithoutReturning {
|
||||
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
|
||||
CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"},
|
||||
UpdateClauses: []string{"UPDATE", "SET", "WHERE", "RETURNING"},
|
||||
DeleteClauses: []string{"DELETE", "FROM", "WHERE", "RETURNING"},
|
||||
})
|
||||
callbackConfig.CreateClauses = append(callbackConfig.CreateClauses, "RETURNING")
|
||||
callbackConfig.UpdateClauses = append(callbackConfig.UpdateClauses, "RETURNING")
|
||||
callbackConfig.DeleteClauses = append(callbackConfig.DeleteClauses, "RETURNING")
|
||||
}
|
||||
callbacks.RegisterDefaultCallbacks(db, callbackConfig)
|
||||
|
||||
if dialector.Conn != nil {
|
||||
db.ConnPool = dialector.Conn
|
||||
@@ -90,10 +121,23 @@ func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression
|
||||
|
||||
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
|
||||
writer.WriteByte('$')
|
||||
writer.WriteString(strconv.Itoa(len(stmt.Vars)))
|
||||
index := 0
|
||||
varLen := len(stmt.Vars)
|
||||
if varLen > 0 {
|
||||
switch stmt.Vars[0].(type) {
|
||||
case pgx.QueryExecMode:
|
||||
index++
|
||||
}
|
||||
}
|
||||
writer.WriteString(strconv.Itoa(varLen - index))
|
||||
}
|
||||
|
||||
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
|
||||
if dialector.WithoutQuotingCheck {
|
||||
writer.WriteString(str)
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
underQuoted, selfQuoted bool
|
||||
continuousBacktick int8
|
||||
|
||||
Reference in New Issue
Block a user