Skip to content

Commit 2b21d95

Browse files
committed
Migrate patch collection
1 parent dcce9ba commit 2b21d95

116 files changed

Lines changed: 448 additions & 431 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/action/patch_collection.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Democratized Data Foundation
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the file licenses/BSL.txt.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0, included in the file
9+
// licenses/APL.txt.
10+
11+
package action
12+
13+
import (
14+
"github.com/sourcenetwork/immutable"
15+
"github.com/sourcenetwork/lens/host-go/config/model"
16+
17+
"github.com/sourcenetwork/defradb/tests/state"
18+
)
19+
20+
// PatchCollection executes a patch collection command, updating 0 to many collections and applying
21+
// a migration if one is provided.
22+
type PatchCollection struct {
23+
stateful
24+
25+
// NodeID may hold the ID (index) of a node to apply this patch to.
26+
//
27+
// If a value is not provided the patch will be applied to all nodes.
28+
NodeID immutable.Option[int]
29+
30+
// The identity of this request. Optional.
31+
//
32+
// If node acp is enabled, identity will be used to check if this operation can be performed.
33+
Identity immutable.Option[state.Identity]
34+
35+
// The Patch to apply to the collection version.
36+
Patch string
37+
38+
// An optional migration that will be set if the patch creates any new CollectionVersions.
39+
Lens immutable.Option[model.Lens]
40+
41+
// Any error expected from the action. Optional.
42+
//
43+
// String can be a partial, and the test will pass if an error is returned that
44+
// contains this string.
45+
ExpectedError string
46+
}
47+
48+
var _ Action = (*PatchCollection)(nil)
49+
var _ Stateful = (*PatchCollection)(nil)
50+
51+
// Execute executes the patch collection action.
52+
func (a *PatchCollection) Execute() {
53+
// The lens IDs are consistent across nodes, so we can patch once for all nodes.
54+
// This will need to change if patches want to replace more than just lens IDs.
55+
patch := replace(a.s, 0, a.Patch)
56+
57+
nodeIDs, nodes := getNodesWithIDs(a.NodeID, a.s.Nodes)
58+
for index, node := range nodes {
59+
nodeID := nodeIDs[index]
60+
ctx := getContextWithIdentity(a.s.Ctx, a.s, a.Identity, nodeID)
61+
err := node.PatchCollection(ctx, patch, a.Lens)
62+
expectedErrorRaised := assertError(a.s.T, err, a.ExpectedError)
63+
64+
assertExpectedErrorRaised(a.s.T, a.ExpectedError, expectedErrorRaised)
65+
}
66+
67+
// If the schema was updated we need to refresh the collection definitions.
68+
refreshCollections(a.s)
69+
}

tests/integration/acp/nac/collection_patch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestNAC_GatesCollectionPatch_AuthorizedIdentity_AllowAccess(t *testing.T) {
3636
},
3737

