improved sync indexes, now check columns;xorm tool bug fiexed
This commit is contained in:
parent
bab16dc763
commit
be22a978d8
|
@ -650,7 +650,8 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
if index.Type == UniqueType {
|
if index.Type == UniqueType {
|
||||||
isExist, err := session.isIndexExist(table.Name, name, true)
|
//isExist, err := session.isIndexExist(table.Name, name, true)
|
||||||
|
isExist, err := session.isIndexExist2(table.Name, index.Cols, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -664,7 +665,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if index.Type == IndexType {
|
} else if index.Type == IndexType {
|
||||||
isExist, err := session.isIndexExist(table.Name, name, false)
|
isExist, err := session.isIndexExist2(table.Name, index.Cols, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"xorm"
|
"xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SyncUser struct {
|
type SyncUser2 struct {
|
||||||
Id int64
|
Id int64
|
||||||
Name string `xorm:"unique"`
|
Name string `xorm:"unique"`
|
||||||
Age int `xorm:"index"`
|
Age int `xorm:"index"`
|
||||||
|
@ -19,7 +19,7 @@ type SyncUser struct {
|
||||||
Date int
|
Date int
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyncLoginInfo struct {
|
type SyncLoginInfo2 struct {
|
||||||
Id int64
|
Id int64
|
||||||
IP string `xorm:"index"`
|
IP string `xorm:"index"`
|
||||||
UserId int64
|
UserId int64
|
||||||
|
@ -32,7 +32,7 @@ type SyncLoginInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func sync(engine *xorm.Engine) error {
|
func sync(engine *xorm.Engine) error {
|
||||||
return engine.Sync(&SyncLoginInfo{}, &SyncUser{})
|
return engine.Sync(&SyncLoginInfo2{}, &SyncUser2{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func sqliteEngine() (*xorm.Engine, error) {
|
func sqliteEngine() (*xorm.Engine, error) {
|
||||||
|
@ -53,7 +53,10 @@ func postgresEngine() (*xorm.Engine, error) {
|
||||||
type engineFunc func() (*xorm.Engine, error)
|
type engineFunc func() (*xorm.Engine, error)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
|
//engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
|
||||||
|
//engines := []engineFunc{sqliteEngine}
|
||||||
|
//engines := []engineFunc{mysqlEngine}
|
||||||
|
engines := []engineFunc{postgresEngine}
|
||||||
for _, enginefunc := range engines {
|
for _, enginefunc := range engines {
|
||||||
Orm, err := enginefunc()
|
Orm, err := enginefunc()
|
||||||
fmt.Println("--------", Orm.DriverName, "----------")
|
fmt.Println("--------", Orm.DriverName, "----------")
|
||||||
|
@ -67,7 +70,12 @@ func main() {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
user := &SyncUser{
|
_, err = Orm.Where("id > 0").Delete(&SyncUser2{})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &SyncUser2{
|
||||||
Name: "testsdf",
|
Name: "testsdf",
|
||||||
Age: 15,
|
Age: 15,
|
||||||
Title: "newsfds",
|
Title: "newsfds",
|
||||||
|
|
2
mysql.go
2
mysql.go
|
@ -276,7 +276,7 @@ func (db *mysql) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
for name, content := range record {
|
for name, content := range record {
|
||||||
switch name {
|
switch name {
|
||||||
case "NON_UNIQUE":
|
case "NON_UNIQUE":
|
||||||
if "YES" == string(content) {
|
if "YES" == string(content) || string(content) == "1" {
|
||||||
indexType = IndexType
|
indexType = IndexType
|
||||||
} else {
|
} else {
|
||||||
indexType = UniqueType
|
indexType = UniqueType
|
||||||
|
|
|
@ -282,7 +282,10 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||||
indexName = indexName[5+len(tableName) : len(indexName)]
|
newIdxName := indexName[5+len(tableName) : len(indexName)]
|
||||||
|
if newIdxName != "" {
|
||||||
|
indexName = newIdxName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
index := &Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}
|
index := &Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}
|
||||||
|
|
37
session.go
37
session.go
|
@ -982,6 +982,43 @@ func (session *Session) isIndexExist(tableName, idxName string, unique bool) (bo
|
||||||
return len(results) > 0, err
|
return len(results) > 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sliceEq(left, right []string) bool {
|
||||||
|
for _, l := range left {
|
||||||
|
var find bool
|
||||||
|
for _, r := range right {
|
||||||
|
if l == r {
|
||||||
|
find = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !find {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// find if index is exist according cols
|
||||||
|
func (session *Session) isIndexExist2(tableName string, cols []string, unique bool) (bool, error) {
|
||||||
|
indexes, err := session.Engine.dialect.GetIndexes(tableName)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, index := range indexes {
|
||||||
|
//fmt.Println(i, "new:", cols, "-old:", index.Cols)
|
||||||
|
if sliceEq(index.Cols, cols) {
|
||||||
|
if unique {
|
||||||
|
return index.Type == UniqueType, nil
|
||||||
|
} else {
|
||||||
|
return index.Type == IndexType, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (session *Session) addColumn(colName string) error {
|
func (session *Session) addColumn(colName string) error {
|
||||||
err := session.newDb()
|
err := session.newDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -230,7 +230,7 @@ func tag(table *xorm.Table, col *xorm.Column) string {
|
||||||
} else if index.Type == xorm.IndexType {
|
} else if index.Type == xorm.IndexType {
|
||||||
uistr = "index"
|
uistr = "index"
|
||||||
}
|
}
|
||||||
if index.Name != col.Name {
|
if len(index.Cols) > 1 {
|
||||||
uistr += "(" + index.Name + ")"
|
uistr += "(" + index.Name + ")"
|
||||||
}
|
}
|
||||||
res = append(res, uistr)
|
res = append(res, uistr)
|
||||||
|
|
Loading…
Reference in New Issue