From 3ded37f240c7baae5d40a91526d1f7ffabecffc5 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 2 Oct 2023 12:09:48 -0400 Subject: [PATCH] Use schema var in updateCollection instead of ref Just reduces the number of references for now, this function will need splitting later. --- db/collection.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/db/collection.go b/db/collection.go index 64aa662734..6c07183e02 100644 --- a/db/collection.go +++ b/db/collection.go @@ -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(schema.Fields, client.FieldDescription{ Name: idFieldName, Kind: client.FieldKind_DocKey, RelationType: client.Relation_Type_INTERNAL_ID, @@ -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 } @@ -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 { @@ -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 }