3838
// This should work as the identity is authorized.
39-
testUtils.PatchCollection{
39+
&action.PatchCollection{
4040
Identity: testUtils.ClientIdentity(1),
4141
Patch: `
4242
[
@@ -69,7 +69,7 @@ func TestNAC_GatesCollectionPatch_NoIdentity_NotAuthorizedError(t *testing.T) {
6969
},
7070

7171
// We haven't authorized non-identities. So, this should error.
72-
testUtils.PatchCollection{
72+
&action.PatchCollection{
7373
Identity: testUtils.NoIdentity(),
7474
Patch: `
7575
[
@@ -103,7 +103,7 @@ func TestNAC_GatesCollectionPatch_WrongIdentity_NotAuthorizedError(t *testing.T)
103103
},
104104

105105
// Wrong user/identity will also not be authorized.
106-
testUtils.PatchCollection{
106+
&action.PatchCollection{
107107
Identity: testUtils.ClientIdentity(2),
108108
Patch: `
109109
[

tests/integration/acp/nac/collection_set_active_version_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestNAC_GatesCollectionSetActiveVersion_AuthorizedIdentity_AllowAccess(t *t
3636
}
3737
`,
3838
},
39-
testUtils.PatchCollection{
39+
&action.PatchCollection{
4040
Identity: testUtils.ClientIdentity(1),
4141
Patch: `
4242
[
@@ -76,7 +76,7 @@ func TestNAC_GatesCollectionSetActiveVersion_NoIdentity_NotAuthorizedError(t *te
7676
}
7777
`,
7878
},
79-
testUtils.PatchCollection{
79+
&action.PatchCollection{
8080
Identity: testUtils.ClientIdentity(1),
8181
Patch: `
8282
[
@@ -117,7 +117,7 @@ func TestNAC_GatesCollectionSetActiveVersion_WrongIdentity_NotAuthorizedError(t
117117
}
118118
`,
119119
},
120-
testUtils.PatchCollection{
120+
&action.PatchCollection{
121121
Identity: testUtils.ClientIdentity(1),
122122
Patch: `
123123
[

tests/integration/acp/nac/relation_admin/collection_patch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestNAC_AdminRelation_CanCollectionPatch(t *testing.T) {
3636
},
3737

3838
// This user, can not perform this gated operation yet.
39-
testUtils.PatchCollection{
39+
&action.PatchCollection{
4040
Identity: testUtils.ClientIdentity(2),
4141
Patch: `
4242
[
@@ -55,7 +55,7 @@ func TestNAC_AdminRelation_CanCollectionPatch(t *testing.T) {
5555
},
5656

5757
// This user, can now perform this gated operation.
58-
testUtils.PatchCollection{
58+
&action.PatchCollection{
5959
Identity: testUtils.ClientIdentity(2),
6060
Patch: `
6161
[

tests/integration/acp/nac/relation_admin/collection_set_active_version_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestNAC_AdminRelation_CanCollectionSetActiveVersion(t *testing.T) {
3636
}
3737
`,
3838
},
39-
testUtils.PatchCollection{
39+
&action.PatchCollection{
4040
Identity: testUtils.ClientIdentity(1),
4141
Patch: `
4242
[

tests/integration/collection/add_relation_with_schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestSchemaUpdatesAddFieldKindForeignObject_WithPatchAddingOneToManyRelation
190190
}
191191
`,
192192
},
193-
testUtils.PatchCollection{
193+
&action.PatchCollection{
194194
Patch: `
195195
[
196196
{ "op": "add", "path": "/Book/Fields/-", "value": {

tests/integration/collection_version/get_schema_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestGetSchema_ReturnsAllSchema(t *testing.T) {
9090
type Books {}
9191
`,
9292
},
93-
testUtils.PatchCollection{
93+
&action.PatchCollection{
9494
Patch: `
9595
[
9696
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "name", "Kind": "String"} },
@@ -169,7 +169,7 @@ func TestGetSchema_ReturnsSchemaForGivenRoot(t *testing.T) {
169169
type Books {}
170170
`,
171171
},
172-
testUtils.PatchCollection{
172+
&action.PatchCollection{
173173
Patch: `
174174
[
175175
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "name", "Kind": "String"} },
@@ -239,7 +239,7 @@ func TestGetSchema_ReturnsSchemaForGivenName(t *testing.T) {
239239
type Books {}
240240
`,
241241
},
242-
testUtils.PatchCollection{
242+
&action.PatchCollection{
243243
Patch: `
244244
[
245245
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "name", "Kind": "String"} },

tests/integration/collection_version/migrations/add_lens_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ func TestAddLens_WithPatchCollection_TransformsDocuments(t *testing.T) {
154154
"name": "Shahzad"
155155
}`,
156156
},
157-
testUtils.PatchCollection{
157+
&action.PatchCollection{
158158
Patch: `
159159
[
160160
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": 11} }
161161
]
162162
`,
163163
},
164-
testUtils.PatchCollection{
164+
&action.PatchCollection{
165165
Patch: `
166166
[
167167
{

tests/integration/collection_version/migrations/query/simple_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestSchemaMigrationQuery(t *testing.T) {
3636
"name": "John"
3737
}`,
3838
},
39-
testUtils.PatchCollection{
39+
&action.PatchCollection{
4040
Patch: `
4141
[
4242
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -107,7 +107,7 @@ func TestSchemaMigrationQueryMultipleDocs(t *testing.T) {
107107
"name": "Shahzad"
108108
}`,
109109
},
110-
testUtils.PatchCollection{
110+
&action.PatchCollection{
111111
Patch: `
112112
[
113113
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -196,7 +196,7 @@ func TestSchemaMigrationQueryWithMigrationRegisteredBeforePatchCollection(t *tes
196196
},
197197
},
198198
},
199-
testUtils.PatchCollection{
199+
&action.PatchCollection{
200200
Patch: `
201201
[
202202
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -240,14 +240,14 @@ func TestSchemaMigrationQueryMigratesToIntermediaryVersion(t *testing.T) {
240240
"name": "John"
241241
}`,
242242
},
243-
testUtils.PatchCollection{
243+
&action.PatchCollection{
244244
Patch: `
245245
[
246246
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
247247
]
248248
`,
249249
},
250-
testUtils.PatchCollection{
250+
&action.PatchCollection{
251251
Patch: `
252252
[
253253
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": "String"} }
@@ -312,14 +312,14 @@ func TestSchemaMigrationQueryMigratesFromIntermediaryVersion(t *testing.T) {
312312
"name": "John"
313313
}`,
314314
},
315-
testUtils.PatchCollection{
315+
&action.PatchCollection{
316316
Patch: `
317317
[
318318
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
319319
]
320320
`,
321321
},
322-
testUtils.PatchCollection{
322+
&action.PatchCollection{
323323
Patch: `
324324
[
325325
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": "String"} }
@@ -384,14 +384,14 @@ func TestSchemaMigrationQueryMigratesAcrossMultipleVersions(t *testing.T) {
384384
"name": "John"
385385
}`,
386386
},
387-
testUtils.PatchCollection{
387+
&action.PatchCollection{
388388
Patch: `
389389
[
390390
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
391391
]
392392
`,
393393
},
394-
testUtils.PatchCollection{
394+
&action.PatchCollection{
395395
Patch: `
396396
[
397397
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": "String"} }
@@ -505,14 +505,14 @@ func TestSchemaMigrationQueryMigratesAcrossMultipleVersionsBeforePatches(t *test
505505
},
506506
},
507507
},
508-
testUtils.PatchCollection{
508+
&action.PatchCollection{
509509
Patch: `
510510
[
511511
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
512512
]
513513
`,
514514
},
515-
testUtils.PatchCollection{
515+
&action.PatchCollection{
516516
Patch: `
517517
[
518518
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": "String"} }
@@ -593,14 +593,14 @@ func TestSchemaMigrationQueryMigratesAcrossMultipleVersionsBeforePatchesWrongOrd
593593
},
594594
},
595595
},
596-
testUtils.PatchCollection{
596+
&action.PatchCollection{
597597
Patch: `
598598
[
599599
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
600600
]
601601
`,
602602
},
603-
testUtils.PatchCollection{
603+
&action.PatchCollection{
604604
Patch: `
605605
[
606606
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": "String"} }
@@ -652,7 +652,7 @@ func TestSchemaMigrationQueryWithUnknownSchemaMigration(t *testing.T) {
652652
"name": "John"
653653
}`,
654654
},
655-
testUtils.PatchCollection{
655+
&action.PatchCollection{
656656
Patch: `
657657
[
658658
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -713,7 +713,7 @@ func TestSchemaMigrationQueryMigrationMutatesExistingScalarField(t *testing.T) {
713713
"name": "John"
714714
}`,
715715
},
716-
testUtils.PatchCollection{
716+
&action.PatchCollection{
717717
Patch: `
718718
[
719719
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -775,7 +775,7 @@ func TestSchemaMigrationQueryMigrationMutatesExistingInlineArrayField(t *testing
775775
"mobile": [644, 832, 8325]
776776
}`,
777777
},
778-
testUtils.PatchCollection{
778+
&action.PatchCollection{
779779
Patch: `
780780
[
781781
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -839,7 +839,7 @@ func TestSchemaMigrationQueryMigrationRemovesExistingField(t *testing.T) {
839839
"age": 40
840840
}`,
841841
},
842-
testUtils.PatchCollection{
842+
&action.PatchCollection{
843843
Patch: `
844844
[
845845
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -901,7 +901,7 @@ func TestSchemaMigrationQueryMigrationPreservesExistingFieldWhenFieldNotRequeste
901901
"age": 40
902902
}`,
903903
},
904-
testUtils.PatchCollection{
904+
&action.PatchCollection{
905905
Patch: `
906906
[
907907
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -978,7 +978,7 @@ func TestSchemaMigrationQueryMigrationCopiesExistingFieldWhenSrcFieldNotRequeste
978978
"age": 40
979979
}`,
980980
},
981-
testUtils.PatchCollection{
981+
&action.PatchCollection{
982982
Patch: `
983983
[
984984
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "yearsLived", "Kind": "Int"} }
@@ -1041,7 +1041,7 @@ func TestSchemaMigrationQueryMigrationCopiesExistingFieldWhenSrcAndDstFieldNotRe
10411041
"age": 40
10421042
}`,
10431043
},
1044-
testUtils.PatchCollection{
1044+
&action.PatchCollection{
10451045
Patch: `
10461046
[
10471047
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "yearsLived", "Kind": "Int"} }

tests/integration/collection_version/migrations/query/with_doc_id_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestSchemaMigrationQueryByDocID(t *testing.T) {
4343
"name": "Fred"
4444
}`,
4545
},
46-
testUtils.PatchCollection{
46+
&action.PatchCollection{
4747
Patch: `
4848
[
4949
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
@@ -150,7 +150,7 @@ func TestSchemaMigrationQueryMultipleQueriesByDocID(t *testing.T) {
150150
"name": "Dave"
151151
}`,
152152
},
153-
testUtils.PatchCollection{
153+
&action.PatchCollection{
154154
Patch: `
155155
[
156156
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }

0 commit comments

Comments
 (0)