Skip to content

Commit

Permalink
PR FIXUP - Validate all definitions outside of loop
Browse files Browse the repository at this point in the history
Sorry, I spotted late that all the definitions were being validated on ever iteration
  • Loading branch information
AndrewSisley committed Jun 14, 2024
1 parent 6196482 commit 603b016
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions internal/db/collection_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ func (db *db) createCollections(
newDefinitions[i].Schema = schema
}

for _, def := range newDefinitions {
for i, def := range newDefinitions {
if len(def.Description.Fields) == 0 {
// This is a schema-only definition, we should not create a collection for it
returnDescriptions = append(returnDescriptions, def)
continue
}

Expand All @@ -76,11 +75,10 @@ func (db *db) createCollections(
return nil, err

Check warning on line 75 in internal/db/collection_define.go

View check run for this annotation

Codecov / codecov/patch

internal/db/collection_define.go#L75

Added line #L75 was not covered by tests
}

desc := def.Description
desc.ID = uint32(colID)
desc.RootID = desc.ID
newDefinitions[i].Description.ID = uint32(colID)
newDefinitions[i].Description.RootID = newDefinitions[i].Description.ID

for _, localField := range desc.Fields {
for _, localField := range def.Description.Fields {
var fieldID uint64
if localField.Name == request.DocIDFieldName {
// There is no hard technical requirement for this, we just think it looks nicer
Expand All @@ -94,24 +92,38 @@ func (db *db) createCollections(
}
}

for i := range desc.Fields {
if desc.Fields[i].Name == localField.Name {
desc.Fields[i].ID = client.FieldID(fieldID)
for j := range def.Description.Fields {
if def.Description.Fields[j].Name == localField.Name {
newDefinitions[i].Description.Fields[j].ID = client.FieldID(fieldID)
break
}
}
}
}

err = db.validateNewCollection(
ctx,
append(newDefinitions, existingDefinitions...),
existingDefinitions,
)
if err != nil {
return nil, err
err = db.validateNewCollection(
ctx,
append(
append(
[]client.CollectionDefinition{},
newDefinitions...,
),
existingDefinitions...,
),
existingDefinitions,
)
if err != nil {
return nil, err
}

for _, def := range newDefinitions {
if len(def.Description.Fields) == 0 {
// This is a schema-only definition, we should not create a collection for it
returnDescriptions = append(returnDescriptions, def)
continue
}

desc, err = description.SaveCollection(ctx, txn, desc)
desc, err := description.SaveCollection(ctx, txn, def.Description)
if err != nil {
return nil, err

Check warning on line 128 in internal/db/collection_define.go

View check run for this annotation

Codecov / codecov/patch

internal/db/collection_define.go#L128

Added line #L128 was not covered by tests
}
Expand Down

0 comments on commit 603b016

Please sign in to comment.