in webv2/api/rules.go [122:229]
func DropRule(w http.ResponseWriter, r *http.Request) {
ruleId := r.FormValue("id")
if ruleId == "" {
http.Error(w, fmt.Sprint("Rule id is empty"), http.StatusBadRequest)
return
}
sessionState := session.GetSessionState()
sessionState.Conv.ConvLock.Lock()
defer sessionState.Conv.ConvLock.Unlock()
conv := sessionState.Conv
var rule internal.Rule
position := -1
for i, r := range conv.Rules {
if r.Id == ruleId {
rule = r
position = i
break
}
}
if position == -1 {
http.Error(w, fmt.Sprint("Rule to be deleted not found"), http.StatusBadRequest)
return
}
if rule.Type == constants.AddIndex {
if rule.Enabled {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
var index ddl.CreateIndex
err = json.Unmarshal(d, &index)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
tableId := index.TableId
indexId := index.Id
err = dropSecondaryIndexHelper(tableId, indexId)
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
}
} else if rule.Type == constants.GlobalDataTypeChange {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
typeMap := map[string]string{}
err = json.Unmarshal(d, &typeMap)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
revertGlobalDataType(typeMap)
} else if rule.Type == constants.EditColumnMaxLength {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
var colMaxLength types.ColMaxLength
err = json.Unmarshal(d, &colMaxLength)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
revertSpColMaxLength(colMaxLength, rule.AssociatedObjects)
} else if rule.Type == constants.AddShardIdPrimaryKey {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
var shardIdPrimaryKey types.ShardIdPrimaryKey
err = json.Unmarshal(d, &shardIdPrimaryKey)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
tableName := checkInterleaving()
if tableName != "" {
http.Error(w, fmt.Sprintf("Rule cannot be deleted because some tables, eg: %v are interleaved. Please remove interleaving and try again.", tableName), http.StatusBadRequest)
return
}
revertShardIdColumnAsPrimaryKey(shardIdPrimaryKey.AddedAtTheStart)
removeShardIdColumnFromForeignKeys(shardIdPrimaryKey.AddedAtTheStart)
} else {
http.Error(w, "Invalid rule type", http.StatusInternalServerError)
return
}
sessionState.Conv.Rules = append(conv.Rules[:position], conv.Rules[position+1:]...)
if len(sessionState.Conv.Rules) == 0 {
sessionState.Conv.Rules = nil
}
session.UpdateSessionFile()
convm := session.ConvWithMetadata{
SessionMetadata: sessionState.SessionMetadata,
Conv: *sessionState.Conv,
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(convm)
}