CreateIndex create index `name`
(value interface{}, name string)
| 833 | |
| 834 | // CreateIndex create index `name` |
| 835 | func (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` |
| 870 | func (m Migrator) DropIndex(value interface{}, name string) error { |
nothing calls this directly
no test coverage detected