From 43a14306dc16b6442da56f2588791a7d8830b6d9 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Fri, 14 Apr 2017 01:19:24 +0300 Subject: [PATCH] Create fulltext index on commit.message --- models/commit.go | 7 +------ models/models.go | 2 +- vendor/github.com/go-xorm/core/dialect.go | 2 ++ vendor/github.com/go-xorm/core/index.go | 1 + vendor/github.com/go-xorm/xorm/engine.go | 9 ++++++++- .../go-xorm/xorm/postgres_dialect.go | 18 +++++++++++++++++- vendor/github.com/go-xorm/xorm/statement.go | 8 +++----- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/models/commit.go b/models/commit.go index 9701c1be..87e427e8 100644 --- a/models/commit.go +++ b/models/commit.go @@ -12,7 +12,7 @@ type Commit struct { ID int64 RepoID int64 `xorm:"UNIQUE(commit_repo_sha)"` Sha string `xorm:"VARCHAR(40) UNIQUE(commit_repo_sha)"` - Message string + Message string `xorm:"TEXT FULLTEXT(commit_message)"` AuthorEmail string AuthorName string AuthorTime time.Time @@ -20,8 +20,3 @@ type Commit struct { CommitterName string CommitterTime time.Time } - -type CommitParent struct { - ParentId int64 `xorm:"pk"` - ChildId int64 `xorm:"pk"` -} diff --git a/models/models.go b/models/models.go index ab3ccf19..50721071 100644 --- a/models/models.go +++ b/models/models.go @@ -72,7 +72,7 @@ func init() { new(Mirror), new(Release), new(LoginSource), new(Webhook), new(HookTask), new(ProtectBranch), new(ProtectBranchWhitelist), new(Team), new(OrgUser), new(TeamUser), new(TeamRepo), - new(Notice), new(EmailAddress), new(Commit), new(CommitParent)) + new(Notice), new(EmailAddress), new(Commit)) gonicNames := []string{"SSL"} for _, name := range gonicNames { diff --git a/vendor/github.com/go-xorm/core/dialect.go b/vendor/github.com/go-xorm/core/dialect.go index 70420ee5..e8d1f01c 100644 --- a/vendor/github.com/go-xorm/core/dialect.go +++ b/vendor/github.com/go-xorm/core/dialect.go @@ -201,6 +201,8 @@ func (db *Base) CreateIndexSql(tableName string, index *Index) string { var idxName string if index.Type == UniqueType { unique = " UNIQUE" + } else if index.Type == FulltextType { + unique = " FULLTEXT" } idxName = index.XName(tableName) return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique, diff --git a/vendor/github.com/go-xorm/core/index.go b/vendor/github.com/go-xorm/core/index.go index 73b95175..29bfbc4f 100644 --- a/vendor/github.com/go-xorm/core/index.go +++ b/vendor/github.com/go-xorm/core/index.go @@ -9,6 +9,7 @@ import ( const ( IndexType = iota + 1 UniqueType + FulltextType ) // database index diff --git a/vendor/github.com/go-xorm/xorm/engine.go b/vendor/github.com/go-xorm/xorm/engine.go index be90ddbe..33284986 100644 --- a/vendor/github.com/go-xorm/xorm/engine.go +++ b/vendor/github.com/go-xorm/xorm/engine.go @@ -909,7 +909,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { } indexNames := make(map[string]int) - var isIndex, isUnique bool + var isIndex, isUnique, isFulltext bool var preKey string for j, key := range tags { k := strings.ToUpper(key) @@ -971,6 +971,11 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { indexNames[indexName] = core.UniqueType case k == "UNIQUE": isUnique = true + case strings.HasPrefix(k, "FULLTEXT(") && strings.HasSuffix(k, ")"): + indexName := k[len("FULLTEXT")+1 : len(k)-1] + indexNames[indexName] = core.FulltextType + case k == "FULLTEXT": + isFulltext = true case k == "NOTNULL": col.Nullable = false case k == "CACHE": @@ -1056,6 +1061,8 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table { if isUnique { indexNames[col.Name] = core.UniqueType + } else if isFulltext { + indexNames[col.Name] = core.FulltextType } else if isIndex { indexNames[col.Name] = core.IndexType } diff --git a/vendor/github.com/go-xorm/xorm/postgres_dialect.go b/vendor/github.com/go-xorm/xorm/postgres_dialect.go index c23ab6f3..af124b0a 100644 --- a/vendor/github.com/go-xorm/xorm/postgres_dialect.go +++ b/vendor/github.com/go-xorm/xorm/postgres_dialect.go @@ -879,6 +879,15 @@ func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) { " AND column_name = ?", args }*/ +func (db *postgres) CreateIndexSql(tableName string, index *core.Index) string { + if index.Type == core.FulltextType { + idxName := index.XName(tableName) + return fmt.Sprintf("CREATE INDEX %v ON %v USING GIN (to_tsvector(%v))", + db.Quote(idxName), db.Quote(tableName), db.Quote(strings.Join(index.Cols, db.Quote("||' '||")))) + } + return db.Base.CreateIndexSql(tableName, index) +} + func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string { return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s", tableName, col.Name, db.SqlType(col)) @@ -1073,9 +1082,16 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) } else { indexType = core.IndexType } - cs := strings.Split(indexdef, "(") + cs := strings.SplitN(indexdef, "(", 2) colNames = strings.Split(cs[1][0:len(cs[1])-1], ",") + for i, colName := range colNames { + if strings.HasPrefix(colName, "to_tsvector(") { + colNames[i] = colName[len("to_tsvector("):len(colName)-1] + indexType = core.FulltextType + } + } + if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) { newIdxName := indexName[5+len(tableName):] if newIdxName != "" { diff --git a/vendor/github.com/go-xorm/xorm/statement.go b/vendor/github.com/go-xorm/xorm/statement.go index fb116b94..5a5183c8 100644 --- a/vendor/github.com/go-xorm/xorm/statement.go +++ b/vendor/github.com/go-xorm/xorm/statement.go @@ -1046,11 +1046,9 @@ func (statement *Statement) genCreateTableSQL() string { func (statement *Statement) genIndexSQL() []string { var sqls []string tbName := statement.TableName() - quote := statement.Engine.Quote - for idxName, index := range statement.RefTable.Indexes { - if index.Type == core.IndexType { - sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)), - quote(tbName), quote(strings.Join(index.Cols, quote(",")))) + for _, index := range statement.RefTable.Indexes { + if index.Type == core.IndexType || index.Type == core.FulltextType { + sql := statement.Engine.dialect.CreateIndexSql(tbName, index) sqls = append(sqls, sql) } }