MCPcopy
hub / github.com/go-gorm/gorm / CreateIndex

Method CreateIndex

migrator/migrator.go:835–867  ·  view source on GitHub ↗

CreateIndex create index `name`

(value interface{}, name string)

Source from the content-addressed store, hash-verified

833
834// CreateIndex create index `name`
835func (m Migrator) CreateIndex(value interface{}, name string) error {
836 return m.RunWithValue(value, func(stmt *gorm.Statement) error {
837 if stmt.Schema == nil {
838 return errors.New("failed to get schema")
839 }
840 if idx := stmt.Schema.LookIndex(name); idx != nil {
841 opts := m.DB.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt)
842 values := []interface{}{clause.Column{Name: idx.Name}, m.CurrentTable(stmt), opts}
843
844 createIndexSQL := "CREATE "
845 if idx.Class != "" {
846 createIndexSQL += idx.Class + " "
847 }
848 createIndexSQL += "INDEX ? ON ??"
849
850 if idx.Type != "" {
851 createIndexSQL += " USING " + idx.Type
852 }
853
854 if idx.Comment != "" {
855 createIndexSQL += fmt.Sprintf(" COMMENT '%s'", idx.Comment)
856 }
857
858 if idx.Option != "" {
859 createIndexSQL += " " + idx.Option
860 }
861
862 return m.DB.Exec(createIndexSQL, values...).Error
863 }
864
865 return fmt.Errorf("failed to create index with name %s", name)
866 })
867}
868
869// DropIndex drop index `name`
870func (m Migrator) DropIndex(value interface{}, name string) error {

Callers

nothing calls this directly

Implementers 1

Migratormigrator/migrator.go

Calls 7

RunWithValueMethod · 0.95
CurrentTableMethod · 0.95
LookIndexMethod · 0.80
NewMethod · 0.65
BuildIndexOptionsMethod · 0.65
MigratorMethod · 0.65
ExecMethod · 0.65

Tested by

no test coverage detected