From 02c5c504b5fda7bcd8ac6a1cacf6a3920b6cbffb Mon Sep 17 00:00:00 2001 From: Chris Quigley Date: Fri, 22 Nov 2024 12:31:12 -0500 Subject: [PATCH 1/4] Changed HTTP Post Index --- client/index.go | 12 ++++++++++ docs/website/references/http/openapi.json | 27 ++++++++++++++++++++++- http/handler_collection.go | 5 ++++- http/openapi.go | 1 + 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/client/index.go b/client/index.go index b09b258224..03da11ee81 100644 --- a/client/index.go +++ b/client/index.go @@ -36,6 +36,18 @@ type IndexDescription struct { Unique bool } +// IndexCreateRequestDescription 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 { + // Name contains the name of the index. + Name string + // Fields contains the fields that are being indexed. + Fields []IndexedFieldDescription + // Unique indicates whether the index is unique. + Unique bool +} + // CollectionIndex is an interface for indexing documents in a collection. type CollectionIndex interface { // Save indexes a document by storing indexed field values. diff --git a/docs/website/references/http/openapi.json b/docs/website/references/http/openapi.json index 1f28b84a92..c5c9fbc7b2 100644 --- a/docs/website/references/http/openapi.json +++ b/docs/website/references/http/openapi.json @@ -442,6 +442,31 @@ }, "type": "object" }, + "index_create_request": { + "properties": { + "Fields": { + "items": { + "properties": { + "Descending": { + "type": "boolean" + }, + "Name": { + "type": "string" + } + }, + "type": "object" + }, + "type": "array" + }, + "Name": { + "type": "string" + }, + "Unique": { + "type": "boolean" + } + }, + "type": "object" + }, "lens_config": { "properties": { "DestinationSchemaVersionID": { @@ -1245,7 +1270,7 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/index" + "$ref": "#/components/schemas/index_create_request" } } }, diff --git a/http/handler_collection.go b/http/handler_collection.go index ddade699e3..7b1d1a8a83 100644 --- a/http/handler_collection.go +++ b/http/handler_collection.go @@ -318,6 +318,9 @@ func (h *collectionHandler) bindRoutes(router *Router) { indexSchema := &openapi3.SchemaRef{ Ref: "#/components/schemas/index", } + indexCreateRequestSchema := &openapi3.SchemaRef{ + Ref: "#/components/schemas/index_create_request", + } collectionNamePathParam := openapi3.NewPathParameter("name"). WithDescription("Collection name"). @@ -389,7 +392,7 @@ func (h *collectionHandler) bindRoutes(router *Router) { createIndexRequest := openapi3.NewRequestBody(). WithRequired(true). - WithContent(openapi3.NewContentWithJSONSchemaRef(indexSchema)) + WithContent(openapi3.NewContentWithJSONSchemaRef(indexCreateRequestSchema)) createIndexResponse := openapi3.NewResponse(). WithDescription("Index description"). WithJSONSchemaRef(indexSchema) diff --git a/http/openapi.go b/http/openapi.go index b217036182..7695e0d55d 100644 --- a/http/openapi.go +++ b/http/openapi.go @@ -32,6 +32,7 @@ var openApiSchemas = map[string]any{ "schema": &client.SchemaDescription{}, "collection_definition": &client.CollectionDefinition{}, "index": &client.IndexDescription{}, + "index_create_request": &client.IndexCreateRequestDescription{}, "delete_result": &client.DeleteResult{}, "update_result": &client.UpdateResult{}, "lens_config": &client.LensConfig{}, From 2d5cb8960b588b815ea0409ccda4364f38688118 Mon Sep 17 00:00:00 2001 From: Chris Quigley Date: Tue, 26 Nov 2024 11:53:58 -0500 Subject: [PATCH 2/4] 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 } From 603502ea7f627ceca27db62219d5880a2a771df8 Mon Sep 17 00:00:00 2001 From: Chris Quigley Date: Wed, 27 Nov 2024 13:34:59 -0500 Subject: [PATCH 3/4] Modified integration tests. Do not merge yet --- internal/db/index_test.go | 133 +++++++++++++----------- internal/db/indexed_docs_test.go | 16 +-- tests/clients/cli/wrapper_collection.go | 2 +- tests/integration/utils.go | 2 +- 4 files changed, 85 insertions(+), 68 deletions(-) diff --git a/internal/db/index_test.go b/internal/db/index_test.go index 950f41c47f..1582cdfcfe 100644 --- a/internal/db/index_test.go +++ b/internal/db/index_test.go @@ -145,13 +145,13 @@ func newIndexTestFixture(t *testing.T) *indexTestFixture { } func (f *indexTestFixture) createCollectionIndex( - desc client.IndexDescription, + desc client.IndexDescriptionCreateRequest, ) (client.IndexDescription, error) { return f.createCollectionIndexFor(f.users.Name().Value(), desc) } -func getUsersIndexDescOnName() client.IndexDescription { - return client.IndexDescription{ +func getUsersIndexDescOnName() client.IndexDescriptionCreateRequest { + return client.IndexDescriptionCreateRequest{ Name: testUsersColIndexName, Fields: []client.IndexedFieldDescription{ {Name: usersNameFieldName}, @@ -159,8 +159,8 @@ func getUsersIndexDescOnName() client.IndexDescription { } } -func getUsersIndexDescOnAge() client.IndexDescription { - return client.IndexDescription{ +func getUsersIndexDescOnAge() client.IndexDescriptionCreateRequest { + return client.IndexDescriptionCreateRequest{ Name: testUsersColIndexAge, Fields: []client.IndexedFieldDescription{ {Name: usersAgeFieldName}, @@ -168,8 +168,8 @@ func getUsersIndexDescOnAge() client.IndexDescription { } } -func getUsersIndexDescOnWeight() client.IndexDescription { - return client.IndexDescription{ +func getUsersIndexDescOnWeight() client.IndexDescriptionCreateRequest { + return client.IndexDescriptionCreateRequest{ Name: testUsersColIndexWeight, Fields: []client.IndexedFieldDescription{ {Name: usersWeightFieldName}, @@ -177,8 +177,8 @@ func getUsersIndexDescOnWeight() client.IndexDescription { } } -func getProductsIndexDescOnCategory() client.IndexDescription { - return client.IndexDescription{ +func getProductsIndexDescOnCategory() client.IndexDescriptionCreateRequest { + return client.IndexDescriptionCreateRequest{ Name: testUsersColIndexAge, Fields: []client.IndexedFieldDescription{ {Name: productsCategoryFieldName}, @@ -193,7 +193,7 @@ func (f *indexTestFixture) createUserCollectionIndexOnName() client.IndexDescrip } func (f *indexTestFixture) createUserCollectionIndexOnNumbers() client.IndexDescription { - indexDesc := client.IndexDescription{ + indexDesc := client.IndexDescriptionCreateRequest{ Name: "users_numbers_index", Fields: []client.IndexedFieldDescription{ {Name: usersNumbersFieldName}, @@ -206,7 +206,7 @@ func (f *indexTestFixture) createUserCollectionIndexOnNumbers() client.IndexDesc return newDesc } -func makeUnique(indexDesc client.IndexDescription) client.IndexDescription { +func makeUnique(indexDesc client.IndexDescriptionCreateRequest) client.IndexDescriptionCreateRequest { indexDesc.Unique = true return indexDesc } @@ -218,7 +218,7 @@ func (f *indexTestFixture) createUserCollectionUniqueIndexOnName() client.IndexD return newDesc } -func addFieldToIndex(indexDesc client.IndexDescription, fieldName string) client.IndexDescription { +func addFieldToIndex(indexDesc client.IndexDescriptionCreateRequest, fieldName string) client.IndexDescriptionCreateRequest { indexDesc.Fields = append(indexDesc.Fields, client.IndexedFieldDescription{ Name: fieldName, }) @@ -274,7 +274,7 @@ func (f *indexTestFixture) commitTxn() { func (f *indexTestFixture) createCollectionIndexFor( collectionName string, - desc client.IndexDescription, + desc client.IndexDescriptionCreateRequest, ) (client.IndexDescription, error) { ctx := SetContextTxn(f.ctx, f.txn) index, err := f.db.createCollectionIndex(ctx, collectionName, desc) @@ -298,34 +298,17 @@ func TestCreateIndex_IfFieldsIsEmpty_ReturnError(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - _, err := f.createCollectionIndex(client.IndexDescription{ + _, err := f.createCollectionIndex(client.IndexDescriptionCreateRequest{ Name: "some_index_name", }) assert.EqualError(t, err, errIndexMissingFields) } -func TestCreateIndex_IfIndexDescriptionIDIsNotZero_ReturnError(t *testing.T) { - f := newIndexTestFixture(t) - defer f.db.Close() - - for _, id := range []uint32{1, 20, 999} { - desc := client.IndexDescription{ - Name: "some_index_name", - ID: id, - Fields: []client.IndexedFieldDescription{ - {Name: usersNameFieldName}, - }, - } - _, err := f.createCollectionIndex(desc) - assert.ErrorIs(t, err, NewErrNonZeroIndexIDProvided(0)) - } -} - func TestCreateIndex_IfValidInput_CreateIndex(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - desc := client.IndexDescription{ + desc := client.IndexDescriptionCreateRequest{ Name: "some_index_name", Fields: []client.IndexedFieldDescription{ {Name: usersNameFieldName}, @@ -342,7 +325,7 @@ func TestCreateIndex_IfFieldNameIsEmpty_ReturnError(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - desc := client.IndexDescription{ + desc := client.IndexDescriptionCreateRequest{ Name: "some_index_name", Fields: []client.IndexedFieldDescription{ {Name: ""}, @@ -356,7 +339,7 @@ func TestCreateIndex_IfFieldHasNoDirection_DefaultToAsc(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - desc := client.IndexDescription{ + desc := client.IndexDescriptionCreateRequest{ Name: "some_index_name", Fields: []client.IndexedFieldDescription{{Name: usersNameFieldName}}, } @@ -370,11 +353,11 @@ func TestCreateIndex_IfIndexWithNameAlreadyExists_ReturnError(t *testing.T) { defer f.db.Close() name := "some_index_name" - desc1 := client.IndexDescription{ + desc1 := client.IndexDescriptionCreateRequest{ Name: name, Fields: []client.IndexedFieldDescription{{Name: usersNameFieldName}}, } - desc2 := client.IndexDescription{ + desc2 := client.IndexDescriptionCreateRequest{ Name: name, Fields: []client.IndexedFieldDescription{{Name: usersAgeFieldName}}, } @@ -389,15 +372,15 @@ func TestCreateIndex_IfGeneratedNameMatchesExisting_AddIncrement(t *testing.T) { defer f.db.Close() name := usersColName + "_" + usersAgeFieldName + "_ASC" - desc1 := client.IndexDescription{ + desc1 := client.IndexDescriptionCreateRequest{ Name: name, Fields: []client.IndexedFieldDescription{{Name: usersNameFieldName}}, } - desc2 := client.IndexDescription{ + desc2 := client.IndexDescriptionCreateRequest{ Name: name + "_2", Fields: []client.IndexedFieldDescription{{Name: usersWeightFieldName}}, } - desc3 := client.IndexDescription{ + desc3 := client.IndexDescriptionCreateRequest{ Name: "", Fields: []client.IndexedFieldDescription{{Name: usersAgeFieldName}}, } @@ -415,7 +398,7 @@ func TestCreateIndex_ShouldSaveToSystemStorage(t *testing.T) { defer f.db.Close() name := "users_age_ASC" - desc := client.IndexDescription{ + desc := client.IndexDescriptionCreateRequest{ Name: name, Fields: []client.IndexedFieldDescription{{Name: usersNameFieldName}}, } @@ -428,15 +411,21 @@ func TestCreateIndex_ShouldSaveToSystemStorage(t *testing.T) { var deserialized client.IndexDescription err = json.Unmarshal(data, &deserialized) assert.NoError(t, err) - desc.ID = 1 - assert.Equal(t, desc, deserialized) + + descWithID := client.IndexDescription{ + Name: desc.Name, + ID: 1, + Fields: desc.Fields, + Unique: desc.Unique, + } + assert.Equal(t, descWithID, deserialized) } func TestCreateIndex_IfCollectionDoesntExist_ReturnError(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - desc := client.IndexDescription{ + desc := client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{{Name: productsPriceFieldName}}, } @@ -449,7 +438,7 @@ func TestCreateIndex_IfPropertyDoesntExist_ReturnError(t *testing.T) { defer f.db.Close() const field = "non_existing_field" - desc := client.IndexDescription{ + desc := client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{{Name: field}}, } @@ -462,8 +451,8 @@ func TestCreateIndex_WithMultipleCollectionsAndIndexes_AssignIncrementedIDPerCol users := f.addUsersCollection() products := f.getProductsCollectionDesc() - makeIndex := func(fieldName string) client.IndexDescription { - return client.IndexDescription{ + makeIndex := func(fieldName string) client.IndexDescriptionCreateRequest { + return client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{ {Name: fieldName}, }, @@ -533,7 +522,7 @@ func TestGetIndexes_ShouldReturnListOfAllExistingIndexes(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - usersIndexDesc := client.IndexDescription{ + usersIndexDesc := client.IndexDescriptionCreateRequest{ Name: "users_name_index", Fields: []client.IndexedFieldDescription{{Name: usersNameFieldName}}, } @@ -541,7 +530,7 @@ func TestGetIndexes_ShouldReturnListOfAllExistingIndexes(t *testing.T) { assert.NoError(t, err) f.getProductsCollectionDesc() - productsIndexDesc := client.IndexDescription{ + productsIndexDesc := client.IndexDescriptionCreateRequest{ Name: "products_description_index", Fields: []client.IndexedFieldDescription{{Name: productsPriceFieldName}}, } @@ -657,7 +646,7 @@ func TestGetCollectionIndexes_ShouldReturnListOfCollectionIndexes(t *testing.T) f := newIndexTestFixture(t) defer f.db.Close() - usersIndexDesc := client.IndexDescription{ + usersIndexDesc := client.IndexDescriptionCreateRequest{ Name: "users_name_index", Fields: []client.IndexedFieldDescription{{Name: usersNameFieldName}}, } @@ -665,7 +654,7 @@ func TestGetCollectionIndexes_ShouldReturnListOfCollectionIndexes(t *testing.T) assert.NoError(t, err) products := f.getProductsCollectionDesc() - productsIndexDesc := client.IndexDescription{ + productsIndexDesc := client.IndexDescriptionCreateRequest{ Name: "products_description_index", Fields: []client.IndexedFieldDescription{{Name: productsPriceFieldName}}, } @@ -679,14 +668,26 @@ func TestGetCollectionIndexes_ShouldReturnListOfCollectionIndexes(t *testing.T) userIndexes, err := f.getCollectionIndexes(f.users.ID()) assert.NoError(t, err) require.Equal(t, 1, len(userIndexes)) - usersIndexDesc.ID = 1 - assert.Equal(t, usersIndexDesc, userIndexes[0]) + + descWithID := client.IndexDescription{ + Name: usersIndexDesc.Name, + ID: 1, + Fields: usersIndexDesc.Fields, + Unique: usersIndexDesc.Unique, + } + assert.Equal(t, descWithID, userIndexes[0]) productIndexes, err := f.getCollectionIndexes(products.ID()) assert.NoError(t, err) require.Equal(t, 1, len(productIndexes)) - productsIndexDesc.ID = 1 - assert.Equal(t, productsIndexDesc, productIndexes[0]) + + productsIndexDescWithID := client.IndexDescription{ + Name: productsIndexDesc.Name, + ID: 1, + Fields: productsIndexDesc.Fields, + Unique: productsIndexDesc.Unique, + } + assert.Equal(t, productsIndexDescWithID, productIndexes[0]) } func TestGetCollectionIndexes_IfSystemStoreFails_ReturnError(t *testing.T) { @@ -970,7 +971,7 @@ func TestCollectionGetIndexes_ShouldReturnIndexesInOrderedByName(t *testing.T) { require.NoError(f.t, err) for i := 1; i <= num; i++ { iStr := toSuffix(i) - indexDesc := client.IndexDescription{ + indexDesc := client.IndexDescriptionCreateRequest{ Name: indexNamePrefix + iStr, Fields: []client.IndexedFieldDescription{ {Name: fieldNamePrefix + iStr}, @@ -1114,14 +1115,14 @@ func TestDropIndex_IfSystemStoreFails_ReturnError(t *testing.T) { func TestDropAllIndexes_ShouldDeleteAllIndexes(t *testing.T) { f := newIndexTestFixture(t) defer f.db.Close() - _, err := f.createCollectionIndexFor(usersColName, client.IndexDescription{ + _, err := f.createCollectionIndexFor(usersColName, client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{ {Name: usersNameFieldName}, }, }) assert.NoError(f.t, err) - _, err = f.createCollectionIndexFor(usersColName, client.IndexDescription{ + _, err = f.createCollectionIndexFor(usersColName, client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{ {Name: usersAgeFieldName}, }, @@ -1231,8 +1232,14 @@ func TestNewCollectionIndex_IfDescriptionHasNoFields_ReturnError(t *testing.T) { defer f.db.Close() desc := getUsersIndexDescOnName() desc.Fields = nil - _, err := NewCollectionIndex(f.users, desc) - require.ErrorIs(t, err, NewErrIndexDescHasNoFields(desc)) + descWithID := client.IndexDescription{ + Name: desc.Name, + ID: 1, + Fields: desc.Fields, + Unique: desc.Unique, + } + _, err := NewCollectionIndex(f.users, descWithID) + require.ErrorIs(t, err, NewErrIndexDescHasNoFields(descWithID)) } func TestNewCollectionIndex_IfDescriptionHasNonExistingField_ReturnError(t *testing.T) { @@ -1240,6 +1247,12 @@ func TestNewCollectionIndex_IfDescriptionHasNonExistingField_ReturnError(t *test defer f.db.Close() desc := getUsersIndexDescOnName() desc.Fields[0].Name = "non_existing_field" - _, err := NewCollectionIndex(f.users, desc) + descWithID := client.IndexDescription{ + Name: desc.Name, + ID: 1, + Fields: desc.Fields, + Unique: desc.Unique, + } + _, err := NewCollectionIndex(f.users, descWithID) require.ErrorIs(t, err, client.NewErrFieldNotExist(desc.Fields[0].Name)) } diff --git a/internal/db/indexed_docs_test.go b/internal/db/indexed_docs_test.go index 2c6ce0af53..49b8795021 100644 --- a/internal/db/indexed_docs_test.go +++ b/internal/db/indexed_docs_test.go @@ -284,8 +284,13 @@ func (f *indexTestFixture) stubSystemStore(systemStoreOn *mocks.DSReaderWriter_E f.users = f.addUsersCollection() } desc := getUsersIndexDescOnName() - desc.ID = 1 - indexOnNameDescData, err := json.Marshal(desc) + descWithID := client.IndexDescription{ + Name: desc.Name, + ID: 1, + Fields: desc.Fields, + Unique: desc.Unique, + } + indexOnNameDescData, err := json.Marshal(descWithID) require.NoError(f.t, err) colIndexKey := keys.NewCollectionIndexKey(immutable.Some(f.users.ID()), "") @@ -1513,13 +1518,12 @@ func TestArrayIndex_With2ArrayFieldsIfDocIsDeleted_ShouldRemoveIndex(t *testing. f := newIndexTestFixture(t) defer f.db.Close() - indexDesc := client.IndexDescription{ + indexDesc := client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{ {Name: usersNumbersFieldName}, {Name: usersHobbiesFieldName}, }, } - _, err := f.createCollectionIndexFor(f.users.Name().Value(), indexDesc) require.NoError(f.t, err) @@ -1541,7 +1545,7 @@ func TestArrayIndex_With2ArrayFieldsIfDocIsDeletedButOneArrayElementHasNoIndexRe f := newIndexTestFixture(t) defer f.db.Close() - indexDesc := client.IndexDescription{ + indexDesc := client.IndexDescriptionCreateRequest{ Fields: []client.IndexedFieldDescription{ {Name: usersNumbersFieldName}, {Name: usersHobbiesFieldName}, @@ -1572,7 +1576,7 @@ func TestArrayIndex_WithUniqueIndexIfDocIsDeleted_ShouldRemoveIndex(t *testing.T f := newIndexTestFixture(t) defer f.db.Close() - indexDesc := client.IndexDescription{ + indexDesc := client.IndexDescriptionCreateRequest{ Unique: true, Fields: []client.IndexedFieldDescription{ {Name: usersNumbersFieldName}, diff --git a/tests/clients/cli/wrapper_collection.go b/tests/clients/cli/wrapper_collection.go index eb9c5f5466..6fbd739879 100644 --- a/tests/clients/cli/wrapper_collection.go +++ b/tests/clients/cli/wrapper_collection.go @@ -332,7 +332,7 @@ func (c *Collection) GetAllDocIDs( func (c *Collection) CreateIndex( ctx context.Context, - indexDesc client.IndexDescription, + indexDesc client.IndexDescriptionCreateRequest, ) (index client.IndexDescription, err error) { if !c.Description().Name.HasValue() { return client.IndexDescription{}, client.ErrOperationNotPermittedOnNamelessCols diff --git a/tests/integration/utils.go b/tests/integration/utils.go index dfd3096bd3..e352a0ff1c 100644 --- a/tests/integration/utils.go +++ b/tests/integration/utils.go @@ -1565,7 +1565,7 @@ func createIndex( for index, node := range nodes { nodeID := nodeIDs[index] collection := s.nodes[nodeID].collections[action.CollectionID] - indexDesc := client.IndexDescription{ + indexDesc := client.IndexDescriptionCreateRequest{ Name: action.IndexName, } if action.FieldName != "" { From 2c1786f411bfbbb34afa98903a6ac40aa23e5e43 Mon Sep 17 00:00:00 2001 From: Chris Quigley Date: Fri, 29 Nov 2024 12:20:20 -0500 Subject: [PATCH 4/4] Make fix --- client/mocks/collection.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/client/mocks/collection.go b/client/mocks/collection.go index 3b80849661..a34140f144 100644 --- a/client/mocks/collection.go +++ b/client/mocks/collection.go @@ -73,7 +73,7 @@ func (_c *Collection_Create_Call) RunAndReturn(run func(context.Context, *client } // CreateIndex provides a mock function with given fields: _a0, _a1 -func (_m *Collection) CreateIndex(_a0 context.Context, _a1 client.IndexDescription) (client.IndexDescription, error) { +func (_m *Collection) CreateIndex(_a0 context.Context, _a1 client.IndexDescriptionCreateRequest) (client.IndexDescription, error) { ret := _m.Called(_a0, _a1) if len(ret) == 0 { @@ -82,16 +82,16 @@ func (_m *Collection) CreateIndex(_a0 context.Context, _a1 client.IndexDescripti var r0 client.IndexDescription var r1 error - if rf, ok := ret.Get(0).(func(context.Context, client.IndexDescription) (client.IndexDescription, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context, client.IndexDescriptionCreateRequest) (client.IndexDescription, error)); ok { return rf(_a0, _a1) } - if rf, ok := ret.Get(0).(func(context.Context, client.IndexDescription) client.IndexDescription); ok { + if rf, ok := ret.Get(0).(func(context.Context, client.IndexDescriptionCreateRequest) client.IndexDescription); ok { r0 = rf(_a0, _a1) } else { r0 = ret.Get(0).(client.IndexDescription) } - if rf, ok := ret.Get(1).(func(context.Context, client.IndexDescription) error); ok { + if rf, ok := ret.Get(1).(func(context.Context, client.IndexDescriptionCreateRequest) error); ok { r1 = rf(_a0, _a1) } else { r1 = ret.Error(1) @@ -107,14 +107,14 @@ type Collection_CreateIndex_Call struct { // CreateIndex is a helper method to define mock.On call // - _a0 context.Context -// - _a1 client.IndexDescription +// - _a1 client.IndexDescriptionCreateRequest func (_e *Collection_Expecter) CreateIndex(_a0 interface{}, _a1 interface{}) *Collection_CreateIndex_Call { return &Collection_CreateIndex_Call{Call: _e.mock.On("CreateIndex", _a0, _a1)} } -func (_c *Collection_CreateIndex_Call) Run(run func(_a0 context.Context, _a1 client.IndexDescription)) *Collection_CreateIndex_Call { +func (_c *Collection_CreateIndex_Call) Run(run func(_a0 context.Context, _a1 client.IndexDescriptionCreateRequest)) *Collection_CreateIndex_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(client.IndexDescription)) + run(args[0].(context.Context), args[1].(client.IndexDescriptionCreateRequest)) }) return _c } @@ -124,7 +124,7 @@ func (_c *Collection_CreateIndex_Call) Return(_a0 client.IndexDescription, _a1 e return _c } -func (_c *Collection_CreateIndex_Call) RunAndReturn(run func(context.Context, client.IndexDescription) (client.IndexDescription, error)) *Collection_CreateIndex_Call { +func (_c *Collection_CreateIndex_Call) RunAndReturn(run func(context.Context, client.IndexDescriptionCreateRequest) (client.IndexDescription, error)) *Collection_CreateIndex_Call { _c.Call.Return(run) return _c }