d28f005552
Fix limit for databases other than sqlite go mod tidy && go mod vendor Remove unneeded break statements Make everything work with the new xorm version Fix xorm logging Fix lint Fix redis init Fix using id field Fix database init for testing Change default database log level Add xorm logger Use const for postgres go mod tidy Merge branch 'master' into update/xorm # Conflicts: # go.mod # go.sum # vendor/modules.txt go mod vendor Fix loading fixtures for postgres Go mod vendor1 Update xorm to version 1 Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/323
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
// Copyright 2019 The Xorm Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package schemas
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// enumerate all index types
|
|
const (
|
|
IndexType = iota + 1
|
|
UniqueType
|
|
)
|
|
|
|
// Index represents a database index
|
|
type Index struct {
|
|
IsRegular bool
|
|
Name string
|
|
Type int
|
|
Cols []string
|
|
}
|
|
|
|
// NewIndex new an index object
|
|
func NewIndex(name string, indexType int) *Index {
|
|
return &Index{true, name, indexType, make([]string, 0)}
|
|
}
|
|
|
|
func (index *Index) XName(tableName string) string {
|
|
if !strings.HasPrefix(index.Name, "UQE_") &&
|
|
!strings.HasPrefix(index.Name, "IDX_") {
|
|
tableParts := strings.Split(strings.Replace(tableName, `"`, "", -1), ".")
|
|
tableName = tableParts[len(tableParts)-1]
|
|
if index.Type == UniqueType {
|
|
return fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
|
|
}
|
|
return fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
|
|
}
|
|
return index.Name
|
|
}
|
|
|
|
// AddColumn add columns which will be composite index
|
|
func (index *Index) AddColumn(cols ...string) {
|
|
for _, col := range cols {
|
|
index.Cols = append(index.Cols, col)
|
|
}
|
|
}
|
|
|
|
func (index *Index) Equal(dst *Index) bool {
|
|
if index.Type != dst.Type {
|
|
return false
|
|
}
|
|
if len(index.Cols) != len(dst.Cols) {
|
|
return false
|
|
}
|
|
|
|
for i := 0; i < len(index.Cols); i++ {
|
|
var found bool
|
|
for j := 0; j < len(dst.Cols); j++ {
|
|
if index.Cols[i] == dst.Cols[j] {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|