|
5 | 5 | "database/sql"
|
6 | 6 | "fmt"
|
7 | 7 | "math"
|
| 8 | + "regexp" |
| 9 | + "strconv" |
8 | 10 | "strings"
|
9 | 11 | "time"
|
10 | 12 |
|
@@ -119,7 +121,9 @@ func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
|
119 | 121 | dialector.Config.DontSupportRenameColumn = true
|
120 | 122 | dialector.Config.DontSupportForShareClause = true
|
121 | 123 | dialector.Config.DontSupportNullAsDefaultValue = true
|
122 |
| - withReturning = true |
| 124 | + if checkVersion(dialector.ServerVersion, "10.5") { |
| 125 | + withReturning = true |
| 126 | + } |
123 | 127 | } else if strings.HasPrefix(dialector.ServerVersion, "5.6.") {
|
124 | 128 | dialector.Config.DontSupportRenameIndex = true
|
125 | 129 | dialector.Config.DontSupportRenameColumn = true
|
@@ -454,3 +458,29 @@ func (dialector Dialector) SavePoint(tx *gorm.DB, name string) error {
|
454 | 458 | func (dialector Dialector) RollbackTo(tx *gorm.DB, name string) error {
|
455 | 459 | return tx.Exec("ROLLBACK TO SAVEPOINT " + name).Error
|
456 | 460 | }
|
| 461 | + |
| 462 | +var versionTrimerRegexp = regexp.MustCompile(`^(\d+).*$`) |
| 463 | + |
| 464 | +// checkVersion newer or equal returns true, old returns false |
| 465 | +func checkVersion(newVersion, oldVersion string) bool { |
| 466 | + if newVersion == oldVersion { |
| 467 | + return true |
| 468 | + } |
| 469 | + |
| 470 | + newVersions := strings.Split(newVersion, ".") |
| 471 | + oldVersions := strings.Split(oldVersion, ".") |
| 472 | + for idx, nv := range newVersions { |
| 473 | + if len(oldVersions) <= idx { |
| 474 | + return true |
| 475 | + } |
| 476 | + |
| 477 | + nvi, _ := strconv.Atoi(versionTrimerRegexp.ReplaceAllString(nv, "$1")) |
| 478 | + ovi, _ := strconv.Atoi(versionTrimerRegexp.ReplaceAllString(oldVersions[idx], "$1")) |
| 479 | + if nvi == ovi { |
| 480 | + continue |
| 481 | + } |
| 482 | + return nvi > ovi |
| 483 | + } |
| 484 | + |
| 485 | + return false |
| 486 | +} |
0 commit comments