From 2d5cb8960b588b815ea0409ccda4364f38688118 Mon Sep 17 00:00:00 2001
From: Chris Quigley <chris.quigley2@gmail.com>
Date: Tue, 26 Nov 2024 11:53:58 -0500
Subject: [PATCH] IndexDescriptionCreateRequest in more places; not yet in
 tests; DO NOT MERGE

---
 cli/index_create.go              |  6 +++---
 client/collection.go             |  2 +-
 client/index.go                  |  4 ++--
 http/client_collection.go        |  2 +-
 http/handler_collection.go       |  7 ++++++-
 http/openapi.go                  |  2 +-
 internal/db/collection_define.go |  7 ++++++-
 internal/db/collection_index.go  | 25 ++++++++++++++-----------
 8 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/cli/index_create.go b/cli/index_create.go
index e9f4350fa0..d3d9162037 100644
--- a/cli/index_create.go
+++ b/cli/index_create.go
@@ -69,7 +69,7 @@ Example: create a unique index for 'Users' collection on 'name' in ascending ord
 				})
 			}
 
-			desc := client.IndexDescription{
+			desc := client.IndexDescriptionCreateRequest{
 				Name:   nameArg,
 				Fields: fields,
 				Unique: uniqueArg,
@@ -79,11 +79,11 @@ Example: create a unique index for 'Users' collection on 'name' in ascending ord
 				return err
 			}
 
-			desc, err = col.CreateIndex(cmd.Context(), desc)
+			descWithID, err := col.CreateIndex(cmd.Context(), desc)
 			if err != nil {
 				return err
 			}
-			return writeJSON(cmd, desc)
+			return writeJSON(cmd, descWithID)
 		},
 	}
 	cmd.Flags().StringVarP(&collectionArg, "collection", "c", "", "Collection name")
