diff --git a/v2/arangodb/collection_documents_replace.go b/v2/arangodb/collection_documents_replace.go index 567e77c9..ff024fa5 100644 --- a/v2/arangodb/collection_documents_replace.go +++ b/v2/arangodb/collection_documents_replace.go @@ -65,7 +65,7 @@ type CollectionDocumentReplaceResponseReader interface { } type CollectionDocumentReplaceResponse struct { - DocumentMeta + DocumentMetaWithOldRev shared.ResponseStruct `json:",inline"` Old, New interface{} } diff --git a/v2/arangodb/collection_documents_replace_impl.go b/v2/arangodb/collection_documents_replace_impl.go index 96779ad8..152dc0ad 100644 --- a/v2/arangodb/collection_documents_replace_impl.go +++ b/v2/arangodb/collection_documents_replace_impl.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2023 ArangoDB GmbH, Cologne, Germany +// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -59,13 +59,13 @@ func (c collectionDocumentReplace) ReplaceDocumentWithOptions(ctx context.Contex } response := struct { - *DocumentMeta `json:",inline"` - *shared.ResponseStruct `json:",inline"` - Old *UnmarshalInto `json:"old,omitempty"` - New *UnmarshalInto `json:"new,omitempty"` + *DocumentMetaWithOldRev `json:",inline"` + *shared.ResponseStruct `json:",inline"` + Old *UnmarshalInto `json:"old,omitempty"` + New *UnmarshalInto `json:"new,omitempty"` }{ - DocumentMeta: &meta.DocumentMeta, - ResponseStruct: &meta.ResponseStruct, + DocumentMetaWithOldRev: &meta.DocumentMetaWithOldRev, + ResponseStruct: &meta.ResponseStruct, Old: newUnmarshalInto(meta.Old), New: newUnmarshalInto(meta.New), @@ -142,7 +142,7 @@ type collectionDocumentReplaceResponseReader struct { array *connection.Array options *CollectionDocumentReplaceOptions response struct { - *DocumentMeta + *DocumentMetaWithOldRev *shared.ResponseStruct `json:",inline"` Old *UnmarshalInto `json:"old,omitempty"` New *UnmarshalInto `json:"new,omitempty"` @@ -161,7 +161,7 @@ func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentRepl meta.New = c.options.NewObject } - c.response.DocumentMeta = &meta.DocumentMeta + c.response.DocumentMetaWithOldRev = &meta.DocumentMetaWithOldRev c.response.ResponseStruct = &meta.ResponseStruct if err := c.array.Unmarshal(&c.response); err != nil { diff --git a/v2/arangodb/collection_documents_update.go b/v2/arangodb/collection_documents_update.go index 35461a51..4a200c28 100644 --- a/v2/arangodb/collection_documents_update.go +++ b/v2/arangodb/collection_documents_update.go @@ -66,7 +66,7 @@ type CollectionDocumentUpdateResponseReader interface { } type CollectionDocumentUpdateResponse struct { - DocumentMeta + DocumentMetaWithOldRev shared.ResponseStruct `json:",inline"` Old, New interface{} } diff --git a/v2/arangodb/collection_documents_update_impl.go b/v2/arangodb/collection_documents_update_impl.go index 45f2a7e0..068d1b39 100644 --- a/v2/arangodb/collection_documents_update_impl.go +++ b/v2/arangodb/collection_documents_update_impl.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2020-2023 ArangoDB GmbH, Cologne, Germany +// Copyright 2020-2025 ArangoDB GmbH, Cologne, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -59,13 +59,13 @@ func (c collectionDocumentUpdate) UpdateDocumentWithOptions(ctx context.Context, } response := struct { - *DocumentMeta `json:",inline"` - *shared.ResponseStruct `json:",inline"` - Old *UnmarshalInto `json:"old,omitempty"` - New *UnmarshalInto `json:"new,omitempty"` + *DocumentMetaWithOldRev `json:",inline"` + *shared.ResponseStruct `json:",inline"` + Old *UnmarshalInto `json:"old,omitempty"` + New *UnmarshalInto `json:"new,omitempty"` }{ - DocumentMeta: &meta.DocumentMeta, - ResponseStruct: &meta.ResponseStruct, + DocumentMetaWithOldRev: &meta.DocumentMetaWithOldRev, + ResponseStruct: &meta.ResponseStruct, Old: newUnmarshalInto(meta.Old), New: newUnmarshalInto(meta.New), @@ -142,7 +142,7 @@ type collectionDocumentUpdateResponseReader struct { array *connection.Array options *CollectionDocumentUpdateOptions response struct { - *DocumentMeta + *DocumentMetaWithOldRev *shared.ResponseStruct `json:",inline"` Old *UnmarshalInto `json:"old,omitempty"` New *UnmarshalInto `json:"new,omitempty"` @@ -161,7 +161,7 @@ func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdat meta.New = c.options.NewObject } - c.response.DocumentMeta = &meta.DocumentMeta + c.response.DocumentMetaWithOldRev = &meta.DocumentMetaWithOldRev c.response.ResponseStruct = &meta.ResponseStruct if err := c.array.Unmarshal(&c.response); err != nil { diff --git a/v2/arangodb/meta.go b/v2/arangodb/meta.go index b36cb7dc..d57f5b5b 100644 --- a/v2/arangodb/meta.go +++ b/v2/arangodb/meta.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2020-2021 ArangoDB GmbH, Cologne, Germany +// Copyright 2020-2025 ArangoDB GmbH, Cologne, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -44,6 +44,11 @@ type DocumentMeta struct { Rev string `json:"_rev,omitempty"` } +type DocumentMetaWithOldRev struct { + DocumentMeta + OldRev string `json:"_oldRev,omitempty"` +} + // validateKey returns an error if the given key is empty otherwise invalid. func validateKey(key string) error { if key == "" { diff --git a/v2/tests/database_collection_doc_replace_test.go b/v2/tests/database_collection_doc_replace_test.go index 280785a0..ebc4f8f2 100644 --- a/v2/tests/database_collection_doc_replace_test.go +++ b/v2/tests/database_collection_doc_replace_test.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany +// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -126,6 +126,33 @@ func Test_DatabaseCollectionDocReplaceIgnoreRevs(t *testing.T) { }) } +func Test_DatabaseCollectionDocReplaceReturnOldRev(t *testing.T) { + Wrap(t, func(t *testing.T, client arangodb.Client) { + WithDatabase(t, client, nil, func(db arangodb.Database) { + WithCollection(t, db, nil, func(col arangodb.Collection) { + withContextT(t, defaultTestTimeout, func(ctx context.Context, tb testing.TB) { + doc := DocWithRev{ + Name: "test-ORG", + } + + metaOrg, err := col.CreateDocument(ctx, doc) + require.NoError(t, err) + + docReplace := DocWithRev{ + Name: "test-REPLACED", + } + + t.Run("OldRev should match", func(t *testing.T) { + metaRep, err := col.ReplaceDocumentWithOptions(ctx, metaOrg.Key, docReplace, nil) + require.NoError(t, err) + require.Equal(t, metaOrg.Rev, metaRep.OldRev) + }) + }) + }) + }) + }) +} + func Test_DatabaseCollectionDocReplaceSilent(t *testing.T) { Wrap(t, func(t *testing.T, client arangodb.Client) { WithDatabase(t, client, nil, func(db arangodb.Database) { diff --git a/v2/tests/database_collection_doc_update_test.go b/v2/tests/database_collection_doc_update_test.go index 8771db4d..c10bd5b9 100644 --- a/v2/tests/database_collection_doc_update_test.go +++ b/v2/tests/database_collection_doc_update_test.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany +// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -126,6 +126,33 @@ func Test_DatabaseCollectionDocUpdateIgnoreRevs(t *testing.T) { }) } +func Test_DatabaseCollectionDocUpdateReturnOldRev(t *testing.T) { + Wrap(t, func(t *testing.T, client arangodb.Client) { + WithDatabase(t, client, nil, func(db arangodb.Database) { + WithCollection(t, db, nil, func(col arangodb.Collection) { + withContextT(t, defaultTestTimeout, func(ctx context.Context, tb testing.TB) { + doc := DocWithRev{ + Name: "test-ORG", + } + + metaOrg, err := col.CreateDocument(ctx, doc) + require.NoError(t, err) + + docUpdate := DocWithRev{ + Name: "test-UPDATED", + } + + t.Run("OldRev should match", func(t *testing.T) { + metaRep, err := col.UpdateDocumentWithOptions(ctx, metaOrg.Key, docUpdate, nil) + require.NoError(t, err) + require.Equal(t, metaOrg.Rev, metaRep.OldRev) + }) + }) + }) + }) + }) +} + func Test_DatabaseCollectionDocUpdateKeepNull(t *testing.T) { Wrap(t, func(t *testing.T, client arangodb.Client) { WithDatabase(t, client, nil, func(db arangodb.Database) {