input defs: Throw an error if pirmary key is not given instead of adding it auto.

This commit is contained in:
Yuce Tekol 2017-11-11 00:49:37 +03:00
parent 83be24f4d2
commit 7a5459da73
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573
4 changed files with 46 additions and 22 deletions

View file

@ -43,6 +43,9 @@ curl localhost:10101/index/repository/input-definition/stargazer \
}
],
"fields": [
{
"primaryKey": true
},
{
"actions": [
{

View file

@ -1309,6 +1309,43 @@ func TestHandler_DuplicatePrimaryKey(t *testing.T) {
t.Fatalf("unexpected body: %s", body)
}
// Ensure throwing error if there's no primary key
hldr.MustCreateIndexIfNotExists("i1", pilosa.IndexOptions{ColumnLabel: "id"})
unmatchColumnBody := []byte(`
{
"frames":[{
"name":"event-time",
"options":{
"timeQuantum": "YMD",
"inverseEnabled": false,
"cacheType": "ranked"
}
}],
"fields": [
{
"name": "foo",
"actions": [
{
"frame": "cab-type",
"valueDestination": "mapping",
"valueMap": {
"Green": 1,
"Yellow": 2
}
}
]
}
]
}`)
w = httptest.NewRecorder()
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", "/index/i1/input-definition/input1", bytes.NewBuffer(unmatchColumnBody)))
if w.Code != http.StatusBadRequest {
t.Fatalf("unexpected status code: %d", w.Code)
} else if body := w.Body.String(); body != pilosa.ErrInputDefinitionHasPrimaryKey.Error()+"\n" {
t.Fatalf("unexpected body: %s", body)
}
// Eusure throwing error if request body is invalid.
jsonErrorBody := []byte(`
{

View file

@ -131,9 +131,7 @@ func (i *InputDefinition) LoadDefinition(pb *internal.InputDefinition) error {
}
if len(pb.Fields) > 0 && !primaryKeyGiven {
// primary field is required if there are other fields.
// add it if it doesn't exist.
i.fields = append(i.fields, InputDefDefaultPrimaryKeyField())
return ErrInputDefinitionHasPrimaryKey
}
return nil
@ -335,16 +333,7 @@ func (i *InputDefinitionInfo) Encode() *internal.InputDefinition {
for _, f := range i.Frames {
def.Frames = append(def.Frames, f.Encode())
}
primaryKeyGiven := false
for _, f := range i.Fields {
if f.PrimaryKey {
f.Name = DefaultColumnLabel
primaryKeyGiven = true
}
def.Fields = append(def.Fields, f.Encode())
}
if len(i.Fields) > 0 && !primaryKeyGiven {
f := InputDefDefaultPrimaryKeyField()
def.Fields = append(def.Fields, f.Encode())
}
return &def
@ -402,11 +391,3 @@ func HandleAction(a Action, value interface{}, colID uint64, timestamp int64) (*
}
return &bit, err
}
func InputDefDefaultPrimaryKeyField() InputDefinitionField {
return InputDefinitionField{
Name: DefaultColumnLabel,
PrimaryKey: true,
Actions: []Action{},
}
}

View file

@ -63,6 +63,9 @@ func TestInputDefinition_Encoding(t *testing.T) {
}
}],
"fields": [
{
"primaryKey": true
},
{
"name": "cabType",
"actions": [
@ -92,9 +95,9 @@ func TestInputDefinition_Encoding(t *testing.T) {
t.Fatalf("unexpected frame meta data: %v", internalDef)
} else if len(internalDef.Fields) != 2 {
t.Fatalf("unexpected number of Fields: %d", len(internalDef.Fields))
} else if len(internalDef.Fields[0].InputDefinitionActions) != 1 {
} else if len(internalDef.Fields[1].InputDefinitionActions) != 1 {
t.Fatalf("unexpected number of Actions: %v", internalDef.Fields[1].InputDefinitionActions)
} else if internalDef.Fields[0].InputDefinitionActions[0].ValueDestination != "mapping" {
} else if internalDef.Fields[1].InputDefinitionActions[0].ValueDestination != "mapping" {
t.Fatalf("unexpected ValueDestination: %v", internalDef.Fields[1].InputDefinitionActions[0])
}
}