Skip to content

Commit cacc02d

Browse files
committed
fix: migration -> think about a chance of null for array
1 parent 86fac8e commit cacc02d

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

server/internal/infrastructure/mongo/migration/250821145423_change_esri_and_stamen_to_default.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ func ChangeEsriAndStamenToDefault(ctx context.Context, c DBClient) error {
3333

3434
update := bson.M{
3535
"$set": bson.M{
36-
"items.$[].groups.$[].fields.$[f].value": "default",
36+
"items.$[i].groups.$[g].fields.$[f].value": "default",
3737
},
3838
}
3939
arrayFilters := options.ArrayFilters{
4040
Filters: []interface{}{
41+
bson.M{"i.groups": bson.M{"$type": "array"}},
42+
bson.M{"g.fields": bson.M{"$type": "array"}},
4143
bson.M{
4244
"f.field": "tile_type",
4345
"f.value": bson.M{

server/internal/infrastructure/mongo/migration/250821145423_change_esri_and_stamen_to_default_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,24 @@ func TestChangeEsriAndStamenToDefault_MixedStructures(t *testing.T) {
317317
},
318318
}
319319

320+
// Document 5: Invalid structure - items is nil
321+
doc5 := bson.M{
322+
"_id": primitive.NewObjectID(),
323+
"items": nil,
324+
}
325+
326+
// Document 6: Invalid structure - groups is nil
327+
doc6 := bson.M{
328+
"_id": primitive.NewObjectID(),
329+
"items": bson.A{
330+
bson.M{
331+
"groups": nil,
332+
},
333+
},
334+
}
335+
320336
// Insert test documents
321-
_, err := db.Collection("property").InsertMany(ctx, []any{doc1, doc2, doc3, doc4})
337+
_, err := db.Collection("property").InsertMany(ctx, []any{doc1, doc2, doc3, doc4, doc5, doc6})
322338
require.NoError(t, err)
323339

324340
// Run migration
@@ -352,4 +368,16 @@ func TestChangeEsriAndStamenToDefault_MixedStructures(t *testing.T) {
352368
require.NoError(t, err)
353369
value4 := updatedDoc4["items"].(primitive.A)[0].(bson.M)["groups"].(primitive.A)[0].(bson.M)["fields"].(primitive.A)[0].(bson.M)["value"]
354370
assert.Equal(t, "keep_this", value4)
371+
372+
// Verify doc5 was not changed (nil items)
373+
var updatedDoc5 bson.M
374+
err = db.Collection("property").FindOne(ctx, bson.M{"_id": doc5["_id"]}).Decode(&updatedDoc5)
375+
require.NoError(t, err)
376+
assert.Nil(t, updatedDoc5["items"])
377+
378+
// Verify doc6 was not changed (nil groups)
379+
var updatedDoc6 bson.M
380+
err = db.Collection("property").FindOne(ctx, bson.M{"_id": doc6["_id"]}).Decode(&updatedDoc6)
381+
require.NoError(t, err)
382+
assert.Nil(t, updatedDoc6["items"].(primitive.A)[0].(bson.M)["groups"])
355383
}

0 commit comments

Comments
 (0)