Skip to content

Commit

Permalink
Use schema var in updateCollection instead of ref
Browse files Browse the repository at this point in the history
Just reduces the number of references for now, this function will need splitting later.
  • Loading branch information
AndrewSisley committed Oct 2, 2023
1 parent 3d4b4a0 commit 9a3ce7b
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ func (db *db) updateCollection(
return db.getCollectionByName(ctx, txn, desc.Name)
}

for _, field := range desc.Schema.Fields {
schema := desc.Schema
for _, field := range schema.Fields {
if field.RelationType.IsSet(client.Relation_Type_ONE) {
idFieldName := field.Name + "_id"
if _, ok := desc.Schema.GetField(idFieldName); !ok {
desc.Schema.Fields = append(desc.Schema.Fields, client.FieldDescription{
if _, ok := schema.GetField(idFieldName); !ok {
schema.Fields = append(desc.Schema.Fields, client.FieldDescription{
Name: idFieldName,
Kind: client.FieldKind_DocKey,
RelationType: client.Relation_Type_INTERNAL_ID,
Expand All @@ -240,23 +241,23 @@ func (db *db) updateCollection(
}
}

for i, field := range desc.Schema.Fields {
for i, field := range schema.Fields {
if field.ID == client.FieldID(0) {
// This is not wonderful and will probably break when we add the ability
// to delete fields, however it is good enough for now and matches the
// create behaviour.
field.ID = client.FieldID(i)
desc.Schema.Fields[i] = field
schema.Fields[i] = field
}

if field.Typ == client.NONE_CRDT {
// If no CRDT Type has been provided, default to LWW_REGISTER.
field.Typ = client.LWW_REGISTER
desc.Schema.Fields[i] = field
schema.Fields[i] = field
}
}

globalSchemaBuf, err := json.Marshal(desc.Schema)
globalSchemaBuf, err := json.Marshal(schema)
if err != nil {
return nil, err
}
Expand All @@ -265,9 +266,9 @@ func (db *db) updateCollection(
if err != nil {
return nil, err
}
previousSchemaVersionID := desc.Schema.VersionID
previousSchemaVersionID := schema.VersionID
schemaVersionID := cid.String()
desc.Schema.VersionID = schemaVersionID
schema.VersionID = schemaVersionID

buf, err := json.Marshal(desc)
if err != nil {
Expand All @@ -282,14 +283,14 @@ func (db *db) updateCollection(
return nil, err
}

schemaVersionHistoryKey := core.NewSchemaHistoryKey(desc.Schema.SchemaID, previousSchemaVersionID)
schemaVersionHistoryKey := core.NewSchemaHistoryKey(schema.SchemaID, previousSchemaVersionID)
err = txn.Systemstore().Put(ctx, schemaVersionHistoryKey.ToDS(), []byte(schemaVersionID))
if err != nil {
return nil, err
}

if setAsDefaultVersion {
err = db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, desc.Schema.SchemaID, schemaVersionID)
err = db.setDefaultSchemaVersionExplicit(ctx, txn, desc.Name, schema.SchemaID, schemaVersionID)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9a3ce7b

Please sign in to comment.