MCPcopy
hub / github.com/pocketbase/pocketbase / AddIndex

Method AddIndex

core/collection_model.go:648–671  ·  view source on GitHub ↗

AddIndex adds a new index into the current collection. If the collection has an existing index matching the new name it will be replaced with the new one.

(name string, unique bool, columnsExpr string, optWhereExpr string)

Source from the content-addressed store, hash-verified

646//
647// If the collection has an existing index matching the new name it will be replaced with the new one.
648func (m *Collection) AddIndex(name string, unique bool, columnsExpr string, optWhereExpr string) {
649 m.RemoveIndex(name)
650
651 var idx strings.Builder
652
653 idx.WriteString("CREATE ")
654 if unique {
655 idx.WriteString("UNIQUE ")
656 }
657 idx.WriteString("INDEX `")
658 idx.WriteString(name)
659 idx.WriteString("` ")
660 idx.WriteString("ON `")
661 idx.WriteString(m.Name)
662 idx.WriteString("` (")
663 idx.WriteString(columnsExpr)
664 idx.WriteString(")")
665 if optWhereExpr != "" {
666 idx.WriteString(" WHERE ")
667 idx.WriteString(optWhereExpr)
668 }
669
670 m.Indexes = append(m.Indexes, idx.String())
671}
672
673// RemoveIndex removes a single index with the specified name from the current collection.
674func (m *Collection) RemoveIndex(name string) {

Callers 11

TestCollectionValidateFunction · 0.80
TestCollectionSaveModelFunction · 0.80
TestCollectionCreateFunction · 0.80
TestCollectionUpdateFunction · 0.80
createMFAsCollectionFunction · 0.80
createOTPsCollectionFunction · 0.80

Calls 2

RemoveIndexMethod · 0.95
StringMethod · 0.45

Tested by 7

TestCollectionValidateFunction · 0.64
TestCollectionSaveModelFunction · 0.64
TestCollectionCreateFunction · 0.64
TestCollectionUpdateFunction · 0.64