Skip to content

Upgrade xorm to v1.0.0 #10646

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

Merged
merged 14 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ require (
gopkg.in/testfixtures.v2 v2.5.0
mvdan.cc/xurls/v2 v2.1.0
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
xorm.io/builder v0.3.6
xorm.io/core v0.7.3
xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a
xorm.io/builder v0.3.7
xorm.io/xorm v1.0.0
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ gitea.com/macaron/session v0.0.0-20191207215012-613cebf0674d h1:XLww3CvnFZkXVwau
gitea.com/macaron/session v0.0.0-20191207215012-613cebf0674d/go.mod h1:FanKy3WjWb5iw/iZBPk4ggoQT9FcM6bkBPvmDmsH6tY=
gitea.com/macaron/toolbox v0.0.0-20190822013122-05ff0fc766b7 h1:N9QFoeNsUXLhl14mefLzGluqV7w2mGU3u+iZU+jCeWk=
gitea.com/macaron/toolbox v0.0.0-20190822013122-05ff0fc766b7/go.mod h1:kgsbFPPS4P+acDYDOPDa3N4IWWOuDJt5/INKRUz7aks=
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s=
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down Expand Up @@ -793,11 +795,11 @@ strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 h1:mUcz5b3
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251/go.mod h1:FJGmPh3vz9jSos1L/F91iAgnC/aejc0wIIrF2ZwJxdY=
xorm.io/builder v0.3.6 h1:ha28mQ2M+TFx96Hxo+iq6tQgnkC9IZkM6D8w9sKHHF8=
xorm.io/builder v0.3.6/go.mod h1:LEFAPISnRzG+zxaxj2vPicRwz67BdhFreKg8yv8/TgU=
xorm.io/builder v0.3.7 h1:2pETdKRK+2QG4mLX4oODHEhn5Z8j1m8sXa7jfu+/SZI=
xorm.io/builder v0.3.7/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
xorm.io/core v0.7.2 h1:mEO22A2Z7a3fPaZMk6gKL/jMD80iiyNwRrX5HOv3XLw=
xorm.io/core v0.7.2/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM=
xorm.io/core v0.7.3 h1:W8ws1PlrnkS1CZU1YWaYLMQcQilwAmQXU0BJDJon+H0=
xorm.io/core v0.7.3/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM=
xorm.io/xorm v0.8.0 h1:iALxgJrX8O00f8Jk22GbZwPmxJNgssV5Mv4uc2HL9PM=
xorm.io/xorm v0.8.0/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY=
xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a h1:hzGd080rlkZ5a7v6Tr3x8PJJnWPfKxGMMl92c8DNcww=
xorm.io/xorm v0.8.2-0.20200120024500-c37aff9b3a4a/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY=
xorm.io/xorm v1.0.0 h1:ceiwUTrJHqfNFxIUcWjkcbz6kt7sINf2dOXlgLLhaQM=
xorm.io/xorm v1.0.0/go.mod h1:o4vnEsQ5V2F1/WK6w4XTwmiWJeGj82tqjAnHe44wVHY=
7 changes: 6 additions & 1 deletion models/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions models/list_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 9 additions & 9 deletions models/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
Expand Down
24 changes: 9 additions & 15 deletions models/migrations/v110.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions models/migrations/v81.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
28 changes: 16 additions & 12 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -127,7 +128,7 @@ func init() {

gonicNames := []string{"SSL", "UID"}
for _, name := range gonicNames {
core.LintGonicMapper[name] = true
names.LintGonicMapper[name] = true
}
}

Expand All @@ -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...)
Expand All @@ -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))
Expand Down Expand Up @@ -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())
}

Expand Down
12 changes: 7 additions & 5 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ 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
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"`
}

Expand Down
2 changes: 1 addition & 1 deletion models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion models/test_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"gopkg.in/testfixtures.v2"
"xorm.io/xorm/schemas"
)

var fixtures *testfixtures.Context
Expand Down Expand Up @@ -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 ' ||
Expand Down
4 changes: 2 additions & 2 deletions models/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Loading