Browse Source

Automatically regenerate indexer for incompatible versions (#2524)

Ethan Koenig 2 years ago
parent
commit
ccff57103a
1 changed files with 18 additions and 19 deletions
  1. 18 19
      modules/indexer/issue.go

+ 18 - 19
modules/indexer/issue.go

@@ -15,6 +15,7 @@ import (
15 15
 	"github.com/blevesearch/bleve/analysis/token/lowercase"
16 16
 	"github.com/blevesearch/bleve/analysis/token/unicodenorm"
17 17
 	"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
18
+	"github.com/blevesearch/bleve/index/upsidedown"
18 19
 )
19 20
 
20 21
 // issueIndexer (thread-safe) index for searching issues
@@ -39,27 +40,25 @@ const issueIndexerAnalyzer = "issueIndexer"
39 40
 // InitIssueIndexer initialize issue indexer
40 41
 func InitIssueIndexer(populateIndexer func() error) {
41 42
 	_, err := os.Stat(setting.Indexer.IssuePath)
42
-	if err != nil {
43
-		if os.IsNotExist(err) {
44
-			if err = createIssueIndexer(); err != nil {
45
-				log.Fatal(4, "CreateIssuesIndexer: %v", err)
46
-			}
47
-			if err = populateIndexer(); err != nil {
48
-				log.Fatal(4, "PopulateIssuesIndex: %v", err)
49
-			}
50
-		} else {
51
-			log.Fatal(4, "InitIssuesIndexer: %v", err)
52
-		}
53
-	} else {
43
+	if err != nil && !os.IsNotExist(err) {
44
+		log.Fatal(4, "InitIssueIndexer: %v", err)
45
+	} else if err == nil {
54 46
 		issueIndexer, err = bleve.Open(setting.Indexer.IssuePath)
55
-		if err != nil {
56
-			log.Error(4, "Unable to open issues indexer (%s)."+
57
-				" If the error is due to incompatible versions, try deleting the indexer files;"+
58
-				" gitea will recreate them with the appropriate version the next time it runs."+
59
-				" Deleting the indexer files will not result in loss of data.",
60
-				setting.Indexer.IssuePath)
61
-			log.Fatal(4, "InitIssuesIndexer, open index: %v", err)
47
+		if err == nil {
48
+			return
49
+		} else if err != upsidedown.IncompatibleVersion {
50
+			log.Fatal(4, "InitIssueIndexer, open index: %v", err)
62 51
 		}
52
+		log.Warn("Incompatible bleve version, deleting and recreating issue indexer")
53
+		if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil {
54
+			log.Fatal(4, "InitIssueIndexer: remove index, %v", err)
55
+		}
56
+	}
57
+	if err = createIssueIndexer(); err != nil {
58
+		log.Fatal(4, "InitIssuesIndexer: create index, %v", err)
59
+	}
60
+	if err = populateIndexer(); err != nil {
61
+		log.Fatal(4, "InitIssueIndexer: populate index, %v", err)
63 62
 	}
64 63
 }
65 64