diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-03-22 23:12:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-22 11:12:55 -0400 |
commit | c61b902538e8e4b2bb83136f190e044a6bbcdd9b (patch) | |
tree | bc08142f5ced69721ab14793b8dc62aa83314268 /models | |
parent | dcaa5643d70cd1be72e53e82a30d2bc6e5f8de92 (diff) | |
download | gitea-c61b902538e8e4b2bb83136f190e044a6bbcdd9b.tar.gz gitea-c61b902538e8e4b2bb83136f190e044a6bbcdd9b.zip |
Upgrade xorm to v1.0.0 (#10646)
* Upgrade xorm to v1.0.0
* small nit
* Fix tests
* Update xorm
* Update xorm
* fix go.sum
* fix test
* Fix bug when dump
* Fix bug
* update xorm to latest
* Fix migration test
* update xorm to latest
* Fix import order
* Use xorm tag
Diffstat (limited to 'models')
-rw-r--r-- | models/access.go | 7 | ||||
-rw-r--r-- | models/list_options.go | 3 | ||||
-rw-r--r-- | models/log.go | 18 | ||||
-rw-r--r-- | models/login_source.go | 20 | ||||
-rw-r--r-- | models/migrations/v110.go | 24 | ||||
-rw-r--r-- | models/migrations/v81.go | 9 | ||||
-rw-r--r-- | models/models.go | 28 | ||||
-rw-r--r-- | models/org_team.go | 12 | ||||
-rw-r--r-- | models/repo.go | 6 | ||||
-rw-r--r-- | models/repo_unit.go | 4 | ||||
-rw-r--r-- | models/ssh_key.go | 2 | ||||
-rw-r--r-- | models/test_fixtures.go | 3 | ||||
-rw-r--r-- | models/unit_tests.go | 4 |
13 files changed, 76 insertions, 64 deletions
diff --git a/models/access.go b/models/access.go index c509867062..4b72a752b2 100644 --- a/models/access.go +++ b/models/access.go @@ -207,7 +207,12 @@ func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]*userAcces // Delete old accesses and insert new ones for repository. if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil { return fmt.Errorf("delete old accesses: %v", err) - } else if _, err = e.Insert(newAccesses); err != nil { + } + if len(newAccesses) == 0 { + return nil + } + + if _, err = e.Insert(newAccesses); err != nil { return fmt.Errorf("insert new accesses: %v", err) } return nil diff --git a/models/list_options.go b/models/list_options.go index c49b83d1ff..0946917fec 100644 --- a/models/list_options.go +++ b/models/list_options.go @@ -25,6 +25,9 @@ func (opts ListOptions) getPaginatedSession() *xorm.Session { func (opts ListOptions) setSessionPagination(sess *xorm.Session) *xorm.Session { opts.setDefaultValues() + if opts.PageSize <= 0 { + return sess + } return sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) } diff --git a/models/log.go b/models/log.go index 4caea9a8b7..1cfddd90a1 100644 --- a/models/log.go +++ b/models/log.go @@ -9,7 +9,7 @@ import ( "code.gitea.io/gitea/modules/log" - "xorm.io/core" + xormlog "xorm.io/xorm/log" ) // XORMLogBridge a logger bridge from Logger to xorm @@ -19,7 +19,7 @@ type XORMLogBridge struct { } // NewXORMLogger inits a log bridge for xorm -func NewXORMLogger(showSQL bool) core.ILogger { +func NewXORMLogger(showSQL bool) xormlog.Logger { return &XORMLogBridge{ showSQL: showSQL, logger: log.GetLogger("xorm"), @@ -72,22 +72,22 @@ func (l *XORMLogBridge) Warnf(format string, v ...interface{}) { } // Level get logger level -func (l *XORMLogBridge) Level() core.LogLevel { +func (l *XORMLogBridge) Level() xormlog.LogLevel { switch l.logger.GetLevel() { case log.TRACE, log.DEBUG: - return core.LOG_DEBUG + return xormlog.LOG_DEBUG case log.INFO: - return core.LOG_INFO + return xormlog.LOG_INFO case log.WARN: - return core.LOG_WARNING + return xormlog.LOG_WARNING case log.ERROR, log.CRITICAL: - return core.LOG_ERR + return xormlog.LOG_ERR } - return core.LOG_OFF + return xormlog.LOG_OFF } // SetLevel set the logger level -func (l *XORMLogBridge) SetLevel(lvl core.LogLevel) { +func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) { } // ShowSQL set if record SQL diff --git a/models/login_source.go b/models/login_source.go index 88028283e8..5350446233 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -22,8 +22,8 @@ import ( "code.gitea.io/gitea/modules/timeutil" "github.com/unknwon/com" - "xorm.io/core" "xorm.io/xorm" + "xorm.io/xorm/convert" ) // LoginType represents an login type. @@ -60,11 +60,11 @@ var SecurityProtocolNames = map[ldap.SecurityProtocol]string{ // Ensure structs implemented interface. var ( - _ core.Conversion = &LDAPConfig{} - _ core.Conversion = &SMTPConfig{} - _ core.Conversion = &PAMConfig{} - _ core.Conversion = &OAuth2Config{} - _ core.Conversion = &SSPIConfig{} + _ convert.Conversion = &LDAPConfig{} + _ convert.Conversion = &SMTPConfig{} + _ convert.Conversion = &PAMConfig{} + _ convert.Conversion = &OAuth2Config{} + _ convert.Conversion = &SSPIConfig{} ) // LDAPConfig holds configuration for LDAP login source. @@ -165,10 +165,10 @@ func (cfg *SSPIConfig) ToDB() ([]byte, error) { type LoginSource struct { ID int64 `xorm:"pk autoincr"` Type LoginType - Name string `xorm:"UNIQUE"` - IsActived bool `xorm:"INDEX NOT NULL DEFAULT false"` - IsSyncEnabled bool `xorm:"INDEX NOT NULL DEFAULT false"` - Cfg core.Conversion `xorm:"TEXT"` + Name string `xorm:"UNIQUE"` + IsActived bool `xorm:"INDEX NOT NULL DEFAULT false"` + IsSyncEnabled bool `xorm:"INDEX NOT NULL DEFAULT false"` + Cfg convert.Conversion `xorm:"TEXT"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/models/migrations/v110.go b/models/migrations/v110.go index dc46c2ef77..4a1c3c47a7 100644 --- a/models/migrations/v110.go +++ b/models/migrations/v110.go @@ -5,32 +5,26 @@ package migrations import ( - "xorm.io/core" "xorm.io/xorm" + "xorm.io/xorm/schemas" ) func changeReviewContentToText(x *xorm.Engine) error { - - if x.Dialect().DBType() == core.MYSQL { + switch x.Dialect().URI().DBType { + case schemas.MYSQL: _, err := x.Exec("ALTER TABLE review MODIFY COLUMN content TEXT") return err - } - - if x.Dialect().DBType() == core.ORACLE { + case schemas.ORACLE: _, err := x.Exec("ALTER TABLE review MODIFY content TEXT") return err - } - - if x.Dialect().DBType() == core.MSSQL { + case schemas.MSSQL: _, err := x.Exec("ALTER TABLE review ALTER COLUMN content TEXT") return err - } - - if x.Dialect().DBType() == core.POSTGRES { + case schemas.POSTGRES: _, err := x.Exec("ALTER TABLE review ALTER COLUMN content TYPE TEXT") return err + default: + // SQLite doesn't support ALTER COLUMN, and it seem to already make String to _TEXT_ default so no migration needed + return nil } - - // SQLite doesn't support ALTER COLUMN, and it seem to already make String to _TEXT_ default so no migration needed - return nil } diff --git a/models/migrations/v81.go b/models/migrations/v81.go index 271d479a64..4e9e7658ee 100644 --- a/models/migrations/v81.go +++ b/models/migrations/v81.go @@ -8,17 +8,18 @@ import ( "fmt" "xorm.io/xorm" + "xorm.io/xorm/schemas" ) func changeU2FCounterType(x *xorm.Engine) error { var err error - switch x.Dialect().DriverName() { - case "mysql": + switch x.Dialect().URI().DBType { + case schemas.MYSQL: _, err = x.Exec("ALTER TABLE `u2f_registration` MODIFY `counter` BIGINT") - case "postgres": + case schemas.POSTGRES: _, err = x.Exec("ALTER TABLE `u2f_registration` ALTER COLUMN `counter` SET DATA TYPE bigint") - case "mssql": + case schemas.MSSQL: _, err = x.Exec("ALTER TABLE `u2f_registration` ALTER COLUMN `counter` BIGINT") } diff --git a/models/models.go b/models/models.go index 088445590f..08c7780317 100644 --- a/models/models.go +++ b/models/models.go @@ -15,8 +15,9 @@ import ( // Needed for the MySQL driver _ "github.com/go-sql-driver/mysql" - "xorm.io/core" "xorm.io/xorm" + "xorm.io/xorm/names" + "xorm.io/xorm/schemas" // Needed for the Postgresql driver _ "github.com/lib/pq" @@ -127,7 +128,7 @@ func init() { gonicNames := []string{"SSL", "UID"} for _, name := range gonicNames { - core.LintGonicMapper[name] = true + names.LintGonicMapper[name] = true } } @@ -152,8 +153,7 @@ func NewTestEngine(x *xorm.Engine) (err error) { return fmt.Errorf("Connect to database: %v", err) } - x.ShowExecTime(true) - x.SetMapper(core.GonicMapper{}) + x.SetMapper(names.GonicMapper{}) x.SetLogger(NewXORMLogger(!setting.ProdMode)) x.ShowSQL(!setting.ProdMode) return x.StoreEngine("InnoDB").Sync2(tables...) @@ -166,8 +166,7 @@ func SetEngine() (err error) { return fmt.Errorf("Failed to connect to database: %v", err) } - x.ShowExecTime(true) - x.SetMapper(core.GonicMapper{}) + x.SetMapper(names.GonicMapper{}) // WARNING: for serv command, MUST remove the output to os.stdout, // so use log file to instead print to stdout. x.SetLogger(NewXORMLogger(setting.Database.LogSQL)) @@ -249,21 +248,26 @@ func Ping() error { // DumpDatabase dumps all data from database according the special database SQL syntax to file system. func DumpDatabase(filePath string, dbType string) error { - var tbs []*core.Table + var tbs []*schemas.Table for _, t := range tables { - t := x.TableInfo(t) - t.Table.Name = t.Name - tbs = append(tbs, t.Table) + t, err := x.TableInfo(t) + if err != nil { + return err + } + tbs = append(tbs, t) } if len(dbType) > 0 { - return x.DumpTablesToFile(tbs, filePath, core.DbType(dbType)) + return x.DumpTablesToFile(tbs, filePath, schemas.DBType(dbType)) } return x.DumpTablesToFile(tbs, filePath) } // MaxBatchInsertSize returns the table's max batch insert size func MaxBatchInsertSize(bean interface{}) int { - t := x.TableInfo(bean) + t, err := x.TableInfo(bean) + if err != nil { + return 50 + } return 999 / len(t.ColumnsSeq()) } diff --git a/models/org_team.go b/models/org_team.go index f8013d12c6..82c27b2c06 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -1072,12 +1072,14 @@ func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) { return err } - if _, err = sess.Insert(units); err != nil { - errRollback := sess.Rollback() - if errRollback != nil { - log.Error("UpdateTeamUnits sess.Rollback: %v", errRollback) + if len(units) > 0 { + if _, err = sess.Insert(units); err != nil { + errRollback := sess.Rollback() + if errRollback != nil { + log.Error("UpdateTeamUnits sess.Rollback: %v", errRollback) + } + return err } - return err } return sess.Commit() diff --git a/models/repo.go b/models/repo.go index 672507819a..dd50db9a0b 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1417,8 +1417,10 @@ func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes [ return err } - if _, err = sess.Insert(units); err != nil { - return err + if len(units) > 0 { + if _, err = sess.Insert(units); err != nil { + return err + } } return sess.Commit() diff --git a/models/repo_unit.go b/models/repo_unit.go index ec680c395e..42ce8f6c8d 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -10,8 +10,8 @@ import ( "code.gitea.io/gitea/modules/timeutil" "github.com/unknwon/com" - "xorm.io/core" "xorm.io/xorm" + "xorm.io/xorm/convert" ) // RepoUnit describes all units of a repository @@ -19,7 +19,7 @@ type RepoUnit struct { ID int64 RepoID int64 `xorm:"INDEX(s)"` Type UnitType `xorm:"INDEX(s)"` - Config core.Conversion `xorm:"TEXT"` + Config convert.Conversion `xorm:"TEXT"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"` } diff --git a/models/ssh_key.go b/models/ssh_key.go index d3e9de5770..5d9f5afef8 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -524,7 +524,7 @@ func AddPublicKey(ownerID int64, name, content string, loginSourceID int64) (*Pu func GetPublicKeyByID(keyID int64) (*PublicKey, error) { key := new(PublicKey) has, err := x. - Id(keyID). + ID(keyID). Get(key) if err != nil { return nil, err diff --git a/models/test_fixtures.go b/models/test_fixtures.go index fe6a790b0d..6c160742bd 100644 --- a/models/test_fixtures.go +++ b/models/test_fixtures.go @@ -9,6 +9,7 @@ import ( "time" "gopkg.in/testfixtures.v2" + "xorm.io/xorm/schemas" ) var fixtures *testfixtures.Context @@ -36,7 +37,7 @@ func LoadFixtures() error { fmt.Printf("LoadFixtures failed after retries: %v\n", err) } // Now if we're running postgres we need to tell it to update the sequences - if x.Dialect().DriverName() == "postgres" { + if x.Dialect().URI().DBType == schemas.POSTGRES { results, err := x.QueryString(`SELECT 'SELECT SETVAL(' || quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) || ', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' || diff --git a/models/unit_tests.go b/models/unit_tests.go index b2c487ddcd..1b27eebcd0 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -20,8 +20,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/unknwon/com" "gopkg.in/testfixtures.v2" - "xorm.io/core" "xorm.io/xorm" + "xorm.io/xorm/names" ) // NonexistentID an ID that will never exist @@ -92,7 +92,7 @@ func CreateTestEngine(fixturesDir string) error { if err != nil { return err } - x.SetMapper(core.GonicMapper{}) + x.SetMapper(names.GonicMapper{}) if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil { return err } |