Skip to content

Commit

Permalink
Replace validateUpdateSchema with areSchemasEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jun 14, 2024
1 parent f492cac commit ea8c8c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 63 deletions.
5 changes: 0 additions & 5 deletions internal/db/collection_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ func (db *db) createCollections(
schemaByName[newDefinition.Schema.Name] = newDefinition.Schema
}

_, err = validateUpdateSchemaFields(schemaByName, client.SchemaDescription{}, schema)
if err != nil {
return nil, err
}

schema, err = description.CreateSchemaVersion(ctx, txn, schema)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

internal/db/collection_define.go#L53

Added line #L53 was not covered by tests
Expand Down
45 changes: 0 additions & 45 deletions internal/db/definition_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,51 +699,6 @@ func validateCollectionDefinitionPolicyDesc(
return nil
}

// validateUpdateSchema validates that the given schema description is a valid update.
//
// Will return true if the given description differs from the current persisted state of the
// schema. Will return an error if it fails validation.
func (db *db) validateUpdateSchema(
existingDescriptionsByName map[string]client.SchemaDescription,
proposedDescriptionsByName map[string]client.SchemaDescription,
proposedDesc client.SchemaDescription,
) (bool, error) {
existingDesc := existingDescriptionsByName[proposedDesc.Name]

hasChangedFields, err := validateUpdateSchemaFields(proposedDescriptionsByName, existingDesc, proposedDesc)
if err != nil {
return hasChangedFields, err
}

return hasChangedFields, err
}

func validateUpdateSchemaFields(
descriptionsByName map[string]client.SchemaDescription,
existingDesc client.SchemaDescription,
proposedDesc client.SchemaDescription,
) (bool, error) {
hasChanged := false
existingFieldsByName := map[string]client.SchemaFieldDescription{}
existingFieldIndexesByName := map[string]int{}
for i, field := range existingDesc.Fields {
existingFieldIndexesByName[field.Name] = i
existingFieldsByName[field.Name] = field
}

newFieldNames := map[string]struct{}{}
for _, proposedField := range proposedDesc.Fields {
_, fieldAlreadyExists := existingFieldsByName[proposedField.Name]

// If the field is new, then the collection has changed
hasChanged = hasChanged || !fieldAlreadyExists

newFieldNames[proposedField.Name] = struct{}{}
}

return hasChanged, nil
}

func validateSchemaFieldNotDeleted(
ctx context.Context,
db *db,
Expand Down
36 changes: 23 additions & 13 deletions internal/db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,18 @@ func (db *db) updateSchema(
migration immutable.Option[model.Lens],
setAsActiveVersion bool,
) error {
hasChanged, err := db.validateUpdateSchema(
existingSchemaByName,
proposedDescriptionsByName,
schema,
)
if err != nil {
return err
previousSchema := existingSchemaByName[schema.Name]

areEqual := areSchemasEqual(schema, previousSchema)
if areEqual {
return nil
}
err = db.validateSchemaUpdate(ctx, proposedDescriptionsByName, existingSchemaByName)

err := db.validateSchemaUpdate(ctx, proposedDescriptionsByName, existingSchemaByName)
if err != nil {
return err
}

if !hasChanged {
return nil
}

for _, field := range schema.Fields {
if field.Kind.IsObject() && !field.Kind.IsArray() {
idFieldName := field.Name + "_id"
Expand All @@ -365,7 +360,6 @@ func (db *db) updateSchema(
}
}

previousSchema := existingSchemaByName[schema.Name]
previousFieldNames := make(map[string]struct{}, len(previousSchema.Fields))
for _, field := range previousSchema.Fields {
previousFieldNames[field.Name] = struct{}{}
Expand Down Expand Up @@ -529,3 +523,19 @@ func (db *db) updateSchema(

return nil
}

func areSchemasEqual(this client.SchemaDescription, that client.SchemaDescription) bool {
if len(this.Fields) != len(that.Fields) {
return false
}

for i, thisField := range this.Fields {
if thisField != that.Fields[i] {
return false
}
}

return this.Name == that.Name &&
this.Root == that.Root &&
this.VersionID == that.VersionID
}

0 comments on commit ea8c8c6

Please sign in to comment.