diff --git a/client/collection.go b/client/collection.go
index b557e2e335..b65da2c0a6 100644
--- a/client/collection.go
+++ b/client/collection.go
@@ -115,7 +115,7 @@ type Collection interface {
 	// only contain letters, numbers, and underscores.
 	// If the name of the index is not provided, it will be generated.
 	// WARNING: This method can not create index for a collection that has a policy.
-	CreateIndex(context.Context, IndexDescription) (IndexDescription, error)
+	CreateIndex(context.Context, IndexDescriptionCreateRequest) (IndexDescription, error)
 
 	// DropIndex drops an index from the collection.
 	DropIndex(ctx context.Context, indexName string) error
diff --git a/client/index.go b/client/index.go
index 03da11ee81..fe245d115d 100644
--- a/client/index.go
+++ b/client/index.go
@@ -36,10 +36,10 @@ type IndexDescription struct {
 	Unique bool
 }
 
-// IndexCreateRequestDescription describes an index creation request.
+// IndexDescriptionCreateRequest describes an index creation request.
 // It does not contain the ID, as it is not a valid field for the request body.
 // Instead it should be automatically generated.
-type IndexCreateRequestDescription struct {
+type IndexDescriptionCreateRequest struct {
 	// Name contains the name of the index.
 	Name string
 	// Fields contains the fields that are being indexed.
diff --git a/http/client_collection.go b/http/client_collection.go
index 3abfa61002..1a56ab50e1 100644
--- a/http/client_collection.go
+++ b/http/client_collection.go
@@ -379,7 +379,7 @@ func (c *Collection) GetAllDocIDs(
 
 func (c *Collection) CreateIndex(
 	ctx context.Context,
-	indexDesc client.IndexDescription,
+	indexDesc client.IndexDescriptionCreateRequest,
 ) (client.IndexDescription, error) {
 	if !c.Description().Name.HasValue() {
 		return client.IndexDescription{}, client.ErrOperationNotPermittedOnNamelessCols
diff --git a/http/handler_collection.go b/http/handler_collection.go
index 7b1d1a8a83..1757fc7d00 100644
--- a/http/handler_collection.go
+++ b/http/handler_collection.go
@@ -259,7 +259,12 @@ func (s *collectionHandler) CreateIndex(rw http.ResponseWriter, req *http.Reques
 		responseJSON(rw, http.StatusBadRequest, errorResponse{err})
 		return
 	}
-	index, err := col.CreateIndex(req.Context(), indexDesc)
+	descWithoutID := client.IndexDescriptionCreateRequest{
+		Name:   indexDesc.Name,
+		Fields: indexDesc.Fields,
+		Unique: indexDesc.Unique,
+	}
+	index, err := col.CreateIndex(req.Context(), descWithoutID)
 	if err != nil {
 		responseJSON(rw, http.StatusBadRequest, errorResponse{err})
 		return
diff --git a/http/openapi.go b/http/openapi.go
index 7695e0d55d..6e906ecd77 100644
--- a/http/openapi.go
+++ b/http/openapi.go
@@ -32,7 +32,7 @@ var openApiSchemas = map[string]any{
 	"schema":                          &client.SchemaDescription{},
 	"collection_definition":           &client.CollectionDefinition{},
 	"index":                           &client.IndexDescription{},
-	"index_create_request":            &client.IndexCreateRequestDescription{},
+	"index_create_request":            &client.IndexDescriptionCreateRequest{},
 	"delete_result":                   &client.DeleteResult{},
 	"update_result":                   &client.UpdateResult{},
 	"lens_config":                     &client.LensConfig{},
diff --git a/internal/db/collection_define.go b/internal/db/collection_define.go
index 6597a54f1c..8f49b9a970 100644
--- a/internal/db/collection_define.go
+++ b/internal/db/collection_define.go
@@ -91,7 +91,12 @@ func (db *db) createCollections(
 		col := db.newCollection(desc, def.Schema)
 
 		for _, index := range desc.Indexes {
-			if _, err := col.createIndex(ctx, index); err != nil {
+			descWithoutID := client.IndexDescriptionCreateRequest{
+				Name:   index.Name,
+				Fields: index.Fields,
+				Unique: index.Unique,
+			}
+			if _, err := col.createIndex(ctx, descWithoutID); err != nil {
 				return nil, err
 			}
 		}
diff --git a/internal/db/collection_index.go b/internal/db/collection_index.go
index 3c5da18c58..515a5047aa 100644
--- a/internal/db/collection_index.go
+++ b/internal/db/collection_index.go
@@ -35,7 +35,7 @@ import (
 func (db *db) createCollectionIndex(
 	ctx context.Context,
 	collectionName string,
-	desc client.IndexDescription,
+	desc client.IndexDescriptionCreateRequest,
 ) (client.IndexDescription, error) {
 	col, err := db.getCollectionByName(ctx, collectionName)
 	if err != nil {
@@ -218,7 +218,7 @@ func (c *collection) deleteIndexedDocWithID(
 // the documents will be indexed by the new index.
 func (c *collection) CreateIndex(
 	ctx context.Context,
-	desc client.IndexDescription,
+	desc client.IndexDescriptionCreateRequest,
 ) (client.IndexDescription, error) {
 	ctx, txn, err := ensureContextTxn(ctx, c.db, false)
 	if err != nil {
@@ -235,7 +235,7 @@ func (c *collection) CreateIndex(
 
 func (c *collection) createIndex(
 	ctx context.Context,
-	desc client.IndexDescription,
+	desc client.IndexDescriptionCreateRequest,
 ) (CollectionIndex, error) {
 	if desc.Name != "" && !schema.IsValidIndexName(desc.Name) {
 		return nil, schema.NewErrIndexWithInvalidName("!")
@@ -266,9 +266,15 @@ func (c *collection) createIndex(
 	if err != nil {
 		return nil, err
 	}
-	desc.ID = uint32(colID)
 
-	buf, err := json.Marshal(desc)
+	descWithID := client.IndexDescription{
+		Name:   desc.Name,
+		ID:     uint32(colID),
+		Fields: desc.Fields,
+		Unique: desc.Unique,
+	}
+
+	buf, err := json.Marshal(descWithID)
 	if err != nil {
 		return nil, err
 	}
@@ -278,7 +284,7 @@ func (c *collection) createIndex(
 	if err != nil {
 		return nil, err
 	}
-	colIndex, err := NewCollectionIndex(c, desc)
+	colIndex, err := NewCollectionIndex(c, descWithID)
 	if err != nil {
 		return nil, err
 	}
@@ -491,7 +497,7 @@ func (c *collection) checkExistingFieldsAndAdjustRelFieldNames(
 
 func (c *collection) generateIndexNameIfNeededAndCreateKey(
 	ctx context.Context,
-	desc *client.IndexDescription,
+	desc *client.IndexDescriptionCreateRequest,
 ) (keys.CollectionIndexKey, error) {
 	// callers of this function must set a context transaction
 	txn := mustGetContextTxn(ctx)
@@ -524,10 +530,7 @@ func (c *collection) generateIndexNameIfNeededAndCreateKey(
 	return indexKey, nil
 }
 
-func validateIndexDescription(desc client.IndexDescription) error {
-	if desc.ID != 0 {
-		return NewErrNonZeroIndexIDProvided(desc.ID)
-	}
+func validateIndexDescription(desc client.IndexDescriptionCreateRequest) error {
 	if len(desc.Fields) == 0 {
 		return ErrIndexMissingFields
 	}